2015-05-15 21:29:14 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2015-06-09 18:16:52 +02:00
|
|
|
"bytes"
|
2015-05-18 07:54:29 +02:00
|
|
|
"compress/gzip"
|
2015-05-15 21:29:14 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2015-06-25 01:55:44 +02:00
|
|
|
"time"
|
2015-05-15 21:29:14 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-05-30 19:54:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-15 21:29:14 +02:00
|
|
|
)
|
|
|
|
|
2015-06-25 01:55:44 +02:00
|
|
|
type closeNotifyingRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
closed chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCloseNotifyingRecorder() *closeNotifyingRecorder {
|
|
|
|
return &closeNotifyingRecorder{
|
|
|
|
httptest.NewRecorder(),
|
|
|
|
make(chan bool, 1),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *closeNotifyingRecorder) close() {
|
|
|
|
c.closed <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
|
|
|
|
return c.closed
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:29:14 +02:00
|
|
|
func TestGzip(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
2015-05-30 19:54:55 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
2015-05-20 23:38:51 +02:00
|
|
|
h := func(c *echo.Context) error {
|
2015-06-25 01:55:44 +02:00
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
2015-06-24 22:36:47 +02:00
|
|
|
return nil
|
2015-05-18 07:54:29 +02:00
|
|
|
}
|
2015-06-09 18:16:52 +02:00
|
|
|
|
|
|
|
// Skip if no Accept-Encoding header
|
2015-05-18 07:54:29 +02:00
|
|
|
Gzip()(h)(c)
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
2015-05-15 21:29:14 +02:00
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
2015-05-18 07:54:29 +02:00
|
|
|
req.Header.Set(echo.AcceptEncoding, "gzip")
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
2015-06-24 22:36:47 +02:00
|
|
|
|
|
|
|
// Gzip
|
2015-05-18 07:54:29 +02:00
|
|
|
Gzip()(h)(c)
|
2015-05-30 19:54:55 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
|
2015-06-24 22:36:47 +02:00
|
|
|
assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
|
2015-05-30 19:54:55 +02:00
|
|
|
r, err := gzip.NewReader(rec.Body)
|
2015-05-15 21:29:14 +02:00
|
|
|
defer r.Close()
|
2015-05-30 19:54:55 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(r)
|
|
|
|
assert.Equal(t, "test", buf.String())
|
2015-05-15 21:29:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 01:55:44 +02:00
|
|
|
|
|
|
|
func TestGzipFlush(t *testing.T) {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := gzip.NewWriter(buf)
|
|
|
|
gw := gzipWriter{Writer: w, ResponseWriter: rec}
|
|
|
|
|
|
|
|
n0 := buf.Len()
|
|
|
|
if n0 != 0 {
|
|
|
|
t.Fatalf("buffer size = %d before writes; want 0", n0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gw.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n1 := buf.Len()
|
|
|
|
if n1 == 0 {
|
|
|
|
t.Fatal("no data after first flush")
|
|
|
|
}
|
|
|
|
|
|
|
|
gw.Write([]byte("x"))
|
|
|
|
|
|
|
|
n2 := buf.Len()
|
|
|
|
if n1 != n2 {
|
|
|
|
t.Fatalf("after writing a single byte, size changed from %d to %d; want no change", n1, n2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := gw.Flush(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n3 := buf.Len()
|
|
|
|
if n2 == n3 {
|
|
|
|
t.Fatal("Flush didn't flush any data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGzipCloseNotify(t *testing.T) {
|
|
|
|
rec := newCloseNotifyingRecorder()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w := gzip.NewWriter(buf)
|
|
|
|
gw := gzipWriter{Writer: w, ResponseWriter: rec}
|
|
|
|
closed := false
|
|
|
|
notifier := gw.CloseNotify()
|
|
|
|
rec.close()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-notifier:
|
|
|
|
closed = true
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, closed, true)
|
|
|
|
}
|
2015-09-12 00:23:57 +02:00
|
|
|
|
|
|
|
func BenchmarkGzip(b *testing.B) {
|
|
|
|
|
|
|
|
b.StopTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
h := func(c *echo.Context) error {
|
|
|
|
c.Response().Write([]byte("test")) // For Content-Type sniffing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
req.Header.Set(echo.AcceptEncoding, "gzip")
|
|
|
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
|
|
|
Gzip()(h)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|