From 96eab1202717e073782ec399a4e0820cae15b1bb Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 17 Aug 2017 17:19:06 -0700 Subject: Updating server dependancies. (#7246) --- vendor/github.com/NYTimes/gziphandler/gzip_test.go | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'vendor/github.com/NYTimes/gziphandler/gzip_test.go') 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) } -- cgit v1.2.3-1-g7c22