diff options
author | John Papandriopoulos <jpap@users.noreply.gitlab.com> | 2017-04-26 01:40:27 +0300 |
---|---|---|
committer | John Papandriopoulos <jpap@users.noreply.gitlab.com> | 2017-04-26 02:02:11 +0300 |
commit | 861eb7ea410b20611fd1342d3921d4b339f3b21e (patch) | |
tree | 5dbc806a24a80219f55a3ef8bbcac42adb781c22 /domain_test.go | |
parent | 043be6d27f1c01010e86d5f30c2006eff254fc03 (diff) |
Support for statically compressed gzip content-encoding
Diffstat (limited to 'domain_test.go')
-rw-r--r-- | domain_test.go | 81 |
1 files changed, 79 insertions, 2 deletions
diff --git a/domain_test.go b/domain_test.go index 7298c22b..b4879ee0 100644 --- a/domain_test.go +++ b/domain_test.go @@ -1,13 +1,16 @@ package main import ( - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "compress/gzip" + "io/ioutil" "mime" "net/http" "net/http/httptest" "net/url" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGroupServeHTTP(t *testing.T) { @@ -51,6 +54,80 @@ func TestDomainServeHTTP(t *testing.T) { assert.HTTPError(t, testDomain.ServeHTTP, "GET", "/not-existing-file", nil) } +func testHTTPGzip(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, acceptEncoding string, str interface{}, ungzip bool) { + w := httptest.NewRecorder() + req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil) + require.NoError(t, err) + if acceptEncoding != "" { + req.Header.Add("Accept-Encoding", acceptEncoding) + } + handler(w, req) + + if ungzip { + reader, err := gzip.NewReader(w.Body) + require.NoError(t, err) + defer reader.Close() + + contentEncoding := w.Header().Get("Content-Encoding") + assert.Equal(t, "gzip", contentEncoding, "Content-Encoding") + + bytes, err := ioutil.ReadAll(reader) + require.NoError(t, err) + assert.Contains(t, string(bytes), str) + } else { + assert.Contains(t, w.Body.String(), str) + } +} + +func TestGroupServeHTTPGzip(t *testing.T) { + setUpTests() + + testGroup := &domain{ + Group: "group", + Project: "", + } + + testSet := []struct { + mode string // HTTP mode + url string // Test URL + params url.Values // Test URL params + acceptEncoding string // Accept encoding header + body interface{} // Expected body at above URL + ungzip bool // Do we expect the request to require unzip? + }{ + // No gzip encoding requested + {"GET", "http://group.test.io/", nil, "", "main-dir", false}, + {"GET", "http://group.test.io/", nil, "identity", "main-dir", false}, + {"GET", "http://group.test.io/", nil, "gzip; q=0", "main-dir", false}, + // gzip encoding requeste}, + {"GET", "http://group.test.io/", nil, "*", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "identity, gzip", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip; q=1", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip; q=0.9", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip, deflate", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip; q=1, deflate", "main-dir", true}, + {"GET", "http://group.test.io/", nil, "gzip; q=0.9, deflate", "main-dir", true}, + // gzip encoding requested, but url does not have compressed content on disk + {"GET", "http://group.test.io/project2/", nil, "*", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "identity, gzip", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip; q=1", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip; q=0.9", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip, deflate", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip; q=1, deflate", "project2-main", false}, + {"GET", "http://group.test.io/project2/", nil, "gzip; q=0.9, deflate", "project2-main", false}, + // malformed headers + {"GET", "http://group.test.io/", nil, ";; gzip", "main-dir", false}, + {"GET", "http://group.test.io/", nil, "middle-out", "main-dir", false}, + {"GET", "http://group.test.io/", nil, "gzip; quality=1", "main-dir", false}, + } + + for _, tt := range testSet { + testHTTPGzip(t, testGroup.ServeHTTP, tt.mode, tt.url, tt.params, tt.acceptEncoding, tt.body, tt.ungzip) + } +} + func testHTTP404(t *testing.T, handler http.HandlerFunc, mode, url string, values url.Values, str interface{}) { w := httptest.NewRecorder() req, err := http.NewRequest(mode, url+"?"+values.Encode(), nil) |