mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
gzip response only if it exceeds a minimal length (#2267)
* gzip response only if it exceeds a minimal length If the response is too short, e.g. a few bytes, compressing the response makes it even larger. The new parameter MinLength to the GzipConfig struct allows to set a threshold (in bytes) as of which response size the compression should be applied. If the response is shorter, no compression will be applied.
This commit is contained in:
@ -88,6 +88,123 @@ func TestGzip(t *testing.T) {
|
||||
assert.Equal(t, "test", buf.String())
|
||||
}
|
||||
|
||||
func TestGzipWithMinLength(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
e := echo.New()
|
||||
// Minimal response length
|
||||
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
||||
e.GET("/", func(c echo.Context) error {
|
||||
c.Response().Write([]byte("foobarfoobar"))
|
||||
return nil
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
||||
r, err := gzip.NewReader(rec.Body)
|
||||
if assert.NoError(err) {
|
||||
buf := new(bytes.Buffer)
|
||||
defer r.Close()
|
||||
buf.ReadFrom(r)
|
||||
assert.Equal("foobarfoobar", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGzipWithMinLengthTooShort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
e := echo.New()
|
||||
// Minimal response length
|
||||
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
||||
e.GET("/", func(c echo.Context) error {
|
||||
c.Response().Write([]byte("test"))
|
||||
return nil
|
||||
})
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal("", rec.Header().Get(echo.HeaderContentEncoding))
|
||||
assert.Contains(rec.Body.String(), "test")
|
||||
}
|
||||
|
||||
func TestGzipWithMinLengthChunked(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
e := echo.New()
|
||||
|
||||
// Gzip chunked
|
||||
chunkBuf := make([]byte, 5)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
var r *gzip.Reader = nil
|
||||
|
||||
c := e.NewContext(req, rec)
|
||||
GzipWithConfig(GzipConfig{MinLength: 10})(func(c echo.Context) error {
|
||||
c.Response().Header().Set("Content-Type", "text/event-stream")
|
||||
c.Response().Header().Set("Transfer-Encoding", "chunked")
|
||||
|
||||
// Write and flush the first part of the data
|
||||
c.Response().Write([]byte("test\n"))
|
||||
c.Response().Flush()
|
||||
|
||||
// Read the first part of the data
|
||||
assert.True(rec.Flushed)
|
||||
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
||||
|
||||
var err error
|
||||
r, err = gzip.NewReader(rec.Body)
|
||||
assert.NoError(err)
|
||||
|
||||
_, err = io.ReadFull(r, chunkBuf)
|
||||
assert.NoError(err)
|
||||
assert.Equal("test\n", string(chunkBuf))
|
||||
|
||||
// Write and flush the second part of the data
|
||||
c.Response().Write([]byte("test\n"))
|
||||
c.Response().Flush()
|
||||
|
||||
_, err = io.ReadFull(r, chunkBuf)
|
||||
assert.NoError(err)
|
||||
assert.Equal("test\n", string(chunkBuf))
|
||||
|
||||
// Write the final part of the data and return
|
||||
c.Response().Write([]byte("test"))
|
||||
return nil
|
||||
})(c)
|
||||
|
||||
assert.NotNil(r)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
buf.ReadFrom(r)
|
||||
assert.Equal("test", buf.String())
|
||||
|
||||
r.Close()
|
||||
}
|
||||
|
||||
func TestGzipWithMinLengthNoContent(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
h := GzipWithConfig(GzipConfig{MinLength: 10})(func(c echo.Context) error {
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
})
|
||||
if assert.NoError(t, h(c)) {
|
||||
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
||||
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
|
||||
assert.Equal(t, 0, len(rec.Body.Bytes()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGzipNoContent(t *testing.T) {
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
|
Reference in New Issue
Block a user