mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
ae721bd2b2
Signed-off-by: Vishal Rana <vr@labstack.com>
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGzip(t *testing.T) {
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
|
h := func(c *echo.Context) error {
|
|
return c.String(http.StatusOK, "test")
|
|
}
|
|
|
|
// Skip if no Accept-Encoding header
|
|
Gzip()(h)(c)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
|
|
// Gzip
|
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
|
req.Header.Set(echo.AcceptEncoding, "gzip")
|
|
rec = httptest.NewRecorder()
|
|
c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
|
Gzip()(h)(c)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
|
r, err := gzip.NewReader(rec.Body)
|
|
defer r.Close()
|
|
if assert.NoError(t, err) {
|
|
buf := new(bytes.Buffer)
|
|
buf.ReadFrom(r)
|
|
assert.Equal(t, "test", buf.String())
|
|
}
|
|
}
|