Skip to content
Snippets Groups Projects
Unverified Commit 52382e08 authored by Will McCutchen's avatar Will McCutchen Committed by GitHub
Browse files

Merge pull request #44 from mccutchen/broken-head-requests

Fix handling of HEAD requests for streaming responses
parents 29d7c7ad d9c2d013
No related branches found
No related tags found
No related merge requests found
......@@ -1627,6 +1627,29 @@ func TestDrip(t *testing.T) {
assertStatusCode(t, w, test.code)
})
}
t.Run("ensure HEAD request works with streaming responses", func(t *testing.T) {
srv := httptest.NewServer(handler)
defer srv.Close()
resp, err := http.Head(srv.URL + "/drip?duration=900ms&delay=100ms")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("error reading response body: %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected HTTP 200 OK rsponse, got %d", resp.StatusCode)
}
if bodySize := len(body); bodySize > 0 {
t.Fatalf("expected empty body from HEAD request, bot: %s", string(body))
}
})
}
func TestRange(t *testing.T) {
......
......@@ -61,7 +61,7 @@ func limitRequestSize(maxSize int64, h http.Handler) http.Handler {
// headResponseWriter implements http.ResponseWriter in order to discard the
// body of the response
type headResponseWriter struct {
http.ResponseWriter
*metaResponseWriter
}
func (hw *headResponseWriter) Write(b []byte) (int, error) {
......@@ -72,7 +72,7 @@ func (hw *headResponseWriter) Write(b []byte) (int, error) {
func autohead(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "HEAD" {
w = &headResponseWriter{w}
w = &headResponseWriter{&metaResponseWriter{w: w}}
}
h.ServeHTTP(w, r)
})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment