diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 406ef03..635ed8a 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -1526,54 +1526,66 @@ func TestCookies(t *testing.T) { func TestBasicAuth(t *testing.T) { t.Run("ok", func(t *testing.T) { - t.Parallel() - - req := newTestRequest(t, "GET", "/basic-auth/user/pass") - req.SetBasicAuth("user", "pass") - - resp := must.DoReq(t, client, req) - result := mustParseResponse[authResponse](t, resp) - expectedResult := authResponse{ - Authorized: true, - User: "user", + for _, method := range []string{"GET", "POST", "PUT", "DELETE", "PATCH"} { + method := method + t.Run(method, func(t *testing.T) { + t.Parallel() + req := newTestRequest(t, method, "/basic-auth/user/pass") + req.SetBasicAuth("user", "pass") + + resp := must.DoReq(t, client, req) + result := mustParseResponse[authResponse](t, resp) + expectedResult := authResponse{ + Authorized: true, + User: "user", + } + assert.DeepEqual(t, result, expectedResult, "expected authorized user") + }) } - assert.DeepEqual(t, result, expectedResult, "expected authorized user") }) t.Run("error/no auth", func(t *testing.T) { - t.Parallel() - - req := newTestRequest(t, "GET", "/basic-auth/user/pass") - resp := must.DoReq(t, client, req) - assert.StatusCode(t, resp, http.StatusUnauthorized) - assert.ContentType(t, resp, jsonContentType) - assert.Header(t, resp, "WWW-Authenticate", `Basic realm="Fake Realm"`) - - result := must.Unmarshal[authResponse](t, resp.Body) - expectedResult := authResponse{ - Authorized: false, - User: "", + for _, method := range []string{"GET", "POST", "PUT", "DELETE", "PATCH"} { + method := method + t.Run(method, func(t *testing.T) { + t.Parallel() + req := newTestRequest(t, method, "/basic-auth/user/pass") + resp := must.DoReq(t, client, req) + assert.StatusCode(t, resp, http.StatusUnauthorized) + assert.ContentType(t, resp, jsonContentType) + assert.Header(t, resp, "WWW-Authenticate", `Basic realm="Fake Realm"`) + + result := must.Unmarshal[authResponse](t, resp.Body) + expectedResult := authResponse{ + Authorized: false, + User: "", + } + assert.DeepEqual(t, result, expectedResult, "expected unauthorized user") + }) } - assert.DeepEqual(t, result, expectedResult, "expected unauthorized user") }) t.Run("error/bad auth", func(t *testing.T) { - t.Parallel() - - req := newTestRequest(t, "GET", "/basic-auth/user/pass") - req.SetBasicAuth("bad", "auth") - - resp := must.DoReq(t, client, req) - assert.StatusCode(t, resp, http.StatusUnauthorized) - assert.ContentType(t, resp, jsonContentType) - assert.Header(t, resp, "WWW-Authenticate", `Basic realm="Fake Realm"`) - - result := must.Unmarshal[authResponse](t, resp.Body) - expectedResult := authResponse{ - Authorized: false, - User: "bad", + for _, method := range []string{"GET", "POST", "PUT", "DELETE", "PATCH"} { + method := method + t.Run(method, func(t *testing.T) { + t.Parallel() + req := newTestRequest(t, method, "/basic-auth/user/pass") + req.SetBasicAuth("bad", "auth") + + resp := must.DoReq(t, client, req) + assert.StatusCode(t, resp, http.StatusUnauthorized) + assert.ContentType(t, resp, jsonContentType) + assert.Header(t, resp, "WWW-Authenticate", `Basic realm="Fake Realm"`) + + result := must.Unmarshal[authResponse](t, resp.Body) + expectedResult := authResponse{ + Authorized: false, + User: "bad", + } + assert.DeepEqual(t, result, expectedResult, "expected unauthorized user") + }) } - assert.DeepEqual(t, result, expectedResult, "expected unauthorized user") }) errorTests := []struct { diff --git a/httpbin/httpbin.go b/httpbin/httpbin.go index 495b72c..8e7ff3d 100644 --- a/httpbin/httpbin.go +++ b/httpbin/httpbin.go @@ -129,7 +129,7 @@ func (h *HTTPBin) Handler() http.Handler { mux.HandleFunc("/anything", h.Anything) mux.HandleFunc("/anything/", h.Anything) mux.HandleFunc("/base64/", h.Base64) - mux.HandleFunc("/basic-auth/", h.BasicAuth) + mux.HandleFunc("/basic-auth/", methods(h.BasicAuth, "GET", "POST", "PUT", "DELETE", "PATCH")) mux.HandleFunc("/bearer", h.Bearer) mux.HandleFunc("/bytes/", h.Bytes) mux.HandleFunc("/cache", h.Cache)