Skip to content

Commit 4c42a46

Browse files
committed
1 parent 0bf0ff7 commit 4c42a46

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

gzhttp/asserts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func assertEqual(t testing.TB, want, got interface{}) {
1515
func assertNotEqual(t testing.TB, want, got interface{}) {
1616
t.Helper()
1717
if reflect.DeepEqual(want, got) {
18-
t.Fatalf("want %#v, got %#v", want, got)
18+
t.Fatalf("did not want %#v, got %#v", want, got)
1919
}
2020
}
2121

gzhttp/gzip.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
9797
// If a Content-Type wasn't specified, infer it from the current buffer.
9898
if ct == "" {
9999
ct = http.DetectContentType(w.buf)
100+
}
101+
102+
// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
103+
// Set the header only if the key does not exist
104+
_, haveType := w.Header()["Content-Type"]
105+
if !haveType {
100106
w.Header().Set(contentType, ct)
101107
}
102108
// If the Content-Type is acceptable to GZIP, initialize the GZIP writer.

gzhttp/gzip_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,18 @@ func newTestHandler(body string) http.Handler {
649649
}
650650
}))
651651
}
652+
653+
func TestGzipHandlerNilContentType(t *testing.T) {
654+
// This just exists to provide something for GzipHandler to wrap.
655+
handler := newTestHandler(testBody)
656+
657+
// content-type header not set when provided nil
658+
659+
req, _ := http.NewRequest("GET", "/whatever", nil)
660+
req.Header.Set("Accept-Encoding", "gzip")
661+
res := httptest.NewRecorder()
662+
res.Header()["Content-Type"] = nil
663+
handler.ServeHTTP(res, req)
664+
665+
assertEqual(t, "", res.Header().Get("Content-Type"))
666+
}

0 commit comments

Comments
 (0)