Download internal/api/middleware_test.go from xjackkx/rg: direct link, hf CLI and curl.
- Browser
- Download file 3.49 kB
-
https://huggingface.co/spaces/xjackkx/rg/resolve/main/internal/api/middleware_test.go
- Command line
-
hf download hf://spaces/xjackkx/rg/internal/api/middleware_test.go
-
curl -L -o middleware_test.go https://huggingface.co/spaces/xjackkx/rg/resolve/main/internal/api/middleware_test.go
3.49 kB
| package api | |
| import ( | |
| "errors" | |
| "io" | |
| "net/http" | |
| "net/http/httptest" | |
| "strings" | |
| "testing" | |
| ) | |
| func TestAuthMiddleware_ValidToken(t *testing.T) { | |
| handler := AuthMiddleware("secret-token", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(http.StatusOK) | |
| })) | |
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | |
| req.Header.Set("Authorization", "Bearer secret-token") | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusOK { | |
| t.Errorf("status: got %d, want %d", rec.Code, http.StatusOK) | |
| } | |
| } | |
| func TestAuthMiddleware_MissingHeader(t *testing.T) { | |
| handler := AuthMiddleware("secret-token", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| t.Fatal("handler should not be called") | |
| })) | |
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusUnauthorized { | |
| t.Errorf("status: got %d, want %d", rec.Code, http.StatusUnauthorized) | |
| } | |
| assertBodyContains(t, rec, "UNAUTHORIZED") | |
| } | |
| func TestAuthMiddleware_WrongToken(t *testing.T) { | |
| handler := AuthMiddleware("secret-token", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| t.Fatal("handler should not be called") | |
| })) | |
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | |
| req.Header.Set("Authorization", "Bearer wrong-token") | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusUnauthorized { | |
| t.Errorf("status: got %d, want %d", rec.Code, http.StatusUnauthorized) | |
| } | |
| assertBodyContains(t, rec, "UNAUTHORIZED") | |
| } | |
| func TestAuthMiddleware_InvalidFormat(t *testing.T) { | |
| handler := AuthMiddleware("secret-token", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| t.Fatal("handler should not be called") | |
| })) | |
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | |
| req.Header.Set("Authorization", "Basic dXNlcjpwYXNz") | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusUnauthorized { | |
| t.Errorf("status: got %d, want %d", rec.Code, http.StatusUnauthorized) | |
| } | |
| } | |
| func TestAuthMiddleware_EmptyConfiguredToken_DisablesAuth(t *testing.T) { | |
| handler := AuthMiddleware("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| w.WriteHeader(http.StatusOK) | |
| })) | |
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusOK { | |
| t.Errorf("status: got %d, want %d", rec.Code, http.StatusOK) | |
| } | |
| } | |
| func TestRequestBodyLimitMiddleware_TooLarge(t *testing.T) { | |
| handler := RequestBodyLimitMiddleware(4, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| _, err := io.ReadAll(r.Body) | |
| if err == nil { | |
| w.WriteHeader(http.StatusOK) | |
| return | |
| } | |
| var maxErr *http.MaxBytesError | |
| if errors.As(err, &maxErr) { | |
| w.WriteHeader(http.StatusRequestEntityTooLarge) | |
| return | |
| } | |
| t.Fatalf("unexpected read error: %v", err) | |
| })) | |
| req := httptest.NewRequest(http.MethodPost, "/test", strings.NewReader("12345")) | |
| rec := httptest.NewRecorder() | |
| handler.ServeHTTP(rec, req) | |
| if rec.Code != http.StatusRequestEntityTooLarge { | |
| t.Fatalf("status: got %d, want %d", rec.Code, http.StatusRequestEntityTooLarge) | |
| } | |
| } | |
| func assertBodyContains(t *testing.T, rec *httptest.ResponseRecorder, substr string) { | |
| t.Helper() | |
| body := rec.Body.String() | |
| if !strings.Contains(body, substr) { | |
| t.Errorf("body %q does not contain %q", body, substr) | |
| } | |
| } | |