Skip to content

Commit

Permalink
Add explicit /head handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mccutchen committed Jul 13, 2021
1 parent 03e69a9 commit 61e4e87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
53 changes: 38 additions & 15 deletions httpbin/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,24 +216,47 @@ func TestGet(t *testing.T) {
}

func TestHEAD(t *testing.T) {
r, _ := http.NewRequest("HEAD", "/", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
testCases := []struct {
verb string
path string
wantCode int
}{
{"HEAD", "/", http.StatusOK},
{"HEAD", "/get", http.StatusOK},
{"HEAD", "/head", http.StatusOK},
{"HEAD", "/post", http.StatusMethodNotAllowed},
{"GET", "/head", http.StatusMethodNotAllowed},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s %s", tc.verb, tc.path), func(t *testing.T) {
r, _ := http.NewRequest(tc.verb, tc.path, nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)

assertStatusCode(t, w, 200)
assertBodyEquals(t, w, "")
assertStatusCode(t, w, tc.wantCode)

contentLengthStr := w.Header().Get("Content-Length")
if contentLengthStr == "" {
t.Fatalf("missing Content-Length header in response")
}
contentLength, err := strconv.Atoi(contentLengthStr)
if err != nil {
t.Fatalf("error converting Content-Lengh %v to integer: %s", contentLengthStr, err)
}
if contentLength <= 0 {
t.Fatalf("Content-Lengh %v should be greater than 0", contentLengthStr)
// we only do further validation when we get an OK response
if tc.wantCode != http.StatusOK {
return
}

assertStatusCode(t, w, http.StatusOK)
assertBodyEquals(t, w, "")

contentLengthStr := w.Header().Get("Content-Length")
if contentLengthStr == "" {
t.Fatalf("missing Content-Length header in response")
}
contentLength, err := strconv.Atoi(contentLengthStr)
if err != nil {
t.Fatalf("error converting Content-Lengh %v to integer: %s", contentLengthStr, err)
}
if contentLength <= 0 {
t.Fatalf("Content-Lengh %v should be greater than 0", contentLengthStr)
}
})
}

}

func TestCORS(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions httpbin/httpbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ func (h *HTTPBin) Handler() http.Handler {
mux.HandleFunc("/forms/post", methods(h.FormsPost, "GET"))
mux.HandleFunc("/encoding/utf8", methods(h.UTF8, "GET"))

mux.HandleFunc("/delete", methods(h.RequestWithBody, "DELETE"))
mux.HandleFunc("/get", methods(h.Get, "GET"))
mux.HandleFunc("/head", methods(h.Get, "HEAD"))
mux.HandleFunc("/patch", methods(h.RequestWithBody, "PATCH"))
mux.HandleFunc("/post", methods(h.RequestWithBody, "POST"))
mux.HandleFunc("/put", methods(h.RequestWithBody, "PUT"))
mux.HandleFunc("/patch", methods(h.RequestWithBody, "PATCH"))
mux.HandleFunc("/delete", methods(h.RequestWithBody, "DELETE"))

mux.HandleFunc("/ip", h.IP)
mux.HandleFunc("/user-agent", h.UserAgent)
Expand Down

0 comments on commit 61e4e87

Please sign in to comment.