-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression_test.go
More file actions
354 lines (315 loc) · 9.26 KB
/
Copy pathcompression_test.go
File metadata and controls
354 lines (315 loc) · 9.26 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package httpassert
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"net/http"
"testing"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
)
const payload = `{"status":"success"}`
func gzipped(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
zw := gzip.NewWriter(&b)
if _, err := zw.Write([]byte(s)); err != nil {
t.Fatalf("cannot gzip: %s", err)
}
if err := zw.Close(); err != nil {
t.Fatalf("cannot close the gzip writer: %s", err)
}
return b.Bytes()
}
func deflatedRaw(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
zw, err := flate.NewWriter(&b, flate.DefaultCompression)
if err != nil {
t.Fatalf("cannot build the flate writer: %s", err)
}
if _, err := zw.Write([]byte(s)); err != nil {
t.Fatalf("cannot deflate: %s", err)
}
if err := zw.Close(); err != nil {
t.Fatalf("cannot close the flate writer: %s", err)
}
return b.Bytes()
}
func deflatedZlib(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
zw := zlib.NewWriter(&b)
if _, err := zw.Write([]byte(s)); err != nil {
t.Fatalf("cannot zlib: %s", err)
}
if err := zw.Close(); err != nil {
t.Fatalf("cannot close the zlib writer: %s", err)
}
return b.Bytes()
}
func brotliEncoded(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
w := brotli.NewWriter(&b)
if _, err := w.Write([]byte(s)); err != nil {
t.Fatalf("cannot encode Brotli: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("cannot close the Brotli writer: %s", err)
}
return b.Bytes()
}
func zstdEncoded(t *testing.T, s string) []byte {
t.Helper()
var b bytes.Buffer
w, err := zstd.NewWriter(&b)
if err != nil {
t.Fatalf("cannot build the zstd writer: %s", err)
}
if _, err := w.Write([]byte(s)); err != nil {
t.Fatalf("cannot encode zstd: %s", err)
}
if err := w.Close(); err != nil {
t.Fatalf("cannot close the zstd writer: %s", err)
}
return b.Bytes()
}
// encoded builds the shape decodeBody operates on: a body and the header that
// claims how it was encoded. It reuses the shared response helper so the tests
// describe a Response consistently.
func encoded(enc string, body []byte) *Response {
h := http.Header{}
if enc != "" {
h.Set("Content-Encoding", enc)
}
r := response("200 OK", h, "")
r.BodyBytes = body
return &r
}
// Test_decodeBody is the whole of #27 in one table: what the assertions end up
// reading, for every way a body can arrive.
func Test_decodeBody(t *testing.T) {
t.Parallel()
tests := []struct {
Name string
Enc string
Body []byte
// Want is the payload the assertions should see. Only checked when
// WantErr is false.
Want string
// WantErr means the body could not be decoded, so the body assertions
// must refuse rather than match against what is there.
WantErr bool
// WantEncoding is what Content-Encoding is recorded as, verbatim.
WantEncoding string
}{
{
Name: "no encoding at all",
Body: []byte(payload),
Want: payload,
},
{
Name: "identity is not an encoding to remove",
Enc: "identity",
Body: []byte(payload),
Want: payload,
WantEncoding: "identity",
},
{
Name: "gzip",
Enc: "gzip",
Body: gzipped(t, payload),
Want: payload,
WantEncoding: "gzip",
},
{
// Content coding names are case-insensitive per RFC 9110, and a
// case-sensitive lookup would refuse a body it can decode.
Name: "GZIP, in the case the server chose",
Enc: "GZIP",
Body: gzipped(t, payload),
Want: payload,
WantEncoding: "GZIP",
},
{
Name: "gzip with surrounding whitespace",
Enc: " gzip ",
Body: gzipped(t, payload),
Want: payload,
WantEncoding: "gzip",
},
{
Name: "Brotli",
Enc: "br",
Body: brotliEncoded(t, payload),
Want: payload,
WantEncoding: "br",
},
{
Name: "zstd",
Enc: "zstd",
Body: zstdEncoded(t, payload),
Want: payload,
WantEncoding: "zstd",
},
{
// The two formats that share the deflate name. Neither reader
// accepts the other's input, so trying both is safe.
Name: "deflate, raw",
Enc: "deflate",
Body: deflatedRaw(t, payload),
Want: payload,
WantEncoding: "deflate",
},
{
Name: "deflate, zlib-wrapped",
Enc: "deflate",
Body: deflatedZlib(t, payload),
Want: payload,
WantEncoding: "deflate",
},
{
Name: "an encoding with no decoder here",
Enc: "compress",
Body: []byte("payload"),
WantErr: true,
WantEncoding: "compress",
},
{
// The header claims gzip and the bytes are not. Silently asserting
// against them is the bug; so is silently treating them as plain.
Name: "a body that does not match its stated encoding",
Enc: "gzip",
Body: []byte(payload),
WantErr: true,
WantEncoding: "gzip",
},
{
Name: "a truncated gzip stream",
Enc: "gzip",
Body: gzipped(t, payload)[:10],
WantErr: true,
WantEncoding: "gzip",
},
{
Name: "invalid Brotli",
Enc: "br",
Body: []byte{0xff},
WantErr: true,
WantEncoding: "br",
},
{
Name: "invalid zstd",
Enc: "zstd",
Body: []byte("not zstd"),
WantErr: true,
WantEncoding: "zstd",
},
{
Name: "truncated zlib-wrapped deflate",
Enc: "deflate",
Body: deflatedZlib(t, payload)[:10],
WantErr: true,
WantEncoding: "deflate",
},
{
// A 204 that carries the header anyway. There is nothing to decode,
// and an empty gzip stream is an error rather than an empty
// payload -- so --assert-body-empty must still pass.
Name: "an empty body with an encoding header",
Enc: "gzip",
Body: nil,
Want: "",
WantEncoding: "gzip",
},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
res := encoded(tc.Enc, tc.Body)
res.decodeBody()
if got, want := res.Encoding, tc.WantEncoding; got != want {
t.Errorf("Encoding = %q, want %q", got, want)
}
if tc.WantErr {
if res.DecodeErr == nil {
t.Fatalf("DecodeErr = nil, want an error; body reads %q", string(res.BodyBytes))
}
// The bytes are left exactly as they arrived, so the failure
// dump can still show what was really there.
if !bytes.Equal(res.BodyBytes, tc.Body) {
t.Errorf("BodyBytes was modified despite the decode failing")
}
return
}
if res.DecodeErr != nil {
t.Fatalf("DecodeErr = %s, want nil", res.DecodeErr)
}
if got := string(res.BodyBytes); got != tc.Want {
t.Errorf("BodyBytes = %q, want %q", got, tc.Want)
}
})
}
}
// Test_decodeBody_leavesHeadersAlone pins the half of the fix that is not about
// the body. net/http deletes Content-Encoding and Content-Length when it
// decodes, which makes a compressed response indistinguishable from one that
// never was -- and telling those apart is the reason to set Accept-Encoding by
// hand in the first place.
func Test_decodeBody_leavesHeadersAlone(t *testing.T) {
t.Parallel()
res := encoded("gzip", gzipped(t, payload))
res.Header.Set("Content-Length", "44")
res.decodeBody()
if got, want := res.Header.Get("Content-Encoding"), "gzip"; got != want {
t.Errorf("Content-Encoding = %q, want %q -- the header must survive decoding", got, want)
}
if got, want := res.Header.Get("Content-Length"), "44"; got != want {
t.Errorf("Content-Length = %q, want %q", got, want)
}
}
// Test_bodyOf covers the guard every body assertion goes through.
func Test_bodyOf(t *testing.T) {
t.Parallel()
t.Run("a decoded body is returned as-is", func(t *testing.T) {
res := encoded("gzip", gzipped(t, payload))
res.decodeBody()
body, err := bodyOf(res)
checkErr(t, "bodyOf", err, "")
if got := string(body); got != payload {
t.Errorf("body = %q, want %q", got, payload)
}
})
t.Run("an undecoded body is refused, and says why", func(t *testing.T) {
res := encoded("compress", []byte("payload"))
res.decodeBody()
_, err := bodyOf(res)
checkErrMatch(t, "bodyOf", err, `^body: response is compress-encoded and was not decoded: `)
})
}
// Test_bodyAssertionsRefuseAnEncodedBody: the guard has to be on all three, not
// on the one that was reported. A missed assertion is a silent false pass.
func Test_bodyAssertionsRefuseAnEncodedBody(t *testing.T) {
t.Parallel()
// A pattern loose enough to match almost any bytes. Before the guard this
// passed against compressed noise, which is the dangerous half of #27: not
// a confusing failure, but a green run that checked nothing.
match, err := AssertBodyMatch(".")
if err != nil {
t.Fatalf("cannot build the assertion: %s", err)
}
assertions := map[string]Assertion{
"AssertBodyMatch": match,
"AssertBodyEqual": AssertBodyEqual(payload),
"AssertBodyEmpty": AssertBodyEmpty(),
"AssertBodyNotEmpty": AssertBodyNotEmpty(),
}
for name, a := range assertions {
t.Run(name, func(t *testing.T) {
res := encoded("compress", []byte("payload"))
res.decodeBody()
checkErrMatch(t, name, check(a, res), `^body: response is compress-encoded and was not decoded: `)
})
}
}