diff --git a/middleware/compress.go b/middleware/compress.go index 7754d5db8..b551b6ce6 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -51,6 +51,7 @@ type gzipResponseWriter struct { wroteBody bool minLength int minLengthExceeded bool + passthrough bool buffer *bytes.Buffer code int } @@ -91,7 +92,8 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) { res := c.Response() res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding) - if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) { + var succeeded bool + if acceptsGzip(c.Request().Header.Values(echo.HeaderAcceptEncoding)) { i := pool.Get() w, ok := i.(*gzip.Writer) if !ok { @@ -113,9 +115,16 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) { // There are different reasons for cases when we have not yet written response to the client and now need to do so. // a) handler response had only response code and no response body (ala 404 or redirects etc). Response code need to be written now. // b) body is shorter than our minimum length threshold and being buffered currently and needs to be written - if !grw.wroteBody { - if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme { - res.Header().Del(echo.HeaderContentEncoding) + // Responses already encoded by the handler pass through unchanged. + if grw.passthrough || (succeeded && grw.passThroughIfEncoded()) { + c.SetResponse(rw) + w.Reset(io.Discard) + } else if !grw.wroteBody && !grw.minLengthExceeded { + // Nothing was compressed, so no coding applies to the body that follows. + res.Header().Del(echo.HeaderContentEncoding) + if !succeeded { + // The error handler writes its own body. + res.Header().Del(echo.HeaderContentLength) } if grw.wroteHeader { rw.WriteHeader(grw.code) @@ -139,21 +148,31 @@ func (config GzipConfig) ToMiddleware() (echo.MiddlewareFunc, error) { pool.Put(w) }() } - return next(c) + err := next(c) + succeeded = err == nil + return err } }, nil } func (w *gzipResponseWriter) WriteHeader(code int) { - w.Header().Del(echo.HeaderContentLength) // Issue #444 + if w.passthrough { + w.ResponseWriter.WriteHeader(code) + return + } w.wroteHeader = true // Delay writing of the header until we know if we'll actually compress the response w.code = code + w.passThroughIfEncoded() } func (w *gzipResponseWriter) Write(b []byte) (int, error) { + if w.passThroughIfEncoded() { + return w.ResponseWriter.Write(b) + } + if w.Header().Get(echo.HeaderContentType) == "" { w.Header().Set(echo.HeaderContentType, http.DetectContentType(b)) } @@ -166,12 +185,13 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) { w.minLengthExceeded = true // The minimum length is exceeded, add Content-Encoding header and write the header - w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806 - if w.wroteHeader { - w.ResponseWriter.WriteHeader(w.code) - } + w.startCompression() - return w.Writer.Write(w.buffer.Bytes()) + if _, err := w.Writer.Write(w.buffer.Bytes()); err != nil { + return 0, err + } + // Report only the caller's bytes; the buffer also holds earlier writes. + return len(b), nil } return n, err @@ -181,13 +201,23 @@ func (w *gzipResponseWriter) Write(b []byte) (int, error) { } func (w *gzipResponseWriter) Flush() { + if w.passThroughIfEncoded() { + if !w.wroteHeader { + // Commit through echo.Response so the error handler sees the response as written. + w.wroteHeader = true + w.code = http.StatusOK + if r, err := echo.UnwrapResponse(w.ResponseWriter); err == nil && !r.Committed { + r.WriteHeader(http.StatusOK) + } + } + _ = http.NewResponseController(w.ResponseWriter).Flush() + return + } + if !w.minLengthExceeded { // Enforce compression because we will not know how much more data will come w.minLengthExceeded = true - w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806 - if w.wroteHeader { - w.ResponseWriter.WriteHeader(w.code) - } + w.startCompression() _, _ = w.Writer.Write(w.buffer.Bytes()) } @@ -198,6 +228,46 @@ func (w *gzipResponseWriter) Flush() { _ = http.NewResponseController(w.ResponseWriter).Flush() } +// startCompression commits gzip headers once the response is known to be compressed. +func (w *gzipResponseWriter) startCompression() { + w.Header().Set(echo.HeaderContentEncoding, gzipScheme) // Issue #806 + w.Header().Del(echo.HeaderContentLength) // Issue #444 + if w.wroteHeader { + w.ResponseWriter.WriteHeader(w.code) + } +} + +// passThroughIfEncoded switches to the underlying writer before gzip handles the body. +func (w *gzipResponseWriter) passThroughIfEncoded() bool { + if w.passthrough { + return true + } + if w.wroteBody || w.minLengthExceeded { + return false + } + if !hasContentCoding(w.Header().Values(echo.HeaderContentEncoding)) { + return false + } + w.passthrough = true + if w.wroteHeader { + w.ResponseWriter.WriteHeader(w.code) + } + return true +} + +// hasContentCoding reports whether Content-Encoding lists any coding other than identity. +func hasContentCoding(headers []string) bool { + for _, header := range headers { + for coding := range strings.SplitSeq(header, ",") { + coding = strings.TrimSpace(coding) + if coding != "" && !strings.EqualFold(coding, "identity") { + return true + } + } + } + return false +} + func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) { return http.NewResponseController(w.ResponseWriter).Hijack() } @@ -233,3 +303,51 @@ func bufferPool() sync.Pool { }, } } + +func acceptsGzip(headers []string) bool { + var gzipAllowed, wildcardAllowed, wildcardRefused bool + for _, header := range headers { + for entry := range strings.SplitSeq(header, ",") { + coding, weight, hasWeight := strings.Cut(entry, ";") + coding = strings.TrimSpace(coding) + switch { + case strings.EqualFold(coding, gzipScheme), strings.EqualFold(coding, "x-gzip"): + // RFC 9110, section 8.4.1.3: x-gzip is equivalent to gzip. + // An explicit refusal or invalid weight takes precedence over + // wildcards and over any duplicate entry allowing gzip. + if hasWeight && !positiveQualityValue(weight) { + return false + } + gzipAllowed = true + case coding == "*": + if hasWeight && !positiveQualityValue(weight) { + wildcardRefused = true + } else { + wildcardAllowed = true + } + } + } + } + return gzipAllowed || (wildcardAllowed && !wildcardRefused) +} + +// positiveQualityValue matches the qvalue grammar in RFC 9110, section 12.4.2. +func positiveQualityValue(weight string) bool { + name, value, ok := strings.Cut(strings.TrimSpace(weight), "=") + if !ok || !strings.EqualFold(name, "q") { + return false + } + whole, fraction, _ := strings.Cut(value, ".") + if (whole != "0" && whole != "1") || len(fraction) > 3 { + return false + } + positive := whole == "1" + for i := range len(fraction) { + digit := fraction[i] + if digit < '0' || digit > '9' || (whole == "1" && digit != '0') { + return false + } + positive = positive || digit != '0' + } + return positive +} diff --git a/middleware/compress_proxy_test.go b/middleware/compress_proxy_test.go new file mode 100644 index 000000000..8d2c24540 --- /dev/null +++ b/middleware/compress_proxy_test.go @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors + +package middleware + +import ( + "bytes" + "compress/gzip" + "compress/zlib" + "io" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "testing" + + "github.com/labstack/echo/v5" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGzip_PreservesProxyContentEncoding(t *testing.T) { + tests := []struct { + name string + method string + minLength int + emptyEncodingField bool + }{ + {"get", http.MethodGet, 0, false}, + {"get above threshold", http.MethodGet, 8, false}, + {"get below threshold", http.MethodGet, 4096, false}, + {"head", http.MethodHead, 0, false}, + {"empty first encoding field", http.MethodGet, 0, true}, + } + payload := []byte("encoded upstream response") + for _, encoding := range []string{"gzip", "deflate"} { + var encoded bytes.Buffer + var compressor io.WriteCloser + if encoding == "gzip" { + compressor = gzip.NewWriter(&encoded) + } else { + compressor = zlib.NewWriter(&encoded) + } + _, err := compressor.Write(payload) + require.NoError(t, err) + require.NoError(t, compressor.Close()) + wire := encoded.Bytes() + + for _, tt := range tests { + t.Run(encoding+"/"+tt.name, func(t *testing.T) { + encodings := []string{encoding} + if tt.emptyEncodingField { + encodings = []string{"", encoding} + } + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, tt.method, r.Method) + for _, value := range encodings { + w.Header().Add(echo.HeaderContentEncoding, value) + } + w.Header().Set(echo.HeaderContentType, echo.MIMETextPlain) + w.Header().Set(echo.HeaderContentLength, strconv.Itoa(len(wire))) + w.WriteHeader(http.StatusCreated) + if r.Method != http.MethodHead { + _, _ = w.Write(wire) + } + })) + defer upstream.Close() + target, err := url.Parse(upstream.URL) + require.NoError(t, err) + + e := echo.New() + e.Use(GzipWithConfig(GzipConfig{MinLength: tt.minLength})) + e.Use(Proxy(NewRoundRobinBalancer([]*ProxyTarget{{URL: target}}))) + proxy := httptest.NewServer(e) + defer proxy.Close() + + transport := &http.Transport{DisableCompression: true} + defer transport.CloseIdleConnections() + client := &http.Client{Transport: transport} + req, err := http.NewRequest(tt.method, proxy.URL, nil) + require.NoError(t, err) + req.Header.Set(echo.HeaderAcceptEncoding, "gzip, deflate") + res, err := client.Do(req) + require.NoError(t, err) + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + require.NoError(t, err) + + assert.Equal(t, http.StatusCreated, res.StatusCode) + assert.Equal(t, encodings, res.Header.Values(echo.HeaderContentEncoding)) + assert.Equal(t, int64(len(wire)), res.ContentLength) + if tt.method == http.MethodHead { + assert.Empty(t, body) + } else { + assert.Equal(t, wire, body, "proxy must preserve the upstream encoded bytes") + } + }) + } + } +} diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 084ffc9c7..7b5794e31 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -6,15 +6,20 @@ package middleware import ( "bytes" "compress/gzip" + "fmt" "io" "net/http" "net/http/httptest" "os" + "strconv" + "strings" "testing" + "testing/iotest" "time" "github.com/labstack/echo/v5" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGzip_NoAcceptEncodingHeader(t *testing.T) { @@ -68,6 +73,357 @@ func TestGzip_AcceptEncodingHeader(t *testing.T) { assert.Equal(t, "test", buf.String()) } +func TestGzip_AcceptEncodingQuality(t *testing.T) { + tests := []struct { + name string + headers []string + gzip bool + }{ + {"explicit refusal", []string{"gzip;q=0"}, false}, + {"zero fraction", []string{"gzip;q=0.000"}, false}, + {"partial token", []string{"custom-gzip"}, false}, + {"uppercase", []string{"GZIP; Q=1.000"}, true}, + {"minimum positive weight", []string{"gzip;q=0.001"}, true}, + {"multiple fields", []string{"br", "gzip;q=0.5"}, true}, + {"wildcard", []string{"br, *;q=0.5"}, true}, + {"wildcard refusal", []string{"*;q=0"}, false}, + {"explicit refusal overrides wildcard", []string{"*", "gzip;q=0"}, false}, + {"explicit allowance overrides wildcard", []string{"*;q=0, gzip"}, true}, + {"empty", []string{""}, false}, + {"identity", []string{"identity"}, false}, + {"x-gzip", []string{"x-gzip"}, true}, + {"x-gzip case-insensitive", []string{"X-GZIP"}, true}, + {"x-gzip refusal", []string{"x-gzip;q=0"}, false}, + {"invalid weight", []string{"gzip;q=bogus"}, false}, + {"out-of-range weight", []string{"gzip;q=2"}, false}, + {"too much precision", []string{"gzip;q=0.0001"}, false}, + {"nonzero digit after one", []string{"gzip;q=1.1"}, false}, + {"exponent", []string{"gzip;q=1e-1"}, false}, + {"malformed explicit weight overrides wildcard", []string{"*, gzip;q="}, false}, + {"duplicate weights", []string{"gzip;q=0;q=1"}, false}, + {"duplicate refusal then allowance", []string{"gzip;q=0", "gzip;q=1"}, false}, + {"duplicate allowance then refusal", []string{"gzip;q=1, gzip;q=0"}, false}, + {"duplicate wildcard refusal", []string{"*;q=0, *"}, false}, + } + + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { return c.String(http.StatusOK, "test") }) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/", nil) + for _, value := range tt.headers { + req.Header.Add(echo.HeaderAcceptEncoding, value) + } + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, echo.HeaderAcceptEncoding, rec.Header().Get(echo.HeaderVary)) + if !tt.gzip { + assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding)) + assert.Equal(t, "test", rec.Body.String()) + return + } + require.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) + reader, err := gzip.NewReader(rec.Body) + require.NoError(t, err) + defer reader.Close() + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Equal(t, "test", string(body)) + }) + } +} + +func TestGzip_CompressesIdentityContentEncoding(t *testing.T) { + for _, value := range []string{"identity", "IDENTITY", "identity, identity"} { + t.Run(value, func(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + c.Response().Header().Set(echo.HeaderContentEncoding, value) + return c.String(http.StatusOK, "test") + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + require.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) + reader, err := gzip.NewReader(rec.Body) + require.NoError(t, err) + defer reader.Close() + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Equal(t, "test", string(body)) + }) + } +} + +func TestGzip_DropsContentLengthOnImplicitHeader(t *testing.T) { + for _, flush := range []bool{false, true} { + t.Run(fmt.Sprintf("flush=%v", flush), func(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + w := c.Response() + w.Header().Set(echo.HeaderContentLength, "4") + if _, err := w.Write([]byte("test")); err != nil { + return err + } + if flush { + return http.NewResponseController(w).Flush() + } + return nil + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + require.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) + assert.Empty(t, rec.Header().Get(echo.HeaderContentLength)) + reader, err := gzip.NewReader(rec.Body) + require.NoError(t, err) + defer reader.Close() + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Equal(t, "test", string(body)) + }) + } +} + +func TestGzip_PreservesContentLengthWhenNotCompressed(t *testing.T) { + encoded, err := gzipString("already encoded") + require.NoError(t, err) + + tests := []struct { + name string + minLength int + handler echo.HandlerFunc + body []byte + }{ + { + name: "below min length", + minLength: 100, + handler: func(c *echo.Context) error { + w := c.Response() + w.Header().Set(echo.HeaderContentLength, "4") + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte("test")) + return err + }, + body: []byte("test"), + }, + { + name: "encoding set after WriteHeader", + handler: func(c *echo.Context) error { + w := c.Response() + w.Header().Set(echo.HeaderContentLength, strconv.Itoa(len(encoded))) + w.WriteHeader(http.StatusOK) + w.Header().Set(echo.HeaderContentEncoding, gzipScheme) + _, err := w.Write(encoded) + return err + }, + body: encoded, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := echo.New() + e.Use(GzipWithConfig(GzipConfig{MinLength: tt.minLength})) + e.GET("/", tt.handler) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, strconv.Itoa(len(tt.body)), rec.Header().Get(echo.HeaderContentLength)) + assert.Equal(t, tt.body, rec.Body.Bytes()) + }) + } +} + +func TestGzip_ErrorClearsEncodingHeaders(t *testing.T) { + tests := []struct { + name string + encoding string + writeHeader bool + code int + }{ + {name: "gzip", encoding: gzipScheme, code: http.StatusBadRequest}, + {name: "case-insensitive gzip", encoding: "GZIP", code: http.StatusBadRequest}, + {name: "other coding", encoding: "br", code: http.StatusBadRequest}, + {name: "after WriteHeader", writeHeader: true, code: http.StatusOK}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + w := c.Response() + if tt.encoding != "" { + w.Header().Set(echo.HeaderContentEncoding, tt.encoding) + } + w.Header().Set(echo.HeaderContentLength, "50") + if tt.writeHeader { + w.WriteHeader(http.StatusOK) + } + return echo.ErrBadRequest + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, tt.code, rec.Code) + assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding)) + assert.Empty(t, rec.Header().Get(echo.HeaderContentLength)) + }) + } +} + +func TestGzip_WriteReportsCallerBytes(t *testing.T) { + e := echo.New() + e.Use(GzipWithConfig(GzipConfig{MinLength: 10})) + e.GET("/", func(c *echo.Context) error { + _, err := io.Copy(c.Response(), iotest.OneByteReader(strings.NewReader(strings.Repeat("a", 20)))) + return err + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + require.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) + reader, err := gzip.NewReader(rec.Body) + require.NoError(t, err) + defer reader.Close() + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Equal(t, strings.Repeat("a", 20), string(body)) +} + +func TestGzip_KeepsContentLengthWithoutBody(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.HEAD("/", func(c *echo.Context) error { + c.Response().Header().Set(echo.HeaderContentLength, "1234") + return c.NoContent(http.StatusOK) + }) + req := httptest.NewRequest(http.MethodHead, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "1234", rec.Header().Get(echo.HeaderContentLength)) + assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding)) +} + +func TestGzip_PassthroughFlushCommitsResponse(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + c.Response().Header().Set(echo.HeaderContentEncoding, gzipScheme) + if err := http.NewResponseController(c.Response()).Flush(); err != nil { + return err + } + r, err := echo.UnwrapResponse(c.Response()) + require.NoError(t, err) + assert.True(t, r.Committed) + return echo.ErrBadRequest + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Empty(t, rec.Body.String()) +} + +func TestGzip_FlushWithoutWriteProducesValidStream(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + return http.NewResponseController(c.Response()).Flush() + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + require.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding)) + reader, err := gzip.NewReader(rec.Body) + require.NoError(t, err) + defer reader.Close() + body, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Empty(t, body) +} + +func TestGzip_PreservesEncodedResponseOnWriteOrFlush(t *testing.T) { + encoded, err := gzipString("already encoded") + require.NoError(t, err) + + for _, flush := range []bool{false, true} { + name := "implicit header" + if flush { + name = "flush before write" + } + t.Run(name, func(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.GET("/", func(c *echo.Context) error { + w := c.Response() + w.Header().Set(echo.HeaderContentEncoding, gzipScheme) + if flush { + if err := http.NewResponseController(w).Flush(); err != nil { + return err + } + } + _, err := w.Write(encoded) + return err + }) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, gzipScheme, rec.Result().Header.Get(echo.HeaderContentEncoding)) + assert.Equal(t, flush, rec.Flushed) + assert.Equal(t, encoded, rec.Body.Bytes()) + }) + } +} + +func TestGzip_PreservesImplicitHeadEncoding(t *testing.T) { + e := echo.New() + e.Use(Gzip()) + e.HEAD("/", func(c *echo.Context) error { + c.Response().Header().Set(echo.HeaderContentEncoding, gzipScheme) + c.Response().Header().Set(echo.HeaderContentLength, "42") + return nil + }) + + req := httptest.NewRequest(http.MethodHead, "/", nil) + req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, gzipScheme, rec.Result().Header.Get(echo.HeaderContentEncoding)) + assert.Equal(t, int64(42), rec.Result().ContentLength) + assert.Empty(t, rec.Body.Bytes()) +} + func TestGzip_chunked(t *testing.T) { e := echo.New() req := httptest.NewRequest(http.MethodGet, "/", nil)