Skip to content

Commit

Permalink
feat: allow POST, PUT, DELETE, PATCH methods on /basic-auth endpoint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeuer authored Feb 12, 2024
1 parent 06f6727 commit f6ce865
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
90 changes: 51 additions & 39 deletions httpbin/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f6ce865

Please sign in to comment.