40 lines
1.4 KiB
Go
40 lines
1.4 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestAuthCompatibilityEndpoints(t *testing.T) {
|
|
handler := NewAuthCompatibilityHandler()
|
|
cases := []struct {
|
|
path string
|
|
status int
|
|
location string
|
|
body string
|
|
}{
|
|
{"/api/auth/login", http.StatusTemporaryRedirect, "https://example.test/auth/login", ""},
|
|
{"/api/auth/callback", http.StatusTemporaryRedirect, "https://example.test/auth/login?error=callback_failed", ""},
|
|
{"/api/auth/captcha", http.StatusOK, "", `{"enabled":false,"message":"平台账号登录不使用外部验证码。"}` + "\n"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.path, func(t *testing.T) {
|
|
r := httptest.NewRequest(http.MethodGet, "https://example.test"+tc.path, nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, r)
|
|
if w.Code != tc.status || w.Header().Get("Location") != tc.location || w.Body.String() != tc.body {
|
|
t.Fatalf("status=%d location=%q body=%q", w.Code, w.Header().Get("Location"), w.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAuthCompatibilityRejectsUnsupportedMethods(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
NewAuthCompatibilityHandler().ServeHTTP(w, httptest.NewRequest(http.MethodPost, "/api/auth/captcha", nil))
|
|
if w.Code != http.StatusMethodNotAllowed || w.Header().Get("Allow") != http.MethodGet {
|
|
t.Fatalf("status=%d allow=%q", w.Code, w.Header().Get("Allow"))
|
|
}
|
|
}
|