Repository navigation
Expand file tree
/
Copy pathcache_control_test.go
More file actions
322 lines (278 loc) · 10.4 KB
/
Copy pathcache_control_test.go
File metadata and controls
322 lines (278 loc) · 10.4 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
package rest
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestRest_cacheControl(t *testing.T) {
tbl := []struct {
url string
version string
exp time.Duration
etag string
maxAge int
}{
{"http://example.com/foo", "v1", time.Hour, "b433be1ea19edaee9dc92ca4b895b6bdf3c058cb", 3600},
{"http://example.com/foo2", "v1", 10 * time.Hour, "6d8466aef3246c1057452561acddf7ad9d0d99e0", 36000},
{"http://example.com/foo", "v2", time.Hour, "481700c52aab0dfbca99f3ffc2a4fbb27884c114", 3600},
{"https://example.com/foo", "v2", time.Hour, "bebd4f1b87f474792c4e75e5affe31fbf67f5778", 3600},
}
for i, tt := range tbl {
t.Run(strconv.Itoa(i), func(t *testing.T) {
req := httptest.NewRequest("GET", tt.url, http.NoBody)
w := httptest.NewRecorder()
h := CacheControl(tt.exp, tt.version)(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
t.Logf("%+v", resp.Header)
assert.Equal(t, `"`+tt.etag+`"`, resp.Header.Get("Etag"))
assert.Equal(t, `max-age=`+strconv.Itoa(int(tt.exp.Seconds()))+", no-cache", resp.Header.Get("Cache-Control"))
})
}
}
func TestCacheControlDynamic(t *testing.T) {
tbl := []struct {
url string
version string
exp time.Duration
etag string
maxAge int
}{
{"http://example.com/foo", "v1", time.Hour, "b433be1ea19edaee9dc92ca4b895b6bdf3c058cb", 3600},
{"http://example.com/foo2", "v1", 10 * time.Hour, "6d8466aef3246c1057452561acddf7ad9d0d99e0", 36000},
{"http://example.com/foo", "v2", time.Hour, "481700c52aab0dfbca99f3ffc2a4fbb27884c114", 3600},
{"https://example.com/foo", "v2", time.Hour, "bebd4f1b87f474792c4e75e5affe31fbf67f5778", 3600},
}
for i, tt := range tbl {
t.Run(strconv.Itoa(i), func(t *testing.T) {
req := httptest.NewRequest("GET", tt.url, http.NoBody)
req.Header.Set("key", tt.version)
w := httptest.NewRecorder()
fn := func(r *http.Request) string {
return r.Header.Get("key")
}
h := CacheControlDynamic(tt.exp, fn)(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {}))
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
t.Logf("%+v", resp.Header)
assert.Equal(t, `"`+tt.etag+`"`, resp.Header.Get("Etag"))
assert.Equal(t, `max-age=`+strconv.Itoa(int(tt.exp.Seconds()))+", no-cache", resp.Header.Get("Cache-Control"))
})
}
}
func TestCacheControl_IfNoneMatch(t *testing.T) {
t.Run("matching etag returns 304", func(t *testing.T) {
handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
_, _ = w.Write([]byte("response body"))
})
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
// pre-computed etag for http://example.com/foo with version v1
req.Header.Set("If-None-Match", `"b433be1ea19edaee9dc92ca4b895b6bdf3c058cb"`)
w := httptest.NewRecorder()
h := CacheControl(time.Hour, "v1")(handler)
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusNotModified, resp.StatusCode)
assert.False(t, handlerCalled, "handler should not be called on 304")
})
t.Run("non-matching etag returns 200", func(t *testing.T) {
handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
_, _ = w.Write([]byte("response body"))
})
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
req.Header.Set("If-None-Match", `"wrong-etag"`)
w := httptest.NewRecorder()
h := CacheControl(time.Hour, "v1")(handler)
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, handlerCalled, "handler should be called on cache miss")
})
t.Run("no if-none-match returns 200", func(t *testing.T) {
handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
_, _ = w.Write([]byte("response body"))
})
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
w := httptest.NewRecorder()
h := CacheControl(time.Hour, "v1")(handler)
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, handlerCalled, "handler should be called without If-None-Match")
})
}
func TestCacheControlDynamic_IfNoneMatch(t *testing.T) {
versionFn := func(r *http.Request) string {
return r.Header.Get("key")
}
t.Run("matching etag returns 304", func(t *testing.T) {
handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
_, _ = w.Write([]byte("response body"))
})
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
req.Header.Set("key", "v1")
req.Header.Set("If-None-Match", `"b433be1ea19edaee9dc92ca4b895b6bdf3c058cb"`)
w := httptest.NewRecorder()
h := CacheControlDynamic(time.Hour, versionFn)(handler)
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusNotModified, resp.StatusCode)
assert.False(t, handlerCalled, "handler should not be called on 304")
})
t.Run("non-matching etag returns 200", func(t *testing.T) {
handlerCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
_, _ = w.Write([]byte("response body"))
})
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
req.Header.Set("key", "v1")
req.Header.Set("If-None-Match", `"wrong-etag"`)
w := httptest.NewRecorder()
h := CacheControlDynamic(time.Hour, versionFn)(handler)
h.ServeHTTP(w, req)
resp := w.Result()
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.True(t, handlerCalled, "handler should be called on cache miss")
})
}
func TestEtagMatches(t *testing.T) {
const etag = `"abc123"`
tbl := []struct {
name string
header string
want bool
}{
{"empty header", "", false},
{"exact match", `"abc123"`, true},
{"no match", `"other"`, false},
{"list, first", `"abc123", "other"`, true},
{"list, last", `"other", "abc123"`, true},
{"list, absent", `"one", "two"`, false},
{"weak validator on header tag", `W/"abc123"`, true},
{"weak validator in list", `"one", W/"abc123"`, true},
{"wildcard alone does not match", "*", false},
{"substring is not a match", `"abc12"`, false},
{"superstring is not a match", `"abc1234"`, false},
{"unquoted is not a match", "abc123", false},
{"spacing tolerated", ` "abc123" `, true},
}
for _, tt := range tbl {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, etagMatches([]string{tt.header}, etag))
})
}
}
func TestCacheControl_ConditionalMethods(t *testing.T) {
// the etag of http://example.com/foo with version v1
const knownEtag = `"b433be1ea19edaee9dc92ca4b895b6bdf3c058cb"`
tbl := []struct {
method string
wantStatus int
wantCalled bool
}{
{http.MethodGet, http.StatusNotModified, false},
{http.MethodHead, http.StatusNotModified, false},
{http.MethodPut, http.StatusOK, true},
{http.MethodPost, http.StatusOK, true},
{http.MethodDelete, http.StatusOK, true},
{http.MethodPatch, http.StatusOK, true},
}
for _, tt := range tbl {
t.Run(tt.method, func(t *testing.T) {
var called bool
handler := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { called = true })
req := httptest.NewRequest(tt.method, "http://example.com/foo", http.NoBody)
req.Header.Set("If-None-Match", knownEtag)
w := httptest.NewRecorder()
CacheControl(time.Hour, "v1")(handler).ServeHTTP(w, req)
assert.Equal(t, tt.wantStatus, w.Code, "unsafe methods carry preconditions this middleware doesn't enforce")
assert.Equal(t, tt.wantCalled, called)
})
}
}
func TestEtagMatchesRepeatedFields(t *testing.T) {
const etag = `"abc123"`
tbl := []struct {
name string
headers []string
want bool
}{
{"no fields", nil, false},
{"single field, match", []string{`"abc123"`}, true},
{"match in the second field", []string{`"one"`, `"abc123"`}, true},
{"match in the third field", []string{`"one"`, `"two"`, `"abc123"`}, true},
{"absent from every field", []string{`"one"`, `"two"`}, false},
{"weak validator in a later field", []string{`"one"`, `W/"abc123"`}, true},
}
for _, tt := range tbl {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, etagMatches(tt.headers, etag))
})
}
}
func TestCacheControl_RepeatedIfNoneMatchHeader(t *testing.T) {
// the etag of http://example.com/foo with version v1
const knownEtag = `"b433be1ea19edaee9dc92ca4b895b6bdf3c058cb"`
var called bool
handler := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { called = true })
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
// repeated fields are one list, a match in any of them counts
req.Header.Add("If-None-Match", `"something-else"`)
req.Header.Add("If-None-Match", knownEtag)
w := httptest.NewRecorder()
CacheControl(time.Hour, "v1")(handler).ServeHTTP(w, req)
assert.Equal(t, http.StatusNotModified, w.Code)
assert.False(t, called)
}
func TestCacheControl_HigherPriorityPreconditions(t *testing.T) {
const knownEtag = `"b433be1ea19edaee9dc92ca4b895b6bdf3c058cb"`
tbl := []struct {
name string
header string
value string
wantStatus int
wantCalled bool
}{
{"no other precondition", "", "", http.StatusNotModified, false},
{"if-match present", "If-Match", `"other"`, http.StatusOK, true},
{"if-unmodified-since present", "If-Unmodified-Since", "Wed, 21 Oct 2015 07:28:00 GMT", http.StatusOK, true},
{"if-match present but empty", "If-Match", "", http.StatusOK, true},
}
for _, tt := range tbl {
t.Run(tt.name, func(t *testing.T) {
var called bool
handler := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { called = true })
req := httptest.NewRequest("GET", "http://example.com/foo", http.NoBody)
req.Header.Set("If-None-Match", knownEtag)
if tt.header != "" {
req.Header.Add(tt.header, tt.value) // Add keeps an empty value as a present field
}
w := httptest.NewRecorder()
CacheControl(time.Hour, "v1")(handler).ServeHTTP(w, req)
// a 304 here would hide the 412 the handler may need to send
assert.Equal(t, tt.wantStatus, w.Code)
assert.Equal(t, tt.wantCalled, called)
})
}
}