2017-06-06 23:02:02 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2017-07-13 15:56:01 +00:00
|
|
|
"io/ioutil"
|
2017-06-06 23:02:02 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/NYTimes/gziphandler"
|
2017-07-13 15:56:01 +00:00
|
|
|
"github.com/codegangsta/negroni"
|
2017-06-06 23:02:02 +00:00
|
|
|
"github.com/containous/traefik/testhelpers"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-13 15:56:01 +00:00
|
|
|
acceptEncodingHeader = "Accept-Encoding"
|
|
|
|
contentEncodingHeader = "Content-Encoding"
|
|
|
|
varyHeader = "Vary"
|
|
|
|
gzipValue = "gzip"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|
|
|
handler := &Compress{}
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2017-07-13 15:56:01 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
baseBody := generateBytes(gziphandler.DefaultMinSize)
|
|
|
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Write(baseBody)
|
|
|
|
}
|
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
rw := httptest.NewRecorder()
|
2017-06-06 23:02:02 +00:00
|
|
|
handler.ServeHTTP(rw, req, next)
|
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
2017-06-06 23:02:02 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
|
|
|
|
|
|
|
if assert.ObjectsAreEqualValues(rw.Body.Bytes(), baseBody) {
|
|
|
|
assert.Fail(t, "expected a compressed body", "got %v", rw.Body.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
|
|
|
handler := &Compress{}
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2017-07-13 15:56:01 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
fakeCompressedBody := generateBytes(gziphandler.DefaultMinSize)
|
|
|
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.Write(fakeCompressedBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req, next)
|
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeCompressedBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenNoAcceptEncodingHeader(t *testing.T) {
|
|
|
|
handler := &Compress{}
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
|
|
|
|
fakeBody := generateBytes(gziphandler.DefaultMinSize)
|
2017-06-06 23:02:02 +00:00
|
|
|
next := func(rw http.ResponseWriter, r *http.Request) {
|
2017-07-13 15:56:01 +00:00
|
|
|
rw.Write(fakeBody)
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req, next)
|
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationShouldNotCompressWhenContentAlreadyCompressed(t *testing.T) {
|
|
|
|
fakeCompressedBody := generateBytes(100000)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
handler := func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.Write(fakeCompressedBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
comp := &Compress{}
|
|
|
|
|
|
|
|
negro := negroni.New(comp)
|
|
|
|
negro.UseHandlerFunc(handler)
|
|
|
|
ts := httptest.NewServer(negro)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert.NoError(t, err, "there should be no error")
|
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert.EqualValues(t, fakeCompressedBody, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationShouldCompressWhenAcceptEncodingHeaderIsPresent(t *testing.T) {
|
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
|
|
|
handler := func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Write(fakeBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
comp := &Compress{}
|
|
|
|
|
|
|
|
negro := negroni.New(comp)
|
|
|
|
negro.UseHandlerFunc(handler)
|
|
|
|
ts := httptest.NewServer(negro)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert.NoError(t, err, "there should be no error")
|
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if assert.ObjectsAreEqualValues(body, fakeBody) {
|
|
|
|
assert.Fail(t, "expected a compressed body", "got %v", body)
|
|
|
|
}
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateBytes(len int) []byte {
|
|
|
|
var value []byte
|
|
|
|
for i := 0; i < len; i++ {
|
2017-07-13 15:56:01 +00:00
|
|
|
value = append(value, 0x61+byte(i))
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|