diff --git a/rest/handler/authhandler_test.go b/rest/handler/authhandler_test.go index a9b131196..56ed728e5 100644 --- a/rest/handler/authhandler_test.go +++ b/rest/handler/authhandler_test.go @@ -5,6 +5,7 @@ import ( "net" "net/http" "net/http/httptest" + "strings" "testing" "time" @@ -90,6 +91,28 @@ func TestAuthHandler_NilError(t *testing.T) { }) } +func TestAuthHandlerWithJSONBody(t *testing.T) { + const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A" + + // Create a request with JSON body + jsonBody := `{"username":"test","password":"secret"}` + req := httptest.NewRequest(http.MethodPost, "http://localhost/login", + strings.NewReader(jsonBody)) + req.Header.Set("Content-Type", "application/json") + // Missing authorization header to trigger the unauthorized path + + handler := Authorize(key)( + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + })) + + resp := httptest.NewRecorder() + handler.ServeHTTP(resp, req) + + // Should return unauthorized + assert.Equal(t, http.StatusUnauthorized, resp.Code) +} + func TestAuthHandlerWithMultipartFormData(t *testing.T) { const key = "B63F477D-BBA3-4E52-96D3-C0034C27694A"