Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed May 31, 2021
1 parent 0bf0ff7 commit 4c42a46
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gzhttp/asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func assertEqual(t testing.TB, want, got interface{}) {
func assertNotEqual(t testing.TB, want, got interface{}) {
t.Helper()
if reflect.DeepEqual(want, got) {
t.Fatalf("want %#v, got %#v", want, got)
t.Fatalf("did not want %#v, got %#v", want, got)
}
}

Expand Down
6 changes: 6 additions & 0 deletions gzhttp/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
// If a Content-Type wasn't specified, infer it from the current buffer.
if ct == "" {
ct = http.DetectContentType(w.buf)
}

// Handles the intended case of setting a nil Content-Type (as for http/server or http/fs)
// Set the header only if the key does not exist
_, haveType := w.Header()["Content-Type"]
if !haveType {
w.Header().Set(contentType, ct)
}
// If the Content-Type is acceptable to GZIP, initialize the GZIP writer.
Expand Down
15 changes: 15 additions & 0 deletions gzhttp/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,18 @@ func newTestHandler(body string) http.Handler {
}
}))
}

func TestGzipHandlerNilContentType(t *testing.T) {
// This just exists to provide something for GzipHandler to wrap.
handler := newTestHandler(testBody)

// content-type header not set when provided nil

req, _ := http.NewRequest("GET", "/whatever", nil)
req.Header.Set("Accept-Encoding", "gzip")
res := httptest.NewRecorder()
res.Header()["Content-Type"] = nil
handler.ServeHTTP(res, req)

assertEqual(t, "", res.Header().Get("Content-Type"))
}

0 comments on commit 4c42a46

Please sign in to comment.