2015-05-15 21:29:14 +02:00
|
|
|
package middleware
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2019-04-16 22:52:06 +02:00
|
|
|
"io"
|
2016-03-20 17:14:55 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2022-11-21 14:29:43 +02:00
|
|
|
"os"
|
2016-02-05 00:40:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-02-05 00:40:08 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGzip(t *testing.T) {
|
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2016-03-20 17:14:55 +02:00
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Skip if no Accept-Encoding header
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-02-05 00:40:08 +02:00
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
|
|
return nil
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
2016-02-05 00:40:08 +02:00
|
|
|
|
2016-12-20 23:46:25 +02:00
|
|
|
// Gzip
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-12-21 09:00:40 +02:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
2016-04-24 19:21:23 +02:00
|
|
|
r, err := gzip.NewReader(rec.Body)
|
2022-10-12 20:47:21 +02:00
|
|
|
if assert.NoError(t, err) {
|
2016-02-05 00:40:08 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2018-02-21 20:44:17 +02:00
|
|
|
defer r.Close()
|
2016-02-05 00:40:08 +02:00
|
|
|
buf.ReadFrom(r)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, "test", buf.String())
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
2019-04-16 22:52:06 +02:00
|
|
|
|
|
|
|
chunkBuf := make([]byte, 5)
|
|
|
|
|
|
|
|
// Gzip chunked
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
Gzip()(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
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.True(t, rec.Flushed)
|
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
2019-04-16 22:52:06 +02:00
|
|
|
r.Reset(rec.Body)
|
|
|
|
|
|
|
|
_, err = io.ReadFull(r, chunkBuf)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test\n", string(chunkBuf))
|
2019-04-16 22:52:06 +02:00
|
|
|
|
|
|
|
// Write and flush the second part of the data
|
|
|
|
c.Response().Write([]byte("test\n"))
|
|
|
|
c.Response().Flush()
|
|
|
|
|
|
|
|
_, err = io.ReadFull(r, chunkBuf)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test\n", string(chunkBuf))
|
2019-04-16 22:52:06 +02:00
|
|
|
|
|
|
|
// Write the final part of the data and return
|
|
|
|
c.Response().Write([]byte("test"))
|
|
|
|
return nil
|
|
|
|
})(c)
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
defer r.Close()
|
|
|
|
buf.ReadFrom(r)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, "test", buf.String())
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
2023-05-31 07:53:33 +02:00
|
|
|
func TestGzipWithMinLength(t *testing.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)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
2023-05-31 07:53:33 +02:00
|
|
|
r, err := gzip.NewReader(rec.Body)
|
2023-07-16 19:15:24 +02:00
|
|
|
if assert.NoError(t, err) {
|
2023-05-31 07:53:33 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
defer r.Close()
|
|
|
|
buf.ReadFrom(r)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.Equal(t, "foobarfoobar", buf.String())
|
2023-05-31 07:53:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGzipWithMinLengthTooShort(t *testing.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)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.Equal(t, "", rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Contains(t, rec.Body.String(), "test")
|
2023-05-31 07:53:33 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 19:15:24 +02:00
|
|
|
func TestGzipWithResponseWithoutBody(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
e.Use(Gzip())
|
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
return c.Redirect(http.StatusMovedPermanently, "http://localhost")
|
|
|
|
})
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
|
|
rec := httptest.NewRecorder()
|
2023-05-31 07:53:33 +02:00
|
|
|
|
2023-07-16 19:15:24 +02:00
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
|
|
|
|
assert.Equal(t, "", rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGzipWithMinLengthChunked(t *testing.T) {
|
2023-05-31 07:53:33 +02:00
|
|
|
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
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.True(t, rec.Flushed)
|
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
r, err = gzip.NewReader(rec.Body)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.NoError(t, err)
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
_, err = io.ReadFull(r, chunkBuf)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test\n", string(chunkBuf))
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
// Write and flush the second part of the data
|
|
|
|
c.Response().Write([]byte("test\n"))
|
|
|
|
c.Response().Flush()
|
|
|
|
|
|
|
|
_, err = io.ReadFull(r, chunkBuf)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test\n", string(chunkBuf))
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
// Write the final part of the data and return
|
|
|
|
c.Response().Write([]byte("test"))
|
|
|
|
return nil
|
|
|
|
})(c)
|
|
|
|
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.NotNil(t, r)
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
buf.ReadFrom(r)
|
2023-07-16 19:15:24 +02:00
|
|
|
assert.Equal(t, "test", buf.String())
|
2023-05-31 07:53:33 +02:00
|
|
|
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:14:55 +02:00
|
|
|
func TestGzipNoContent(t *testing.T) {
|
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-12-21 09:00:40 +02:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-12-20 23:46:25 +02:00
|
|
|
return c.NoContent(http.StatusNoContent)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-06-07 07:27:36 +02:00
|
|
|
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()))
|
2016-03-20 17:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-15 10:15:13 +02:00
|
|
|
func TestGzipEmpty(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 := Gzip()(func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "")
|
|
|
|
})
|
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Equal(t, "text/plain; charset=UTF-8", rec.Header().Get(echo.HeaderContentType))
|
|
|
|
r, err := gzip.NewReader(rec.Body)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
assert.Equal(t, "", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:14:55 +02:00
|
|
|
func TestGzipErrorReturned(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Gzip())
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2016-12-11 08:05:41 +02:00
|
|
|
return echo.ErrNotFound
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-12-21 09:00:40 +02:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2016-12-11 08:05:41 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
2016-03-20 17:14:55 +02:00
|
|
|
}
|
2017-02-14 05:12:58 +02:00
|
|
|
|
2020-11-07 05:52:35 +02:00
|
|
|
func TestGzipErrorReturnedInvalidConfig(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
// Invalid level
|
|
|
|
e.Use(GzipWithConfig(GzipConfig{Level: 12}))
|
|
|
|
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(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
assert.Contains(t, rec.Body.String(), "gzip")
|
|
|
|
}
|
|
|
|
|
2017-02-14 05:12:58 +02:00
|
|
|
// Issue #806
|
|
|
|
func TestGzipWithStatic(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Gzip())
|
2017-02-15 00:11:10 +02:00
|
|
|
e.Static("/test", "../_fixture/images")
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/test/walle.png", nil)
|
2017-02-14 05:12:58 +02:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
// Data is written out in chunks when Content-Length == "", so only
|
|
|
|
// validate the content length if it's not set.
|
|
|
|
if cl := rec.Header().Get("Content-Length"); cl != "" {
|
|
|
|
assert.Equal(t, cl, rec.Body.Len())
|
|
|
|
}
|
|
|
|
r, err := gzip.NewReader(rec.Body)
|
|
|
|
if assert.NoError(t, err) {
|
2018-02-23 21:32:27 +02:00
|
|
|
defer r.Close()
|
2022-11-21 14:29:43 +02:00
|
|
|
want, err := os.ReadFile("../_fixture/images/walle.png")
|
2018-02-23 21:32:27 +02:00
|
|
|
if assert.NoError(t, err) {
|
2018-02-25 23:42:58 +02:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
assert.Equal(t, want, buf.Bytes())
|
2018-02-23 21:32:27 +02:00
|
|
|
}
|
2017-02-14 05:12:58 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-07 05:52:35 +02:00
|
|
|
|
|
|
|
func BenchmarkGzip(b *testing.B) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
|
|
|
|
|
|
h := Gzip()(func(c echo.Context) error {
|
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
// Gzip
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h(c)
|
|
|
|
}
|
|
|
|
}
|