1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
echo/middleware/compress_test.go
Vishal Rana 1ee3bc23e3 Added gzip middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-15 12:29:14 -07:00

43 lines
844 B
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"compress/gzip"
"github.com/labstack/echo"
"io/ioutil"
)
func TestGzip(t *testing.T) {
req, _ := http.NewRequest(echo.GET, "/", nil)
req.Header.Set(echo.AcceptEncoding, "gzip")
w := httptest.NewRecorder()
res := &echo.Response{Writer: w}
c := echo.NewContext(req, res, echo.New())
Gzip()(func(c *echo.Context) *echo.HTTPError {
return c.String(http.StatusOK, "test")
})(c)
if w.Header().Get(echo.ContentEncoding) != "gzip" {
t.Errorf("expected Content-Encoding: gzip, got %d", w.Header().Get(echo.ContentEncoding))
}
r, err := gzip.NewReader(w.Body)
defer r.Close()
if err != nil {
t.Error(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
s := string(b)
if s != "test" {
t.Errorf(`expected "test", got "%s"`, s)
}
}