2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
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"
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/testhelpers"
|
2017-06-06 23:02:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-08-21 09:10:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-13 15:56:01 +00:00
|
|
|
acceptEncodingHeader = "Accept-Encoding"
|
|
|
|
contentEncodingHeader = "Content-Encoding"
|
2017-11-10 13:12:02 +00:00
|
|
|
contentTypeHeader = "Content-Type"
|
2017-07-13 15:56:01 +00:00
|
|
|
varyHeader = "Vary"
|
|
|
|
gzipValue = "gzip"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|
|
|
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)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2018-08-06 18:00:03 +00:00
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
assert.NoError(t, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
})
|
|
|
|
handler := &compress{next: next}
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
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) {
|
|
|
|
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)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-07-13 15:56:01 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler := &compress{next: next}
|
2017-07-13 15:56:01 +00:00
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
|
|
|
|
fakeBody := generateBytes(gziphandler.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler := &compress{next: next}
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
2017-11-10 13:12:02 +00:00
|
|
|
func TestShouldNotCompressWhenGRPC(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
req.Header.Add(contentTypeHeader, "application/grpc")
|
|
|
|
|
|
|
|
baseBody := generateBytes(gziphandler.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler := &compress{next: next}
|
2017-11-10 13:12:02 +00:00
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
2018-11-14 09:18:03 +00:00
|
|
|
handler.ServeHTTP(rw, req)
|
2017-11-10 13:12:02 +00:00
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(acceptEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), baseBody)
|
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
func TestIntegrationShouldNotCompress(t *testing.T) {
|
2017-07-13 15:56:01 +00:00
|
|
|
fakeCompressedBody := generateBytes(100000)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2018-11-14 09:18:03 +00:00
|
|
|
handler http.Handler
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "when content already compressed",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when content already compressed and status code Created",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.WriteHeader(http.StatusCreated)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeCompressedBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusCreated,
|
|
|
|
},
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
compress := &compress{next: test.handler}
|
|
|
|
ts := httptest.NewServer(compress)
|
2017-08-21 09:10:03 +00:00
|
|
|
defer ts.Close()
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, fakeCompressedBody, body)
|
|
|
|
})
|
|
|
|
}
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:48:03 +00:00
|
|
|
func TestShouldWriteHeaderWhenFlush(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-11-08 23:48:03 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipValue)
|
|
|
|
rw.Header().Add(varyHeader, acceptEncodingHeader)
|
|
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
|
|
rw.(http.Flusher).Flush()
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write([]byte("short"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
2017-11-08 23:48:03 +00:00
|
|
|
})
|
2018-11-14 09:18:03 +00:00
|
|
|
handler := &compress{next: next}
|
|
|
|
ts := httptest.NewServer(handler)
|
2017-11-08 23:48:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
|
|
|
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
func TestIntegrationShouldCompress(t *testing.T) {
|
2017-07-13 15:56:01 +00:00
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
2018-11-14 09:18:03 +00:00
|
|
|
handler http.Handler
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "when AcceptEncoding header is present",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "when AcceptEncoding header is present and status code Created",
|
2018-11-14 09:18:03 +00:00
|
|
|
handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2017-08-21 09:10:03 +00:00
|
|
|
rw.WriteHeader(http.StatusCreated)
|
2018-11-14 09:18:03 +00:00
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}),
|
2017-08-21 09:10:03 +00:00
|
|
|
expectedStatusCode: http.StatusCreated,
|
|
|
|
},
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2018-11-14 09:18:03 +00:00
|
|
|
compress := &compress{next: test.handler}
|
|
|
|
ts := httptest.NewServer(compress)
|
2017-08-21 09:10:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
|
|
|
req.Header.Add(acceptEncodingHeader, gzipValue)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, test.expectedStatusCode, resp.StatusCode)
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, gzipValue, resp.Header.Get(contentEncodingHeader))
|
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2017-08-21 09:10:03 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if assert.ObjectsAreEqualValues(body, fakeBody) {
|
|
|
|
assert.Fail(t, "expected a compressed body", "got %v", body)
|
|
|
|
}
|
|
|
|
})
|
2017-07-13 15:56:01 +00:00
|
|
|
}
|
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
|
|
|
|
}
|