Passing the correct status code when compression is disabled within the Brotli handler

Co-authored-by: Kevin Pollet <pollet.kevin@gmail.com>
This commit is contained in:
Romain 2024-06-05 15:04:04 +02:00 committed by GitHub
parent 6ca4c5da5c
commit 6155c900be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 4 deletions

View file

@ -160,6 +160,7 @@ func (r *responseWriter) Write(p []byte) (int, error) {
} }
if !found { if !found {
r.compressionDisabled = true r.compressionDisabled = true
r.rw.WriteHeader(r.statusCode)
return r.rw.Write(p) return r.rw.Write(p)
} }
} }
@ -167,6 +168,7 @@ func (r *responseWriter) Write(p []byte) (int, error) {
for _, excludedContentType := range r.excludedContentTypes { for _, excludedContentType := range r.excludedContentTypes {
if excludedContentType.equals(mediaType, params) { if excludedContentType.equals(mediaType, params) {
r.compressionDisabled = true r.compressionDisabled = true
r.rw.WriteHeader(r.statusCode)
return r.rw.Write(p) return r.rw.Write(p)
} }
} }

View file

@ -356,7 +356,7 @@ func Test_ExcludedContentTypes(t *testing.T) {
h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set(contentType, test.contentType) rw.Header().Set(contentType, test.contentType)
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusAccepted)
_, err := rw.Write(bigTestBody) _, err := rw.Write(bigTestBody)
require.NoError(t, err) require.NoError(t, err)
@ -368,7 +368,7 @@ func Test_ExcludedContentTypes(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)
if test.expCompression { if test.expCompression {
assert.Equal(t, "br", rw.Header().Get(contentEncoding)) assert.Equal(t, "br", rw.Header().Get(contentEncoding))
@ -460,7 +460,7 @@ func Test_IncludedContentTypes(t *testing.T) {
h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set(contentType, test.contentType) rw.Header().Set(contentType, test.contentType)
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusAccepted)
_, err := rw.Write(bigTestBody) _, err := rw.Write(bigTestBody)
require.NoError(t, err) require.NoError(t, err)
@ -472,7 +472,7 @@ func Test_IncludedContentTypes(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)
if test.expCompression { if test.expCompression {
assert.Equal(t, "br", rw.Header().Get(contentEncoding)) assert.Equal(t, "br", rw.Header().Get(contentEncoding))