1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
echo/middleware/compress_test.go

52 lines
1.0 KiB
Go
Raw Normal View History

package middleware
import (
"compress/gzip"
"net/http"
"net/http/httptest"
"testing"
"bytes"
"github.com/labstack/echo"
)
func TestGzip(t *testing.T) {
// Empty Accept-Encoding header
req, _ := http.NewRequest(echo.GET, "/", nil)
w := httptest.NewRecorder()
res := echo.NewResponse(w)
c := echo.NewContext(req, res, echo.New())
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
Gzip()(h)(c)
s := w.Body.String()
if s != "test" {
t.Errorf("expected `test`, with empty Accept-Encoding header, got %s.", s)
}
// Content-Encoding header
req.Header.Set(echo.AcceptEncoding, "gzip")
w = httptest.NewRecorder()
c.Response().SetWriter(w)
Gzip()(h)(c)
ce := w.Header().Get(echo.ContentEncoding)
if ce != "gzip" {
t.Errorf("expected Content-Encoding header `gzip`, got %d.", ce)
}
// Body
r, err := gzip.NewReader(w.Body)
defer r.Close()
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
buf.ReadFrom(r)
s = buf.String()
if s != "test" {
t.Errorf("expected body `test`, got %s.", s)
}
}