|
| 1 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gzip |
| 6 | + |
| 7 | +import ( |
| 8 | + "archive/zip" |
| 9 | + "bytes" |
| 10 | + "io/ioutil" |
| 11 | + "net/http" |
| 12 | + "net/http/httptest" |
| 13 | + "testing" |
| 14 | + |
| 15 | + gzipp "github.com/klauspost/compress/gzip" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + macaron "gopkg.in/macaron.v1" |
| 18 | +) |
| 19 | + |
| 20 | +func setup(sampleResponse []byte) (*macaron.Macaron, *[]byte) { |
| 21 | + m := macaron.New() |
| 22 | + m.Use(Middleware()) |
| 23 | + m.Get("/", func() *[]byte { return &sampleResponse }) |
| 24 | + return m, &sampleResponse |
| 25 | +} |
| 26 | + |
| 27 | +func reqNoAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte) { |
| 28 | + // Request without accept gzip: Should not gzip |
| 29 | + resp := httptest.NewRecorder() |
| 30 | + req, err := http.NewRequest("GET", "/", nil) |
| 31 | + assert.NoError(t, err) |
| 32 | + m.ServeHTTP(resp, req) |
| 33 | + |
| 34 | + _, ok := resp.HeaderMap[contentEncodingHeader] |
| 35 | + assert.False(t, ok) |
| 36 | + |
| 37 | + contentEncoding := resp.Header().Get(contentEncodingHeader) |
| 38 | + assert.NotContains(t, contentEncoding, "gzip") |
| 39 | + |
| 40 | + result := resp.Body.Bytes() |
| 41 | + assert.Equal(t, *sampleResponse, result) |
| 42 | +} |
| 43 | + |
| 44 | +func reqAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte, expectGzip bool) { |
| 45 | + // Request without accept gzip: Should not gzip |
| 46 | + resp := httptest.NewRecorder() |
| 47 | + req, err := http.NewRequest("GET", "/", nil) |
| 48 | + assert.NoError(t, err) |
| 49 | + req.Header.Set(acceptEncodingHeader, "gzip") |
| 50 | + m.ServeHTTP(resp, req) |
| 51 | + |
| 52 | + _, ok := resp.HeaderMap[contentEncodingHeader] |
| 53 | + assert.Equal(t, ok, expectGzip) |
| 54 | + |
| 55 | + contentEncoding := resp.Header().Get(contentEncodingHeader) |
| 56 | + if expectGzip { |
| 57 | + assert.Contains(t, contentEncoding, "gzip") |
| 58 | + gzippReader, err := gzipp.NewReader(resp.Body) |
| 59 | + assert.NoError(t, err) |
| 60 | + result, err := ioutil.ReadAll(gzippReader) |
| 61 | + assert.NoError(t, err) |
| 62 | + assert.Equal(t, *sampleResponse, result) |
| 63 | + } else { |
| 64 | + assert.NotContains(t, contentEncoding, "gzip") |
| 65 | + result := resp.Body.Bytes() |
| 66 | + assert.Equal(t, *sampleResponse, result) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestMiddlewareSmall(t *testing.T) { |
| 71 | + m, sampleResponse := setup([]byte("Small response")) |
| 72 | + |
| 73 | + reqNoAcceptGzip(t, m, sampleResponse) |
| 74 | + |
| 75 | + reqAcceptGzip(t, m, sampleResponse, false) |
| 76 | +} |
| 77 | + |
| 78 | +func TestMiddlewareLarge(t *testing.T) { |
| 79 | + b := make([]byte, MinSize+1) |
| 80 | + for i := range b { |
| 81 | + b[i] = byte(i % 256) |
| 82 | + } |
| 83 | + m, sampleResponse := setup(b) |
| 84 | + |
| 85 | + reqNoAcceptGzip(t, m, sampleResponse) |
| 86 | + |
| 87 | + // This should be gzipped as we accept gzip |
| 88 | + reqAcceptGzip(t, m, sampleResponse, true) |
| 89 | +} |
| 90 | + |
| 91 | +func TestMiddlewareGzip(t *testing.T) { |
| 92 | + b := make([]byte, MinSize*10) |
| 93 | + for i := range b { |
| 94 | + b[i] = byte(i % 256) |
| 95 | + } |
| 96 | + outputBuffer := bytes.NewBuffer([]byte{}) |
| 97 | + gzippWriter := gzipp.NewWriter(outputBuffer) |
| 98 | + gzippWriter.Write(b) |
| 99 | + gzippWriter.Flush() |
| 100 | + gzippWriter.Close() |
| 101 | + output := outputBuffer.Bytes() |
| 102 | + |
| 103 | + m, sampleResponse := setup(output) |
| 104 | + |
| 105 | + reqNoAcceptGzip(t, m, sampleResponse) |
| 106 | + |
| 107 | + // This should not be gzipped even though we accept gzip |
| 108 | + reqAcceptGzip(t, m, sampleResponse, false) |
| 109 | +} |
| 110 | + |
| 111 | +func TestMiddlewareZip(t *testing.T) { |
| 112 | + b := make([]byte, MinSize*10) |
| 113 | + for i := range b { |
| 114 | + b[i] = byte(i % 256) |
| 115 | + } |
| 116 | + outputBuffer := bytes.NewBuffer([]byte{}) |
| 117 | + zipWriter := zip.NewWriter(outputBuffer) |
| 118 | + fileWriter, err := zipWriter.Create("default") |
| 119 | + assert.NoError(t, err) |
| 120 | + fileWriter.Write(b) |
| 121 | + //fileWriter.Close() |
| 122 | + zipWriter.Close() |
| 123 | + output := outputBuffer.Bytes() |
| 124 | + |
| 125 | + m, sampleResponse := setup(output) |
| 126 | + |
| 127 | + reqNoAcceptGzip(t, m, sampleResponse) |
| 128 | + |
| 129 | + // This should not be gzipped even though we accept gzip |
| 130 | + reqAcceptGzip(t, m, sampleResponse, false) |
| 131 | +} |
0 commit comments