diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 7873586a4ad9fde5cd2a8a307860ee6560427946..aa67e1ff85c43aeb69b5efaa9e903b99ebb232b4 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -1604,6 +1604,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) { diff --git a/httpbin/middleware.go b/httpbin/middleware.go index e35a5a9df5ec6b5834c37582fda74801251e9e6d..5186789cdcd25713893b540c11dfde9bc878dcbf 100644 --- a/httpbin/middleware.go +++ b/httpbin/middleware.go @@ -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) })