Apply content type exclusion on response

Co-authored-by: kevinpollet <pollet.kevin@gmail.com>
This commit is contained in:
Jean-Baptiste Doumenjou 2021-02-12 12:12:03 +01:00 committed by GitHub
parent 0937cba870
commit 951d61bfcd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 22 deletions

View file

@ -7,9 +7,9 @@ import (
"net/http/httptest"
"testing"
"github.com/NYTimes/gziphandler"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/traefik/gziphandler"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
"github.com/traefik/traefik/v2/pkg/testhelpers"
)
@ -91,25 +91,26 @@ func TestShouldNotCompressWhenNoAcceptEncodingHeader(t *testing.T) {
func TestShouldNotCompressWhenSpecificContentType(t *testing.T) {
baseBody := generateBytes(gziphandler.DefaultMinSize)
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
_, err := rw.Write(baseBody)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
})
testCases := []struct {
desc string
conf dynamic.Compress
reqContentType string
desc string
conf dynamic.Compress
reqContentType string
respContentType string
}{
{
desc: "text/event-stream",
desc: "Exclude Request Content-Type",
conf: dynamic.Compress{
ExcludedContentTypes: []string{"text/event-stream"},
},
reqContentType: "text/event-stream",
},
{
desc: "Exclude Response Content-Type",
conf: dynamic.Compress{
ExcludedContentTypes: []string{"text/event-stream"},
},
respContentType: "text/event-stream",
},
{
desc: "application/grpc",
conf: dynamic.Compress{},
@ -128,6 +129,17 @@ func TestShouldNotCompressWhenSpecificContentType(t *testing.T) {
req.Header.Add(contentTypeHeader, test.reqContentType)
}
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)
}
})
handler, err := New(context.Background(), next, test.conf, "test")
require.NoError(t, err)