summaryrefslogtreecommitdiffstats
path: root/modules/gzip/gzip_test.go
blob: 5fc56cc7f0b4b22cc589e236951b51423ca78d36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package gzip

import (
	"archive/zip"
	"bytes"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	"gitea.com/macaron/macaron"
	gzipp "github.com/klauspost/compress/gzip"
	"github.com/stretchr/testify/assert"
)

func setup(sampleResponse []byte) (*macaron.Macaron, *[]byte) {
	m := macaron.New()
	m.Use(Middleware())
	m.Get("/", func() *[]byte { return &sampleResponse })
	return m, &sampleResponse
}

func reqNoAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte) {
	// Request without accept gzip: Should not gzip
	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/", nil)
	assert.NoError(t, err)
	m.ServeHTTP(resp, req)

	_, ok := resp.HeaderMap[contentEncodingHeader]
	assert.False(t, ok)

	contentEncoding := resp.Header().Get(contentEncodingHeader)
	assert.NotContains(t, contentEncoding, "gzip")

	result := resp.Body.Bytes()
	assert.Equal(t, *sampleResponse, result)
}

func reqAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte, expectGzip bool) {
	// Request without accept gzip: Should not gzip
	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/", nil)
	assert.NoError(t, err)
	req.Header.Set(acceptEncodingHeader, "gzip")
	m.ServeHTTP(resp, req)

	_, ok := resp.HeaderMap[contentEncodingHeader]
	assert.Equal(t, ok, expectGzip)

	contentEncoding := resp.Header().Get(contentEncodingHeader)
	if expectGzip {
		assert.Contains(t, contentEncoding, "gzip")
		gzippReader, err := gzipp.NewReader(resp.Body)
		assert.NoError(t, err)
		result, err := ioutil.ReadAll(gzippReader)
		assert.NoError(t, err)
		assert.Equal(t, *sampleResponse, result)
	} else {
		assert.NotContains(t, contentEncoding, "gzip")
		result := resp.Body.Bytes()
		assert.Equal(t, *sampleResponse, result)
	}
}

func TestMiddlewareSmall(t *testing.T) {
	m, sampleResponse := setup([]byte("Small response"))

	reqNoAcceptGzip(t, m, sampleResponse)

	reqAcceptGzip(t, m, sampleResponse, false)
}

func TestMiddlewareLarge(t *testing.T) {
	b := make([]byte, MinSize+1)
	for i := range b {
		b[i] = byte(i % 256)
	}
	m, sampleResponse := setup(b)

	reqNoAcceptGzip(t, m, sampleResponse)

	// This should be gzipped as we accept gzip
	reqAcceptGzip(t, m, sampleResponse, true)
}

func TestMiddlewareGzip(t *testing.T) {
	b := make([]byte, MinSize*10)
	for i := range b {
		b[i] = byte(i % 256)
	}
	outputBuffer := bytes.NewBuffer([]byte{})
	gzippWriter := gzipp.NewWriter(outputBuffer)
	gzippWriter.Write(b)
	gzippWriter.Flush()
	gzippWriter.Close()
	output := outputBuffer.Bytes()

	m, sampleResponse := setup(output)

	reqNoAcceptGzip(t, m, sampleResponse)

	// This should not be gzipped even though we accept gzip
	reqAcceptGzip(t, m, sampleResponse, false)
}

func TestMiddlewareZip(t *testing.T) {
	b := make([]byte, MinSize*10)
	for i := range b {
		b[i] = byte(i % 256)
	}
	outputBuffer := bytes.NewBuffer([]byte{})
	zipWriter := zip.NewWriter(outputBuffer)
	fileWriter, err := zipWriter.Create("default")
	assert.NoError(t, err)
	fileWriter.Write(b)
	//fileWriter.Close()
	zipWriter.Close()
	output := outputBuffer.Bytes()

	m, sampleResponse := setup(output)

	reqNoAcceptGzip(t, m, sampleResponse)

	// This should not be gzipped even though we accept gzip
	reqAcceptGzip(t, m, sampleResponse, false)
}