Fix brotli response status code when compression is disabled

This commit is contained in:
Romain 2024-01-30 17:32:05 +01:00 committed by GitHub
parent 9be523d772
commit 85039e0d54
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 2 deletions

View file

@ -138,6 +138,7 @@ func (r *responseWriter) Write(p []byte) (int, error) {
// If we detect a contentEncoding, we know we are never going to compress. // If we detect a contentEncoding, we know we are never going to compress.
if r.rw.Header().Get(contentEncoding) != "" { if r.rw.Header().Get(contentEncoding) != "" {
r.compressionDisabled = true r.compressionDisabled = true
r.rw.WriteHeader(r.statusCode)
return r.rw.Write(p) return r.rw.Write(p)
} }

View file

@ -27,7 +27,7 @@ func Test_Vary(t *testing.T) {
rw := httptest.NewRecorder() rw := httptest.NewRecorder()
h.ServeHTTP(rw, req) h.ServeHTTP(rw, req)
assert.Equal(t, http.StatusOK, rw.Code) assert.Equal(t, http.StatusAccepted, rw.Code)
assert.Equal(t, acceptEncoding, rw.Header().Get(vary)) assert.Equal(t, acceptEncoding, rw.Header().Get(vary))
} }
@ -41,7 +41,7 @@ func Test_SmallBodyNoCompression(t *testing.T) {
h.ServeHTTP(rw, req) h.ServeHTTP(rw, req)
// With less than 1024 bytes the response should not be compressed. // With less than 1024 bytes the response should not be compressed.
assert.Equal(t, http.StatusOK, rw.Code) assert.Equal(t, http.StatusAccepted, rw.Code)
assert.Empty(t, rw.Header().Get(contentEncoding)) assert.Empty(t, rw.Header().Get(contentEncoding))
assert.Equal(t, smallTestBody, rw.Body.Bytes()) assert.Equal(t, smallTestBody, rw.Body.Bytes())
} }
@ -55,6 +55,7 @@ func Test_AlreadyCompressed(t *testing.T) {
rw := httptest.NewRecorder() rw := httptest.NewRecorder()
h.ServeHTTP(rw, req) h.ServeHTTP(rw, req)
assert.Equal(t, http.StatusAccepted, rw.Code)
assert.Equal(t, bigTestBody, rw.Body.Bytes()) assert.Equal(t, bigTestBody, rw.Body.Bytes())
} }
@ -749,6 +750,7 @@ func newTestHandler(t *testing.T, body []byte) http.Handler {
rw.Header().Set("Content-Encoding", "br") rw.Header().Set("Content-Encoding", "br")
} }
rw.WriteHeader(http.StatusAccepted)
_, err := rw.Write(body) _, err := rw.Write(body)
require.NoError(t, err) require.NoError(t, err)
}), }),