This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcdn_cache_test.go
428 lines (354 loc) · 11.9 KB
/
cdn_cache_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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)
// Should cache first response for an unspecified period of time when it
// doesn't specify its own cache headers. Subsequent requests should return
// a cached response.
func TestCacheFirstResponse(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
testRequestsCachedIndefinite(t, req, nil)
}
// Should cache responses for the period defined in a `Expires: n` response
// header.
func TestCacheExpires(t *testing.T) {
ResetBackends(backendsByPriority)
const cacheDuration = time.Duration(5 * time.Second)
handler := func(w http.ResponseWriter) {
headerValue := time.Now().UTC().Add(cacheDuration).Format(http.TimeFormat)
w.Header().Set("Expires", headerValue)
}
req := NewUniqueEdgeGET(t)
testRequestsCachedDuration(t, req, handler, cacheDuration)
}
// Should cache responses for the period defined in a `Cache-Control:
// max-age=n` response header.
func TestCacheCacheControlMaxAge(t *testing.T) {
ResetBackends(backendsByPriority)
const cacheDuration = time.Duration(5 * time.Second)
headerValue := fmt.Sprintf("max-age=%.0f", cacheDuration.Seconds())
handler := func(w http.ResponseWriter) {
w.Header().Set("Cache-Control", headerValue)
}
req := NewUniqueEdgeGET(t)
testRequestsCachedDuration(t, req, handler, cacheDuration)
}
// Should cache responses for the period defined in a `Cache-Control:
// max-age=n` response header when a `Expires: n*2` header is also present.
func TestCacheExpiresAndMaxAge(t *testing.T) {
ResetBackends(backendsByPriority)
const cacheDuration = time.Duration(5 * time.Second)
const expiresDuration = cacheDuration * 2
maxAgeValue := fmt.Sprintf("max-age=%.0f", cacheDuration.Seconds())
handler := func(w http.ResponseWriter) {
expiresValue := time.Now().UTC().Add(expiresDuration).Format(http.TimeFormat)
w.Header().Set("Expires", expiresValue)
w.Header().Set("Cache-Control", maxAgeValue)
}
req := NewUniqueEdgeGET(t)
testRequestsCachedDuration(t, req, handler, cacheDuration)
}
// This tests documents actual behaviour; even though it contravenes RFC 7234 section 5.2.1.1:
// http://tools.ietf.org/html/rfc7234#section-5.2.1.1
// Serves a cached response to a request with a `Cache-Control: max-age=0` header.
func TestCacheReqHeaderMaxAge(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Header.Set("Cache-Control", "max-age=0")
testRequestsCachedIndefinite(t, req, nil)
}
// This tests documents actual behaviour; even though it contravenes RFC 7234 section 5.2.1.4:
// http://tools.ietf.org/html/rfc7234#section-5.2.1.4
// Serves a cached response to a request with a `Cache-Control: no-cache` header.
func TestCacheReqHeaderNoCache(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Header.Set("Cache-Control", "no-cache")
testRequestsCachedIndefinite(t, req, nil)
}
// This tests documents actual behaviour; even though it contravenes RFC 7234 section 5.2.1.5:
// http://tools.ietf.org/html/rfc7234#section-5.2.1.5
// Serves a cached response to a request with a `Cache-Control: no-store` header.
func TestCacheReqHeaderNoStore(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Header.Set("Cache-Control", "no-store")
testRequestsCachedIndefinite(t, req, nil)
}
// Should cache the response to a request with a `Cookie` header.
func TestCacheHeaderCookie(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Header.Set("Cookie", "sekret=mekmitasdigoat")
testRequestsCachedIndefinite(t, req, nil)
}
// Should cache a response with a `Set-Cookie` and no explicit
// `Cache-Control` headers.
func TestCacheHeaderSetCookie(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(w http.ResponseWriter) {
w.Header().Set("Set-Cookie", "sekret=mekmitasdigoat")
}
req := NewUniqueEdgeGET(t)
testRequestsCachedIndefinite(t, req, handler)
}
// Should cache the response to a request with a `Authorization` header.
// This tests documents actual behaviour; even though it appears to
// contravene RFC 7234 section 3.2:
// http://tools.ietf.org/html/rfc7234#section-3.2
func TestCacheHeaderAuthorization(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Header.Set("Authorization", "Basic YXJlbnR5b3U6aW5xdWlzaXRpdmU=")
testRequestsCachedIndefinite(t, req, nil)
}
// Should cache responses with a status code of 404. It's a common
// misconception that 404 responses shouldn't be cached; they should because
// they can be expensive to generate.
func TestCache404Response(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
}
req := NewUniqueEdgeGET(t)
testRequestsCachedIndefinite(t, req, handler)
}
// Should cache multiple distinct responses for the same URL when origin responds
// with a `Vary` header and clients provide requests with different values
// for that header.
func TestCacheVary(t *testing.T) {
ResetBackends(backendsByPriority)
if vendorCloudflare {
t.Skip(notSupportedByVendor)
}
const reqHeaderName = "CustomThing"
const respHeaderName = "Reflected-" + reqHeaderName
headerVals := []string{
"first distinct",
"second distinct",
"third distinct",
}
req := NewUniqueEdgeGET(t)
for _, populateCache := range []bool{true, false} {
for _, headerVal := range headerVals {
if populateCache {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Vary", reqHeaderName)
w.Header().Set(respHeaderName, r.Header.Get(reqHeaderName))
})
} else {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Request should not have made it to origin")
w.Header().Set(respHeaderName, "not cached")
})
}
req.Header.Set(reqHeaderName, headerVal)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if recVal := resp.Header.Get(respHeaderName); recVal != headerVal {
t.Errorf(
"Request received wrong %q header. Expected %q, got %q",
respHeaderName,
headerVal,
recVal,
)
}
}
}
}
// Should deliver gzip compressed response bodies to client requests with
// the header `Accept-Encoding: gzip` and plaintext response bodies for
// clients that don't. Some vendors:
// - appear to implement this independent of normal `Vary` headers
// - will make a single request w/gzip to origin and handle
// compression/decompression to the client themselves.
func TestCacheAcceptEncodingGzip(t *testing.T) {
ResetBackends(backendsByPriority)
const expectedBody = "may or may not be gzipped"
var reqAcceptEncoding string
var expectedContentEncoding string
// Tell the transport not to add Accept-Encoding headers and automatically
// decompress responses. Restore the setting after the test.
origClientDisableCompression := client.DisableCompression
client.DisableCompression = true
defer func() {
client.DisableCompression = origClientDisableCompression
}()
req := NewUniqueEdgeGET(t)
for _, populateCache := range []bool{true, false} {
for _, gzipContent := range []bool{false, true} {
if gzipContent {
reqAcceptEncoding = "gzip"
expectedContentEncoding = "gzip"
} else {
reqAcceptEncoding = "somethingelse"
expectedContentEncoding = ""
}
if populateCache {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
// NB: Some vendors don't appear to depend on this.
w.Header().Set("Vary", "Accept-Encoding")
// Don't switch on `gzipContent` because the edge may ask for gzip
// even if the client hasn't.
if r.Header.Get("Accept-Encoding") == "gzip" {
gzbuf := new(bytes.Buffer)
gzwriter := gzip.NewWriter(gzbuf)
gzwriter.Write([]byte(expectedBody))
gzwriter.Close()
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(gzbuf.Bytes())
} else {
w.Write([]byte(expectedBody))
}
})
} else {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Request should not have made it to origin")
w.Write([]byte("uncached response"))
})
}
req.Header.Set("Accept-Encoding", reqAcceptEncoding)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if headerVal := resp.Header.Get("Content-Encoding"); headerVal != expectedContentEncoding {
t.Fatalf(
"Request received incorrect Content-Encoding header. Expected %q, got %q",
expectedContentEncoding,
headerVal,
)
}
var rawBody io.ReadCloser
if gzipContent {
var err error
rawBody, err = gzip.NewReader(resp.Body)
if err != nil {
t.Fatal(err)
}
defer rawBody.Close()
} else {
rawBody = resp.Body
}
body, err := ioutil.ReadAll(rawBody)
if err != nil {
t.Fatal(err)
}
if bodyStr := string(body); bodyStr != expectedBody {
t.Errorf(
"Request received incorrect response body. Expected %q, got %q",
expectedBody,
bodyStr,
)
}
}
}
}
// Should cache distinct responses for requests with the same path but
// different query params.
func TestCacheUniqueQueryParams(t *testing.T) {
ResetBackends(backendsByPriority)
const respHeaderName = "Request-RawQuery"
req1 := NewUniqueEdgeGET(t)
req2 := NewUniqueEdgeGET(t)
if req1.URL.Path != req2.URL.Path {
t.Fatalf(
"Request paths do not match. Expected %q, got %q",
req1.URL.Path,
req2.URL.Path,
)
}
if req1.URL.RawQuery == req2.URL.RawQuery {
t.Fatalf(
"Request query params do not differ. Expected %q != %q",
req1.URL.RawQuery,
req2.URL.RawQuery,
)
}
for _, populateCache := range []bool{true, false} {
for _, req := range []*http.Request{req1, req2} {
if populateCache {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(respHeaderName, r.URL.RawQuery)
})
} else {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Errorf(
"Request with query param %q should not have made it to origin",
r.URL.RawQuery,
)
})
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if recVal := resp.Header.Get(respHeaderName); recVal != req.URL.RawQuery {
t.Errorf(
"Request received wrong %q header. Expected %q, got %q",
respHeaderName,
req.URL.RawQuery,
recVal,
)
}
}
}
}
// Should cache distinct responses for requests with the same query params
// but paths of different case-sensitivity.
func TestCacheUniqueCaseSensitive(t *testing.T) {
ResetBackends(backendsByPriority)
const reqPath = "/CaseSensitive"
const respHeaderName = "Request-Path"
req1 := NewUniqueEdgeGET(t)
req2 := NewUniqueEdgeGET(t)
req1.URL.Path = strings.ToLower(reqPath)
req2.URL.Path = strings.ToUpper(reqPath)
req1.URL.RawQuery = req2.URL.RawQuery
if req1.URL.Path == req2.URL.Path {
t.Fatalf(
"Request paths do not differ. Expected %q != %q",
req1.URL.Path,
req2.URL.Path,
)
}
if req1.URL.RawQuery != req2.URL.RawQuery {
t.Fatalf(
"Request query params do not match. Expected %q, got %q",
req1.URL.RawQuery,
req2.URL.RawQuery,
)
}
for _, populateCache := range []bool{true, false} {
for _, req := range []*http.Request{req1, req2} {
if populateCache {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(respHeaderName, r.URL.Path)
})
} else {
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Errorf(
"Request with path %q should not have made it to origin",
r.URL.Path,
)
})
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if recVal := resp.Header.Get(respHeaderName); recVal != req.URL.Path {
t.Errorf(
"Request received wrong %q header. Expected %q, got %q",
respHeaderName,
req.URL.Path,
recVal,
)
}
}
}
}