mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
d9a6052c67
Signed-off-by: Vishal Rana <vr@labstack.com>
74 lines
1.9 KiB
Go
74 lines
1.9 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) {
|
|
e := echo.New()
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
|
|
// Skip if no Accept-Encoding header
|
|
h := Gzip()(func(c echo.Context) error {
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
return nil
|
|
})
|
|
h(c)
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
|
|
// Gzip
|
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
h(c)
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
|
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
|
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())
|
|
}
|
|
}
|
|
|
|
func TestGzipNoContent(t *testing.T) {
|
|
e := echo.New()
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
h := Gzip()(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 TestGzipErrorReturned(t *testing.T) {
|
|
e := echo.New()
|
|
e.Use(Gzip())
|
|
e.GET("/", func(c echo.Context) error {
|
|
return echo.ErrNotFound
|
|
})
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
rec := httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
|
}
|