summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/NYTimes/gziphandler/gzip_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/NYTimes/gziphandler/gzip_test.go')
-rw-r--r--vendor/github.com/NYTimes/gziphandler/gzip_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/github.com/NYTimes/gziphandler/gzip_test.go b/vendor/github.com/NYTimes/gziphandler/gzip_test.go
index 0f6488cae..d5a7eb82b 100644
--- a/vendor/github.com/NYTimes/gziphandler/gzip_test.go
+++ b/vendor/github.com/NYTimes/gziphandler/gzip_test.go
@@ -319,6 +319,66 @@ func TestDontWriteWhenNotWrittenTo(t *testing.T) {
}
}
+var contentTypeTests = []struct {
+ name string
+ contentType string
+ acceptedContentTypes []string
+ expectedGzip bool
+}{
+ {
+ name: "Always gzip when content types are empty",
+ contentType: "",
+ acceptedContentTypes: []string{},
+ expectedGzip: true,
+ },
+ {
+ name: "Exact content-type match",
+ contentType: "application/json",
+ acceptedContentTypes: []string{"application/json"},
+ expectedGzip: true,
+ },
+ {
+ name: "Case insensitive content-type matching",
+ contentType: "Application/Json",
+ acceptedContentTypes: []string{"application/json"},
+ expectedGzip: true,
+ },
+ {
+ name: "Non-matching content-type",
+ contentType: "text/xml",
+ acceptedContentTypes: []string{"application/json"},
+ expectedGzip: false,
+ },
+}
+
+func TestContentTypes(t *testing.T) {
+ for _, tt := range contentTypeTests {
+ handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ w.Header().Set("Content-Type", tt.contentType)
+ io.WriteString(w, testBody)
+ })
+
+ wrapper, err := GzipHandlerWithOpts(ContentTypes(tt.acceptedContentTypes))
+ if !assert.Nil(t, err, "NewGzipHandlerWithOpts returned error", tt.name) {
+ continue
+ }
+
+ req, _ := http.NewRequest("GET", "/whatever", nil)
+ req.Header.Set("Accept-Encoding", "gzip")
+ resp := httptest.NewRecorder()
+ wrapper(handler).ServeHTTP(resp, req)
+ res := resp.Result()
+
+ assert.Equal(t, 200, res.StatusCode)
+ if tt.expectedGzip {
+ assert.Equal(t, "gzip", res.Header.Get("Content-Encoding"), tt.name)
+ } else {
+ assert.NotEqual(t, "gzip", res.Header.Get("Content-Encoding"), tt.name)
+ }
+ }
+}
+
// --------------------------------------------------------------------
func BenchmarkGzipHandler_S2k(b *testing.B) { benchmark(b, false, 2048) }