2018-11-14 09:18:03 +00:00
|
|
|
package compress
|
2017-06-06 23:02:02 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-15 09:56:08 +00:00
|
|
|
"compress/gzip"
|
2019-10-31 10:36:05 +00:00
|
|
|
"context"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2017-06-06 23:02:02 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-06-14 15:42:44 +00:00
|
|
|
"net/http/httptrace"
|
|
|
|
"net/textproto"
|
2017-06-06 23:02:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
"github.com/klauspost/compress/gzhttp"
|
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"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/testhelpers"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-07-13 15:56:01 +00:00
|
|
|
contentEncodingHeader = "Content-Encoding"
|
2017-11-10 13:12:02 +00:00
|
|
|
contentTypeHeader = "Content-Type"
|
2017-07-13 15:56:01 +00:00
|
|
|
varyHeader = "Vary"
|
2017-06-06 23:02:02 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
func TestNegotiation(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
acceptEncHeader string
|
|
|
|
expEncoding string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no accept header",
|
2024-01-16 14:30:06 +00:00
|
|
|
expEncoding: "",
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "unsupported accept header",
|
|
|
|
acceptEncHeader: "notreal",
|
|
|
|
expEncoding: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "accept any header",
|
|
|
|
acceptEncHeader: "*",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: brotliName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "gzip accept header",
|
|
|
|
acceptEncHeader: "gzip",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: gzipName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "br accept header",
|
|
|
|
acceptEncHeader: "br",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: brotliName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multi accept header, prefer br",
|
|
|
|
acceptEncHeader: "br;q=0.8, gzip;q=0.6",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: brotliName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
2024-06-06 14:42:04 +00:00
|
|
|
desc: "multi accept header, prefer gzip",
|
2022-11-15 09:56:08 +00:00
|
|
|
acceptEncHeader: "gzip;q=1.0, br;q=0.8",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: gzipName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multi accept header list, prefer br",
|
|
|
|
acceptEncHeader: "gzip, br",
|
2024-06-12 09:38:04 +00:00
|
|
|
expEncoding: brotliName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "zstd accept header",
|
|
|
|
acceptEncHeader: "zstd",
|
|
|
|
expEncoding: zstdName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multi accept header, prefer zstd",
|
|
|
|
acceptEncHeader: "zstd;q=0.9, br;q=0.8, gzip;q=0.6",
|
|
|
|
expEncoding: zstdName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multi accept header, prefer gzip",
|
|
|
|
acceptEncHeader: "gzip;q=1.0, br;q=0.8, zstd;q=0.7",
|
|
|
|
expEncoding: gzipName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "multi accept header list, prefer zstd",
|
|
|
|
acceptEncHeader: "gzip, br, zstd",
|
|
|
|
expEncoding: zstdName,
|
2022-11-15 09:56:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
if test.acceptEncHeader != "" {
|
|
|
|
req.Header.Add(acceptEncodingHeader, test.acceptEncHeader)
|
|
|
|
}
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = rw.Write(generateBytes(10))
|
|
|
|
})
|
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{MinResponseBodyBytes: 1}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expEncoding, rw.Header().Get(contentEncodingHeader))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 23:02:02 +00:00
|
|
|
func TestShouldCompressWhenNoContentEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
baseBody := generateBytes(gzhttp.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
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
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
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
2017-06-06 23:02:02 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
gr, err := gzip.NewReader(rw.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
got, err := io.ReadAll(gr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, got, baseBody)
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenContentEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2017-06-06 23:02:02 +00:00
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
fakeCompressedBody := generateBytes(gzhttp.DefaultMinSize)
|
2018-11-14 09:18:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2024-06-06 14:42:04 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipName)
|
2017-07-13 15:56:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
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
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-01-16 14:30:06 +00:00
|
|
|
func TestShouldNotCompressWhenNoAcceptEncodingHeader(t *testing.T) {
|
2017-07-13 15:56:01 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
fakeBody := generateBytes(gzhttp.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)
|
|
|
|
}
|
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
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
|
|
|
|
2024-01-16 14:30:06 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(varyHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
2022-11-15 09:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 09:26:05 +00:00
|
|
|
func TestShouldNotCompressWhenIdentityAcceptEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
req.Header.Set(acceptEncodingHeader, "identity")
|
|
|
|
|
|
|
|
fakeBody := generateBytes(gzhttp.DefaultMinSize)
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get(acceptEncodingHeader) != "identity" {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(varyHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCompressWhenEmptyAcceptEncodingHeader(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
|
|
|
req.Header.Set(acceptEncodingHeader, "")
|
|
|
|
|
|
|
|
fakeBody := generateBytes(gzhttp.DefaultMinSize)
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get(acceptEncodingHeader) != "" {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rw.Write(fakeBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(varyHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:56:08 +00:00
|
|
|
func TestShouldNotCompressHeadRequest(t *testing.T) {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodHead, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2022-11-15 09:56:08 +00:00
|
|
|
|
|
|
|
fakeBody := generateBytes(gzhttp.DefaultMinSize)
|
|
|
|
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, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
2022-11-15 09:56:08 +00:00
|
|
|
assert.Empty(t, rw.Header().Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
func TestShouldNotCompressWhenSpecificContentType(t *testing.T) {
|
2021-07-19 08:22:14 +00:00
|
|
|
baseBody := generateBytes(gzhttp.DefaultMinSize)
|
2019-10-31 10:36:05 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2021-02-12 11:12:03 +00:00
|
|
|
desc string
|
|
|
|
conf dynamic.Compress
|
|
|
|
reqContentType string
|
|
|
|
respContentType string
|
2019-10-31 10:36:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-02-12 11:12:03 +00:00
|
|
|
desc: "Exclude Request Content-Type",
|
2019-10-31 10:36:05 +00:00
|
|
|
conf: dynamic.Compress{
|
|
|
|
ExcludedContentTypes: []string{"text/event-stream"},
|
|
|
|
},
|
|
|
|
reqContentType: "text/event-stream",
|
|
|
|
},
|
2021-02-12 11:12:03 +00:00
|
|
|
{
|
|
|
|
desc: "Exclude Response Content-Type",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
ExcludedContentTypes: []string{"text/event-stream"},
|
|
|
|
},
|
|
|
|
respContentType: "text/event-stream",
|
|
|
|
},
|
2019-10-31 10:36:05 +00:00
|
|
|
{
|
2024-01-17 10:32:06 +00:00
|
|
|
desc: "Include Response Content-Type",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
IncludedContentTypes: []string{"text/plain"},
|
|
|
|
},
|
|
|
|
respContentType: "text/html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Ignoring application/grpc with exclude option",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
ExcludedContentTypes: []string{"application/json"},
|
|
|
|
},
|
|
|
|
reqContentType: "application/grpc",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Ignoring application/grpc with include option",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
IncludedContentTypes: []string{"application/json"},
|
|
|
|
},
|
|
|
|
reqContentType: "application/grpc",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Ignoring application/grpc with no option",
|
2019-10-31 10:36:05 +00:00
|
|
|
conf: dynamic.Compress{},
|
|
|
|
reqContentType: "application/grpc",
|
|
|
|
},
|
|
|
|
}
|
2017-11-10 13:12:02 +00:00
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2019-10-31 10:36:05 +00:00
|
|
|
if test.reqContentType != "" {
|
|
|
|
req.Header.Add(contentTypeHeader, test.reqContentType)
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:12:03 +00:00
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if len(test.respContentType) > 0 {
|
|
|
|
rw.Header().Set(contentTypeHeader, test.respContentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-31 10:36:05 +00:00
|
|
|
handler, err := New(context.Background(), next, test.conf, "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(acceptEncodingHeader))
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), baseBody)
|
|
|
|
})
|
|
|
|
}
|
2017-11-10 13:12:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 10:32:06 +00:00
|
|
|
func TestShouldCompressWhenSpecificContentType(t *testing.T) {
|
|
|
|
baseBody := generateBytes(gzhttp.DefaultMinSize)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
conf dynamic.Compress
|
|
|
|
respContentType string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Include Response Content-Type",
|
|
|
|
conf: dynamic.Compress{
|
|
|
|
IncludedContentTypes: []string{"text/html"},
|
|
|
|
},
|
|
|
|
respContentType: "text/html",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2024-01-17 10:32:06 +00:00
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header().Set(contentTypeHeader, test.respContentType)
|
|
|
|
|
|
|
|
if _, err := rw.Write(baseBody); err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
handler, err := New(context.Background(), next, test.conf, "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
2024-01-17 10:32:06 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, rw.Header().Get(varyHeader))
|
|
|
|
assert.NotEqualValues(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) {
|
2024-06-06 14:42:04 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipName)
|
2017-08-21 09:10:03 +00:00
|
|
|
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) {
|
2024-06-06 14:42:04 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipName)
|
2017-08-21 09:10:03 +00:00
|
|
|
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) {
|
2021-07-19 08:22:14 +00:00
|
|
|
compress, err := New(context.Background(), test.handler, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
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)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2017-08-21 09:10:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2017-08-21 09:10:03 +00:00
|
|
|
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) {
|
2024-06-06 14:42:04 +00:00
|
|
|
rw.Header().Add(contentEncodingHeader, gzipName)
|
2017-11-08 23:48:03 +00:00
|
|
|
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
|
|
|
})
|
2021-07-19 08:22:14 +00:00
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
ts := httptest.NewServer(handler)
|
2017-11-08 23:48:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2017-11-08 23:48:03 +00:00
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
2017-11-08 23:48:03 +00:00
|
|
|
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) {
|
2021-07-19 08:22:14 +00:00
|
|
|
compress, err := New(context.Background(), test.handler, dynamic.Compress{}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
ts := httptest.NewServer(compress)
|
2017-08-21 09:10:03 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, ts.URL, nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2017-08-21 09:10:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, resp.Header.Get(contentEncodingHeader))
|
2017-08-21 09:10:03 +00:00
|
|
|
assert.Equal(t, acceptEncodingHeader, resp.Header.Get(varyHeader))
|
2017-07-13 15:56:01 +00:00
|
|
|
|
2021-03-04 19:08:03 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2017-08-21 09:10:03 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-20 16:00:08 +00:00
|
|
|
func TestMinResponseBodyBytes(t *testing.T) {
|
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
minResponseBodyBytes int
|
|
|
|
expectedCompression bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "should compress",
|
|
|
|
expectedCompression: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should not compress",
|
|
|
|
minResponseBodyBytes: 100001,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2021-09-20 16:00:08 +00:00
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, err := rw.Write(fakeBody); err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
handler, err := New(context.Background(), next, dynamic.Compress{MinResponseBodyBytes: test.minResponseBodyBytes}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
if test.expectedCompression {
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, rw.Header().Get(contentEncodingHeader))
|
2021-09-20 16:00:08 +00:00
|
|
|
assert.NotEqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
|
|
|
|
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:42:44 +00:00
|
|
|
// This test is an adapted version of net/http/httputil.Test1xxResponses test.
|
|
|
|
func Test1xxResponses(t *testing.T) {
|
|
|
|
fakeBody := generateBytes(100000)
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h := w.Header()
|
|
|
|
h.Add("Link", "</style.css>; rel=preload; as=style")
|
|
|
|
h.Add("Link", "</script.js>; rel=preload; as=script")
|
|
|
|
w.WriteHeader(http.StatusEarlyHints)
|
|
|
|
|
|
|
|
h.Add("Link", "</foo.js>; rel=preload; as=script")
|
|
|
|
w.WriteHeader(http.StatusProcessing)
|
|
|
|
|
|
|
|
if _, err := w.Write(fakeBody); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
compress, err := New(context.Background(), next, dynamic.Compress{MinResponseBodyBytes: 1024}, "testing")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
server := httptest.NewServer(compress)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
frontendClient := server.Client()
|
|
|
|
|
|
|
|
checkLinkHeaders := func(t *testing.T, expected, got []string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if len(expected) != len(got) {
|
|
|
|
t.Errorf("Expected %d link headers; got %d", len(expected), len(got))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range expected {
|
|
|
|
if i >= len(got) {
|
|
|
|
t.Errorf("Expected %q link header; got nothing", expected[i])
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected[i] != got[i] {
|
|
|
|
t.Errorf("Expected %q link header; got %q", expected[i], got[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var respCounter uint8
|
|
|
|
trace := &httptrace.ClientTrace{
|
|
|
|
Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
|
|
|
|
switch code {
|
|
|
|
case http.StatusEarlyHints:
|
|
|
|
checkLinkHeaders(t, []string{"</style.css>; rel=preload; as=style", "</script.js>; rel=preload; as=script"}, header["Link"])
|
|
|
|
case http.StatusProcessing:
|
|
|
|
checkLinkHeaders(t, []string{"</style.css>; rel=preload; as=style", "</script.js>; rel=preload; as=script", "</foo.js>; rel=preload; as=script"}, header["Link"])
|
|
|
|
default:
|
|
|
|
t.Error("Unexpected 1xx response")
|
|
|
|
}
|
|
|
|
|
|
|
|
respCounter++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequestWithContext(httptrace.WithClientTrace(context.Background(), trace), http.MethodGet, server.URL, nil)
|
2024-06-06 14:42:04 +00:00
|
|
|
req.Header.Add(acceptEncodingHeader, gzipName)
|
2023-06-14 15:42:44 +00:00
|
|
|
|
|
|
|
res, err := frontendClient.Do(req)
|
2023-11-17 00:50:06 +00:00
|
|
|
assert.NoError(t, err)
|
2023-06-14 15:42:44 +00:00
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if respCounter != 2 {
|
|
|
|
t.Errorf("Expected 2 1xx responses; got %d", respCounter)
|
|
|
|
}
|
|
|
|
checkLinkHeaders(t, []string{"</style.css>; rel=preload; as=style", "</script.js>; rel=preload; as=script", "</foo.js>; rel=preload; as=script"}, res.Header["Link"])
|
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(t, gzipName, res.Header.Get(contentEncodingHeader))
|
2023-06-14 15:42:44 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
|
|
|
assert.NotEqualValues(t, body, fakeBody)
|
|
|
|
}
|
|
|
|
|
2021-07-19 08:22:14 +00:00
|
|
|
func BenchmarkCompress(b *testing.B) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
parallel bool
|
|
|
|
size int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "2k",
|
|
|
|
size: 2048,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "20k",
|
|
|
|
size: 20480,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "100k",
|
|
|
|
size: 102400,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 2048,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "20k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 20480,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "100k parallel",
|
|
|
|
parallel: true,
|
|
|
|
size: 102400,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
b.Run(test.name, func(b *testing.B) {
|
|
|
|
baseBody := generateBytes(test.size)
|
|
|
|
|
|
|
|
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := rw.Write(baseBody)
|
|
|
|
assert.NoError(b, err)
|
|
|
|
})
|
|
|
|
handler, _ := New(context.Background(), next, dynamic.Compress{}, "testing")
|
|
|
|
|
2022-08-09 15:36:08 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "/whatever", nil)
|
2021-07-19 08:22:14 +00:00
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(test.size))
|
|
|
|
if test.parallel {
|
|
|
|
b.ResetTimer()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
runBenchmark(b, req, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2024-02-19 14:44:03 +00:00
|
|
|
for range b.N {
|
2021-07-19 08:22:14 +00:00
|
|
|
runBenchmark(b, req, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBenchmark(b *testing.B, req *http.Request, handler http.Handler) {
|
|
|
|
b.Helper()
|
|
|
|
|
|
|
|
res := httptest.NewRecorder()
|
|
|
|
handler.ServeHTTP(res, req)
|
|
|
|
if code := res.Code; code != 200 {
|
|
|
|
b.Fatalf("Expected 200 but got %d", code)
|
|
|
|
}
|
|
|
|
|
2024-06-06 14:42:04 +00:00
|
|
|
assert.Equal(b, gzipName, res.Header().Get(contentEncodingHeader))
|
2021-07-19 08:22:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 08:00:03 +00:00
|
|
|
func generateBytes(length int) []byte {
|
2017-06-06 23:02:02 +00:00
|
|
|
var value []byte
|
2024-02-19 14:44:03 +00:00
|
|
|
for i := range length {
|
2017-07-13 15:56:01 +00:00
|
|
|
value = append(value, 0x61+byte(i))
|
2017-06-06 23:02:02 +00:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|