2015-05-15 21:29:14 +02:00
|
|
|
package middleware
|
|
|
|
|
2016-02-05 00:40:08 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2016-03-20 17:14:55 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2016-02-05 00:40:08 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGzip(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-03-22 02:27:14 +02:00
|
|
|
rq := test.NewRequest(echo.GET, "/", nil)
|
2016-02-05 00:40:08 +02:00
|
|
|
rec := test.NewResponseRecorder()
|
2016-03-22 02:27:14 +02:00
|
|
|
c := echo.NewContext(rq, rec, e)
|
2016-03-20 17:14:55 +02:00
|
|
|
|
2016-02-15 18:11:29 +02:00
|
|
|
// Skip if no Accept-Encoding header
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-02-05 00:40:08 +02:00
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
|
|
return nil
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2016-02-05 00:40:08 +02:00
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
|
|
|
2016-03-22 02:27:14 +02:00
|
|
|
rq = test.NewRequest(echo.GET, "/", nil)
|
2016-04-06 16:28:53 +02:00
|
|
|
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
|
2016-02-05 00:40:08 +02:00
|
|
|
rec = test.NewResponseRecorder()
|
2016-03-22 02:27:14 +02:00
|
|
|
c = echo.NewContext(rq, rec, e)
|
2016-02-05 00:40:08 +02:00
|
|
|
|
|
|
|
// Gzip
|
2016-04-02 23:19:39 +02:00
|
|
|
h(c)
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Equal(t, "gzip", rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
2016-02-05 00:40:08 +02:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:14:55 +02:00
|
|
|
func TestGzipNoContent(t *testing.T) {
|
|
|
|
e := echo.New()
|
2016-03-22 02:27:14 +02:00
|
|
|
rq := test.NewRequest(echo.GET, "/", nil)
|
2016-03-20 17:14:55 +02:00
|
|
|
rec := test.NewResponseRecorder()
|
2016-03-22 02:27:14 +02:00
|
|
|
c := echo.NewContext(rq, rec, e)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-03-20 17:14:55 +02:00
|
|
|
return c.NoContent(http.StatusOK)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2016-03-20 17:14:55 +02:00
|
|
|
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
|
2016-03-20 17:14:55 +02:00
|
|
|
b, err := ioutil.ReadAll(rec.Body)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, 0, len(b))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGzipErrorReturned(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Gzip())
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Get("/", func(c echo.Context) error {
|
2016-03-20 17:14:55 +02:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "error")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-03-22 02:27:14 +02:00
|
|
|
rq := test.NewRequest(echo.GET, "/", nil)
|
2016-03-20 17:14:55 +02:00
|
|
|
rec := test.NewResponseRecorder()
|
2016-03-22 02:27:14 +02:00
|
|
|
e.ServeHTTP(rq, rec)
|
2016-02-05 00:40:08 +02:00
|
|
|
|
2016-04-06 16:28:53 +02:00
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
2016-03-20 17:14:55 +02:00
|
|
|
b, err := ioutil.ReadAll(rec.Body)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "error", string(b))
|
|
|
|
}
|
|
|
|
}
|