1
0
Fork 0

Implement customizable minimum body size for compress middleware

This commit is contained in:
Lukas Schulte Pelkum 2021-09-20 18:00:08 +02:00 committed by GitHub
parent 8f0832d340
commit 07a3c37a23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 134 additions and 9 deletions

View file

@ -23,6 +23,7 @@ type compress struct {
next http.Handler
name string
excludes []string
minSize int
}
// New creates a new compress middleware.
@ -39,7 +40,12 @@ func New(ctx context.Context, next http.Handler, conf dynamic.Compress, name str
excludes = append(excludes, mediaType)
}
return &compress{next: next, name: name, excludes: excludes}, nil
minSize := gzhttp.DefaultMinSize
if conf.MinResponseBodyBytes > 0 {
minSize = conf.MinResponseBodyBytes
}
return &compress{next: next, name: name, excludes: excludes, minSize: minSize}, nil
}
func (c *compress) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
@ -64,7 +70,7 @@ func (c *compress) gzipHandler(ctx context.Context) http.Handler {
wrapper, err := gzhttp.NewWrapper(
gzhttp.ExceptContentTypes(c.excludes),
gzhttp.CompressionLevel(gzip.DefaultCompression),
gzhttp.MinSize(gzhttp.DefaultMinSize))
gzhttp.MinSize(c.minSize))
if err != nil {
log.FromContext(ctx).Error(err)
}

View file

@ -305,6 +305,57 @@ func TestIntegrationShouldCompress(t *testing.T) {
}
}
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 {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
req.Header.Add(acceptEncodingHeader, gzipValue)
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 {
assert.Equal(t, gzipValue, rw.Header().Get(contentEncodingHeader))
assert.NotEqualValues(t, rw.Body.Bytes(), fakeBody)
return
}
assert.Empty(t, rw.Header().Get(contentEncodingHeader))
assert.EqualValues(t, rw.Body.Bytes(), fakeBody)
})
}
}
func BenchmarkCompress(b *testing.B) {
testCases := []struct {
name string