mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
6f728d428d
Signed-off-by: Vishal Rana <vr@labstack.com>
52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
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 = echo.NewResponse(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)
|
|
}
|
|
}
|