2015-05-15 12:29:14 -07:00
|
|
|
package middleware
|
|
|
|
|
2016-02-04 14:40:08 -08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2017-02-13 19:12:58 -08:00
|
|
|
"io/ioutil"
|
2016-03-20 08:14:55 -07:00
|
|
|
"net/http"
|
2016-09-22 22:53:44 -07:00
|
|
|
"net/http/httptest"
|
2016-02-04 14:40:08 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGzip(t *testing.T) {
|
|
|
|
e := echo.New()
|
2017-02-22 15:57:12 -08:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-09-22 22:53:44 -07:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 10:21:23 -07:00
|
|
|
c := e.NewContext(req, rec)
|
2016-03-20 08:14:55 -07:00
|
|
|
|
2016-02-15 08:11:29 -08:00
|
|
|
// Skip if no Accept-Encoding header
|
2016-04-02 14:19:39 -07:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-02-04 14:40:08 -08:00
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
|
|
return nil
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
|
|
|
h(c)
|
2016-04-24 10:21:23 -07:00
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
2016-02-04 14:40:08 -08:00
|
|
|
|
2016-12-20 13:46:25 -08:00
|
|
|
// Gzip
|
2017-05-13 15:44:27 -07:00
|
|
|
req = httptest.NewRequest(echo.GET, "/", nil)
|
2016-12-20 23:00:40 -08:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-22 22:53:44 -07:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 10:21:23 -07:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h(c)
|
2016-12-20 23:00:40 -08:00
|
|
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
2016-04-24 10:21:23 -07:00
|
|
|
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
|
|
|
r, err := gzip.NewReader(rec.Body)
|
2016-02-04 14:40:08 -08:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
buf := new(bytes.Buffer)
|
2018-02-21 21:44:17 +03:00
|
|
|
defer r.Close()
|
2016-02-04 14:40:08 -08:00
|
|
|
buf.ReadFrom(r)
|
|
|
|
assert.Equal(t, "test", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 08:14:55 -07:00
|
|
|
func TestGzipNoContent(t *testing.T) {
|
|
|
|
e := echo.New()
|
2017-02-22 15:57:12 -08:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-12-20 23:00:40 -08:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-22 22:53:44 -07:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 10:21:23 -07:00
|
|
|
c := e.NewContext(req, rec)
|
2016-04-02 14:19:39 -07:00
|
|
|
h := Gzip()(func(c echo.Context) error {
|
2016-12-20 13:46:25 -08:00
|
|
|
return c.NoContent(http.StatusNoContent)
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
2016-06-06 22:27:36 -07:00
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
|
|
|
|
assert.Equal(t, 0, len(rec.Body.Bytes()))
|
2016-03-20 08:14:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGzipErrorReturned(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Gzip())
|
2016-04-18 16:59:58 -07:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2016-12-10 22:05:41 -08:00
|
|
|
return echo.ErrNotFound
|
2016-04-02 14:19:39 -07:00
|
|
|
})
|
2017-02-22 15:57:12 -08:00
|
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
2016-12-20 23:00:40 -08:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
2016-09-22 22:53:44 -07:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2016-12-10 22:05:41 -08:00
|
|
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
2016-04-06 07:28:53 -07:00
|
|
|
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
2016-03-20 08:14:55 -07:00
|
|
|
}
|
2017-02-13 19:12:58 -08:00
|
|
|
|
|
|
|
// Issue #806
|
|
|
|
func TestGzipWithStatic(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(Gzip())
|
2017-02-14 14:11:10 -08:00
|
|
|
e.Static("/test", "../_fixture/images")
|
2017-02-22 15:57:12 -08:00
|
|
|
req := httptest.NewRequest(echo.GET, "/test/walle.png", nil)
|
2017-02-13 19:12:58 -08:00
|
|
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
// Data is written out in chunks when Content-Length == "", so only
|
|
|
|
// validate the content length if it's not set.
|
|
|
|
if cl := rec.Header().Get("Content-Length"); cl != "" {
|
|
|
|
assert.Equal(t, cl, rec.Body.Len())
|
|
|
|
}
|
|
|
|
r, err := gzip.NewReader(rec.Body)
|
|
|
|
if assert.NoError(t, err) {
|
2018-02-23 22:32:27 +03:00
|
|
|
defer r.Close()
|
|
|
|
want, err := ioutil.ReadFile("../_fixture/images/walle.png")
|
|
|
|
if assert.NoError(t, err) {
|
2018-02-25 22:42:58 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
assert.Equal(t, want, buf.Bytes())
|
2018-02-23 22:32:27 +03:00
|
|
|
}
|
2017-02-13 19:12:58 -08:00
|
|
|
}
|
|
|
|
}
|