Skip to content
Snippets Groups Projects
Commit 61e4e875 authored by Will McCutchen's avatar Will McCutchen
Browse files

Add explicit /head handler

parent 03e69a9f
No related branches found
No related tags found
No related merge requests found
...@@ -216,11 +216,31 @@ func TestGet(t *testing.T) { ...@@ -216,11 +216,31 @@ func TestGet(t *testing.T) {
} }
func TestHEAD(t *testing.T) { func TestHEAD(t *testing.T) {
r, _ := http.NewRequest("HEAD", "/", nil) 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() w := httptest.NewRecorder()
handler.ServeHTTP(w, r) handler.ServeHTTP(w, r)
assertStatusCode(t, w, 200) assertStatusCode(t, w, tc.wantCode)
// 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, "") assertBodyEquals(t, w, "")
contentLengthStr := w.Header().Get("Content-Length") contentLengthStr := w.Header().Get("Content-Length")
...@@ -234,6 +254,9 @@ func TestHEAD(t *testing.T) { ...@@ -234,6 +254,9 @@ func TestHEAD(t *testing.T) {
if contentLength <= 0 { if contentLength <= 0 {
t.Fatalf("Content-Lengh %v should be greater than 0", contentLengthStr) t.Fatalf("Content-Lengh %v should be greater than 0", contentLengthStr)
} }
})
}
} }
func TestCORS(t *testing.T) { func TestCORS(t *testing.T) {
......
...@@ -124,11 +124,12 @@ func (h *HTTPBin) Handler() http.Handler { ...@@ -124,11 +124,12 @@ func (h *HTTPBin) Handler() http.Handler {
mux.HandleFunc("/forms/post", methods(h.FormsPost, "GET")) mux.HandleFunc("/forms/post", methods(h.FormsPost, "GET"))
mux.HandleFunc("/encoding/utf8", methods(h.UTF8, "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("/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("/post", methods(h.RequestWithBody, "POST"))
mux.HandleFunc("/put", methods(h.RequestWithBody, "PUT")) 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("/ip", h.IP)
mux.HandleFunc("/user-agent", h.UserAgent) mux.HandleFunc("/user-agent", h.UserAgent)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment