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_resp_headers_test.go
296 lines (239 loc) · 7.62 KB
/
cdn_resp_headers_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
package main
import (
"fmt"
"net/http"
"regexp"
"testing"
"time"
)
// Test that useful common cache-related parameters are sent to the
// client by this CDN provider.
// Should set an Age header, when origin doesn't provide one, representing
// how long the object has been in edge's cache.
func TestRespHeaderAgeFromEdge(t *testing.T) {
ResetBackends(backendsByPriority)
const secondsToWaitBetweenRequests = 5
expectedHeaderVals := []string{
"0",
fmt.Sprintf("%d", secondsToWaitBetweenRequests),
}
req := NewUniqueEdgeGET(t)
for requestCount, expectedHeaderVal := range expectedHeaderVals {
requestCount = requestCount + 1
switch requestCount {
case 1:
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=1800, public")
w.Write([]byte("cacheable request"))
})
case 2:
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Origin received request and it shouldn't have")
})
// Wait for Age to increment.
time.Sleep(time.Duration(secondsToWaitBetweenRequests) * time.Second)
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Request %d received incorrect status %q", requestCount, resp.Status)
}
if val := resp.Header.Get("Age"); val != expectedHeaderVal {
t.Errorf(
"Request %d received incorrect Age header. Got %q, expected %q",
requestCount,
val,
expectedHeaderVal,
)
}
}
}
// Should propagate an Age header from origin and then increment it for the
// time it is in edge's cache. This assumes no request/response delay:
// http://tools.ietf.org/html/rfc7234#section-4.2.3
func TestRespHeaderAgeFromOrigin(t *testing.T) {
ResetBackends(backendsByPriority)
const originAgeInSeconds = 100
const secondsToWaitBetweenRequests = 5
expectedHeaderVals := []string{
fmt.Sprintf("%d", originAgeInSeconds),
fmt.Sprintf("%d", originAgeInSeconds+secondsToWaitBetweenRequests),
}
req := NewUniqueEdgeGET(t)
for requestCount, expectedHeaderVal := range expectedHeaderVals {
requestCount = requestCount + 1
switch requestCount {
case 1:
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Age", expectedHeaderVal)
w.Header().Set("Cache-Control", "max-age=1800, public")
w.Write([]byte("cacheable request"))
})
case 2:
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Origin received request and it shouldn't have")
})
// Wait for Age to increment.
time.Sleep(time.Duration(secondsToWaitBetweenRequests) * time.Second)
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("Request %d received incorrect status %q", requestCount, resp.Status)
}
if val := resp.Header.Get("Age"); val != expectedHeaderVal {
t.Errorf(
"Request %d received incorrect Age header. Got %q, expected %q",
requestCount,
val,
expectedHeaderVal,
)
}
}
}
// Should set an X-Cache header containing HIT/MISS from 'origin, itself'
func TestRespHeaderXCacheAppend(t *testing.T) {
ResetBackends(backendsByPriority)
if vendorCloudflare {
t.Skip(notSupportedByVendor)
}
const originXCache = "HIT"
var (
xCache string
expectedXCache string
)
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Cache", originXCache)
})
// Get first request, will come from origin, cannot be cached - hence cache MISS
req := NewUniqueEdgeGET(t)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
xCache = resp.Header.Get("X-Cache")
expectedXCache = fmt.Sprintf("%s, MISS", originXCache)
if xCache != expectedXCache {
t.Errorf(
"X-Cache on initial hit is wrong: expected %q, got %q",
expectedXCache,
xCache,
)
}
}
// Should set a header containing 'HIT' or 'MISS' depending on whether request is cached
func TestRespHeaderCacheHitMiss(t *testing.T) {
ResetBackends(backendsByPriority)
var (
headerName string
headerValue string
)
switch {
case vendorCloudflare:
headerName = "CF-Cache-Status"
case vendorFastly:
headerName = "X-Cache"
default:
t.Fatal(notImplementedForVendor)
}
expectedHeaderValues := []string{"MISS", "HIT"}
const cacheDuration = time.Second
if vendorCloudflare {
cloudFlareStatuses := []string{"EXPIRED", "HIT"}
expectedHeaderValues = append(expectedHeaderValues, cloudFlareStatuses...)
}
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
cacheControlValue := fmt.Sprintf("max-age=%.0f", cacheDuration.Seconds())
w.Header().Set("Cache-Control", cacheControlValue)
})
req := NewUniqueEdgeGET(t)
for count, expectedValue := range expectedHeaderValues {
if expectedValue == "EXPIRED" {
// sleep long enough for object to have expired
sleepDuration := cacheDuration + time.Second
time.Sleep(sleepDuration)
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
headerValue = resp.Header.Get(headerName)
if headerValue != expectedValue {
t.Errorf(
"%s on request %d is wrong: expected %q, got %q",
headerName,
count+1,
expectedValue,
headerValue,
)
}
}
}
// Should set an 'Served-By' header giving information on the edge node and location served from.
func TestRespHeaderServedBy(t *testing.T) {
ResetBackends(backendsByPriority)
var expectedServedByRegexp *regexp.Regexp
var headerName string
switch {
case vendorCloudflare:
headerName = "CF-RAY"
expectedServedByRegexp = regexp.MustCompile("^[a-z0-9]{16}-[A-Z]{3}$")
case vendorFastly:
headerName = "X-Served-By"
expectedServedByRegexp = regexp.MustCompile("^cache-[a-z0-9]+-[A-Z]{3}$")
default:
t.Fatal(notImplementedForVendor)
}
req := NewUniqueEdgeGET(t)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
actualHeader := resp.Header.Get(headerName)
if actualHeader == "" {
t.Error(headerName + " header has not been set by Edge")
}
if expectedServedByRegexp.FindString(actualHeader) != actualHeader {
t.Errorf("%s is not as expected: got %q", headerName, actualHeader)
}
}
// Should set an X-Cache-Hits header containing hit count for this object,
// from the Edge AND the Origin, assuming Origin sets one.
// This is in the format "{origin-hit-count}, {edge-hit-count}"
func TestRespHeaderXCacheHitsAppend(t *testing.T) {
ResetBackends(backendsByPriority)
if vendorCloudflare {
t.Skip(notSupportedByVendor)
}
const originXCacheHits = "53"
var (
xCacheHits string
expectedXCacheHits string
)
uuid := NewUUID()
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/%s", uuid) {
w.Header().Set("X-Cache-Hits", originXCacheHits)
}
})
sourceURL := fmt.Sprintf("https://%s/%s", *edgeHost, uuid)
// Get first request, will come from origin. Edge Hit Count 0
req, _ := http.NewRequest("GET", sourceURL, nil)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
xCacheHits = resp.Header.Get("X-Cache-Hits")
expectedXCacheHits = fmt.Sprintf("%s, 0", originXCacheHits)
if xCacheHits != expectedXCacheHits {
t.Errorf(
"X-Cache-Hits on initial hit is wrong: expected %q, got %q",
expectedXCacheHits,
xCacheHits,
)
}
// Get request again. Should come from Edge now, hit count 1
resp = RoundTripCheckError(t, req)
defer resp.Body.Close()
xCacheHits = resp.Header.Get("X-Cache-Hits")
expectedXCacheHits = fmt.Sprintf("%s, 1", originXCacheHits)
if xCacheHits != expectedXCacheHits {
t.Errorf(
"X-Cache-Hits on second hit is wrong: expected %q, got %q",
expectedXCacheHits,
xCacheHits,
)
}
}