1
0
mirror of https://github.com/labstack/echo.git synced 2025-05-31 23:19:42 +02:00
echo/middleware/compress_test.go
Vishal Rana 73fa05f826 Added panic recover middleware
Signed-off-by: Vishal Rana <vr@labstack.com>
2015-05-17 22:54:29 -07:00

53 lines
1.1 KiB
Go

package middleware
import (
"compress/gzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
)
func TestGzip(t *testing.T) {
// Empty Accept-Encoding header
req, _ := http.NewRequest(echo.GET, "/", nil)
w := httptest.NewRecorder()
res := &echo.Response{Writer: w}
c := echo.NewContext(req, res, echo.New())
h := func(c *echo.Context) *echo.HTTPError {
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.Response{Writer: 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.Error(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
s = string(b)
if s != "test" {
t.Errorf("expected body `test`, got %s.", s)
}
}