-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecompress_test.go
208 lines (164 loc) · 5.11 KB
/
decompress_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
package compress_test
import (
"bytes"
"fmt"
"github.com/andybalholm/brotli"
"github.com/aurowora/compress"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zlib"
"github.com/klauspost/compress/zstd"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
/*
gin-compress Copyright (C) 2022 Aurora McGinnis
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// disable all algos
var dcOpts = []compress.CompressOption{
compress.WithAlgo("gzip", false),
compress.WithAlgo("br", false),
compress.WithAlgo("zstd", false),
compress.WithAlgo("deflate", false),
}
var lol = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH"
var lolLarge = strings.Repeat("A", 600*1024)
func TestDecompressBrotli(t *testing.T) {
r := setupRouter(dcOpts...)
b := bytes.NewBuffer(nil)
bc := brotli.NewWriter(b)
_, err := bc.Write([]byte(lol))
assert.NoError(t, err)
err = bc.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "br")
r.ServeHTTP(w, req)
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
assert.Equal(t, lol, w.Body.String())
}
func TestDecompressGzip(t *testing.T) {
r := setupRouter(dcOpts...)
b := bytes.NewBuffer(nil)
gz := gzip.NewWriter(b)
_, err := gz.Write([]byte(lol))
assert.NoError(t, err)
err = gz.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "gzip")
r.ServeHTTP(w, req)
assert.Equal(t, "", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
assert.Equal(t, lol, w.Body.String())
}
func TestDecompressZstd(t *testing.T) {
r := setupRouter(dcOpts...)
b := bytes.NewBuffer(nil)
z, err := zstd.NewWriter(b)
assert.NoError(t, err)
_, err = z.Write([]byte(lol))
assert.NoError(t, err)
err = z.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "zstd")
r.ServeHTTP(w, req)
assert.Equal(t, "", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
assert.Equal(t, lol, w.Body.String())
}
func TestDecompressDeflate(t *testing.T) {
r := setupRouter(dcOpts...)
b := bytes.NewBuffer(nil)
z := zlib.NewWriter(b)
_, err := z.Write([]byte(lol))
assert.NoError(t, err)
err = z.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "deflate")
r.ServeHTTP(w, req)
assert.Equal(t, "", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
assert.Equal(t, lol, w.Body.String())
}
func TestMultipleDecompressions1(t *testing.T) {
// do not alter the limit
r := setupRouter(dcOpts...)
b := bytes.NewBuffer(nil)
z := zlib.NewWriter(b)
g := gzip.NewWriter(z)
_, err := g.Write([]byte(lolLarge))
assert.NoError(t, err)
err = g.Close()
assert.NoError(t, err)
err = z.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "gzip, deflate")
r.ServeHTTP(w, req)
assert.Equal(t, "gzip", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
}
func TestMultipleDecompressions2(t *testing.T) {
var sDcOpts = []compress.CompressOption{
compress.WithAlgo("gzip", false),
compress.WithAlgo("br", false),
compress.WithAlgo("zstd", false),
compress.WithAlgo("deflate", false),
compress.WithMaxDecodeSteps(2),
}
r := setupRouter(sDcOpts...)
b := bytes.NewBuffer(nil)
z := zlib.NewWriter(b)
g := gzip.NewWriter(z)
_, err := g.Write([]byte(lolLarge))
assert.NoError(t, err)
err = g.Close()
assert.NoError(t, err)
err = z.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "gzip, deflate")
r.ServeHTTP(w, req)
assert.Equal(t, "", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
assert.Equal(t, lolLarge, w.Body.String())
}
func TestMultipleDecompressions3(t *testing.T) {
var sDcOpts = []compress.CompressOption{
compress.WithAlgo("gzip", false),
compress.WithAlgo("br", false),
compress.WithAlgo("zstd", false),
compress.WithAlgo("deflate", false),
compress.WithMaxDecodeSteps(4),
}
r := setupRouter(sDcOpts...)
b := bytes.NewBuffer(nil)
z := zlib.NewWriter(b)
g := gzip.NewWriter(z)
_, err := g.Write([]byte(lolLarge))
assert.NoError(t, err)
err = g.Close()
assert.NoError(t, err)
err = z.Close()
assert.NoError(t, err)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/echo", b)
req.Header.Set("Content-Encoding", "gzipButDifferentLol, deflate")
r.ServeHTTP(w, req)
assert.Equal(t, "gzipButDifferentLol", w.Header().Get("X-Request-Content-Encoding"))
assert.Equal(t, "200", fmt.Sprintf("%v", w.Code))
}