forked from gin-contrib/gzip
/
gzip_test.go
339 lines (277 loc) · 10.1 KB
/
gzip_test.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package gzip
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/klauspost/compress/gzip"
"github.com/stretchr/testify/assert"
)
const (
testResponse = "Gzip Test Response "
testReverseResponse = "Gzip Test Reverse Response "
)
type rServer struct{}
func (s *rServer) ServeHTTP(rw http.ResponseWriter, _ *http.Request) {
fmt.Fprint(rw, testReverseResponse)
}
type closeNotifyingRecorder struct {
*httptest.ResponseRecorder
closed chan bool
}
func newCloseNotifyingRecorder() *closeNotifyingRecorder {
return &closeNotifyingRecorder{
httptest.NewRecorder(),
make(chan bool, 1),
}
}
func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
return c.closed
}
func newServer() *gin.Engine {
// init reverse proxy server
rServer := httptest.NewServer(new(rServer))
target, _ := url.Parse(rServer.URL)
rp := httputil.NewSingleHostReverseProxy(target)
router := gin.New()
router.Use(Gzip(DefaultCompression))
router.GET("/", func(c *gin.Context) {
c.Header("Content-Length", strconv.Itoa(len(testResponse)))
c.String(200, testResponse)
})
router.Any("/reverse", func(c *gin.Context) {
rp.ServeHTTP(c.Writer, c.Request)
})
return router
}
func TestGzip(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
req.Header.Add("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
r := newServer()
r.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
assert.NotEqual(t, w.Body.Len(), 19)
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
gr, err := gzip.NewReader(w.Body)
assert.NoError(t, err)
defer gr.Close()
body, _ := ioutil.ReadAll(gr)
assert.Equal(t, string(body), testResponse)
}
func TestGzipPNG(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/image.png", nil)
req.Header.Add("Accept-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression))
router.GET("/image.png", func(c *gin.Context) {
c.String(200, "this is a PNG!")
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Header().Get("Vary"), "")
assert.Equal(t, w.Body.String(), "this is a PNG!")
}
func TestMatchSupportedRequests(t *testing.T) {
router := gin.New()
router.Use(
Gzip(DefaultCompression,
WithMatchSupportedRequestFn(func(req *http.Request) (bool, bool) {
xheader := req.Header.Get("X-Test-Header")
if xheader == "" {
return false, false
}
ok, supported := strings.HasPrefix(xheader, "+"), strings.HasSuffix(xheader, "compress me")
return ok, supported
}),
// For testing the precedence order
WithExcludedExtensions([]string{".php"}),
WithExcludedPaths([]string{"/api/"}),
))
router.GET("/index.html", func(c *gin.Context) {
c.String(200, "this is a HTML!")
})
t.Run("Is Compressed/matched header's value", func(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
req.Header.Add("Accept-Encoding", "gzip")
req.Header.Add("X-Test-Header", "+compress me")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
assert.NotEqual(t, w.Body.Len(), 19)
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
})
t.Run("Is Compressed/no header", func(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
req.Header.Add("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
assert.NotEqual(t, w.Body.Len(), 19)
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
})
t.Run("Is Not Compressed/no match", func(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
req.Header.Add("Accept-Encoding", "gzip")
req.Header.Add("X-Test-Header", "+skip me")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "this is a HTML!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
})
t.Run("Is Not Compressed/Precedence over Exclusion rules", func(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
req.Header.Add("Accept-Encoding", "gzip")
req.Header.Add("X-Test-Header", "+compressme")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "this is a HTML!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
})
}
func TestExcludedExtensions(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/index.html", nil)
req.Header.Add("Accept-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression, WithExcludedExtensions([]string{".html"})))
router.GET("/index.html", func(c *gin.Context) {
c.String(200, "this is a HTML!")
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "this is a HTML!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
func TestExcludedPaths(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/api/books", nil)
req.Header.Add("Accept-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression, WithExcludedPaths([]string{"/api/"})))
router.GET("/api/books", func(c *gin.Context) {
c.String(200, "this is books!")
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "this is books!", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
func TestNoGzip(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
w := httptest.NewRecorder()
r := newServer()
r.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "")
assert.Equal(t, w.Header().Get("Content-Length"), "19")
assert.Equal(t, w.Body.String(), testResponse)
}
func TestGzipWithReverseProxy(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "GET", "/reverse", nil)
req.Header.Add("Accept-Encoding", "gzip")
w := newCloseNotifyingRecorder()
r := newServer()
r.ServeHTTP(w, req)
assert.Equal(t, w.Code, 200)
assert.Equal(t, w.Header().Get("Content-Encoding"), "gzip")
assert.Equal(t, w.Header().Get("Vary"), "Accept-Encoding")
assert.NotEqual(t, w.Header().Get("Content-Length"), "0")
assert.NotEqual(t, w.Body.Len(), 19)
assert.Equal(t, fmt.Sprint(w.Body.Len()), w.Header().Get("Content-Length"))
gr, err := gzip.NewReader(w.Body)
assert.NoError(t, err)
defer gr.Close()
body, _ := ioutil.ReadAll(gr)
assert.Equal(t, string(body), testReverseResponse)
}
func TestDecompressGzip(t *testing.T) {
buf := &bytes.Buffer{}
gz, _ := gzip.NewWriterLevel(buf, gzip.DefaultCompression)
if _, err := gz.Write([]byte(testResponse)); err != nil {
gz.Close()
t.Fatal(err)
}
gz.Close()
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", buf)
req.Header.Add("Content-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
if v := c.Request.Header.Get("Content-Encoding"); v != "" {
t.Errorf("unexpected `Content-Encoding`: %s header", v)
}
if v := c.Request.Header.Get("Content-Length"); v != "" {
t.Errorf("unexpected `Content-Length`: %s header", v)
}
data, err := c.GetRawData()
if err != nil {
t.Fatal(err)
}
c.Data(200, "text/plain", data)
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, testResponse, w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
func TestDecompressGzipWithEmptyBody(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", nil)
req.Header.Add("Content-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
c.String(200, "ok")
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "", w.Header().Get("Content-Encoding"))
assert.Equal(t, "", w.Header().Get("Vary"))
assert.Equal(t, "ok", w.Body.String())
assert.Equal(t, "", w.Header().Get("Content-Length"))
}
func TestDecompressGzipWithIncorrectData(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), "POST", "/", bytes.NewReader([]byte(testResponse)))
req.Header.Add("Content-Encoding", "gzip")
router := gin.New()
router.Use(Gzip(DefaultCompression, WithDecompressFn(DefaultDecompressHandle)))
router.POST("/", func(c *gin.Context) {
c.String(200, "ok")
})
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
}