From 6155c900be5539d8d5ebe9ba7a4be5f88e9f067f Mon Sep 17 00:00:00 2001 From: Romain Date: Wed, 5 Jun 2024 15:04:04 +0200 Subject: [PATCH] Passing the correct status code when compression is disabled within the Brotli handler Co-authored-by: Kevin Pollet --- pkg/middlewares/compress/brotli/brotli.go | 2 ++ pkg/middlewares/compress/brotli/brotli_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/middlewares/compress/brotli/brotli.go b/pkg/middlewares/compress/brotli/brotli.go index 17225f252..27802b0f6 100644 --- a/pkg/middlewares/compress/brotli/brotli.go +++ b/pkg/middlewares/compress/brotli/brotli.go @@ -160,6 +160,7 @@ func (r *responseWriter) Write(p []byte) (int, error) { } if !found { r.compressionDisabled = true + r.rw.WriteHeader(r.statusCode) return r.rw.Write(p) } } @@ -167,6 +168,7 @@ func (r *responseWriter) Write(p []byte) (int, error) { for _, excludedContentType := range r.excludedContentTypes { if excludedContentType.equals(mediaType, params) { r.compressionDisabled = true + r.rw.WriteHeader(r.statusCode) return r.rw.Write(p) } } diff --git a/pkg/middlewares/compress/brotli/brotli_test.go b/pkg/middlewares/compress/brotli/brotli_test.go index 005587411..67c794c46 100644 --- a/pkg/middlewares/compress/brotli/brotli_test.go +++ b/pkg/middlewares/compress/brotli/brotli_test.go @@ -356,7 +356,7 @@ func Test_ExcludedContentTypes(t *testing.T) { h := mustNewWrapper(t, cfg)(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { rw.Header().Set(contentType, test.contentType) - rw.WriteHeader(http.StatusOK) + rw.WriteHeader(http.StatusAccepted) _, err := rw.Write(bigTestBody) require.NoError(t, err) @@ -368,7 +368,7 @@ func Test_ExcludedContentTypes(t *testing.T) { rw := httptest.NewRecorder() h.ServeHTTP(rw, req) - assert.Equal(t, http.StatusOK, rw.Code) + assert.Equal(t, http.StatusAccepted, rw.Code) if test.expCompression { 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) { rw.Header().Set(contentType, test.contentType) - rw.WriteHeader(http.StatusOK) + rw.WriteHeader(http.StatusAccepted) _, err := rw.Write(bigTestBody) require.NoError(t, err) @@ -472,7 +472,7 @@ func Test_IncludedContentTypes(t *testing.T) { rw := httptest.NewRecorder() h.ServeHTTP(rw, req) - assert.Equal(t, http.StatusOK, rw.Code) + assert.Equal(t, http.StatusAccepted, rw.Code) if test.expCompression { assert.Equal(t, "br", rw.Header().Get(contentEncoding))