-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcache_control_test.go
More file actions
235 lines (214 loc) · 6.4 KB
/
Copy pathcache_control_test.go
File metadata and controls
235 lines (214 loc) · 6.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
package cache
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
// Without the opt-in, Cache-Control on the response is ignored: the
// historical behavior of always-cache continues so existing users see
// no surprises.
func TestMiddlewareIgnoresCacheControlByDefault(t *testing.T) {
adapter := &adapterMock{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Header().Set("Cache-Control", "no-store")
fmt.Fprintf(w, "v=%d", calls)
}))
for i := 0; i < 2; i++ {
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://x/cc-default", nil))
}
if calls != 1 {
t.Fatalf("handler called %d times, want 1 (Cache-Control must be ignored without opt-in)", calls)
}
}
// With the opt-in, a response with Cache-Control: no-store must not be
// stored, and subsequent requests must re-invoke the handler.
func TestMiddlewareRespectsResponseNoStore(t *testing.T) {
adapter := &adapterMock{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
ClientWithRespectCacheControl(),
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Header().Set("Cache-Control", "no-store")
fmt.Fprintf(w, "v=%d", calls)
}))
for i := 0; i < 2; i++ {
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://x/cc-no-store", nil))
}
if calls != 2 {
t.Fatalf("handler called %d times, want 2", calls)
}
}
// Cache-Control: private targets a per-user cache; a shared HTTP cache
// like this one must not store it.
func TestMiddlewareRespectsResponsePrivate(t *testing.T) {
adapter := &adapterMock{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
ClientWithRespectCacheControl(),
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Header().Set("Cache-Control", "private, max-age=60")
fmt.Fprintf(w, "v=%d", calls)
}))
for i := 0; i < 2; i++ {
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://x/cc-private", nil))
}
if calls != 2 {
t.Fatalf("handler called %d times, want 2 (private must not be stored)", calls)
}
}
// max-age overrides the client TTL when the opt-in is enabled.
// s-maxage takes precedence for shared caches.
func TestMiddlewareRespectsResponseMaxAge(t *testing.T) {
tests := []struct {
name string
header string
wantSecs int
}{
{"max-age sets TTL", "max-age=42", 42},
{"s-maxage wins for shared caches", "max-age=10, s-maxage=99", 99},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
const url = "http://x/cc-maxage"
adapter := &adapterMock{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Hour),
ClientWithRespectCacheControl(),
)
if err != nil {
t.Fatal(err)
}
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", tt.header)
w.Write([]byte("ok"))
}))
before := time.Now()
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, url, nil))
after := time.Now()
stored, ok := adapter.Get(generateKey(url))
if !ok {
t.Fatal("response was not cached")
}
resp := BytesToResponse(stored)
gotTTL := resp.Expiration.Sub(before)
wantTTL := time.Duration(tt.wantSecs) * time.Second
elapsed := after.Sub(before)
// Allow slack for time measurement noise.
if gotTTL < wantTTL-elapsed || gotTTL > wantTTL+100*time.Millisecond {
t.Errorf("TTL = %v, want approximately %v", gotTTL, wantTTL)
}
})
}
}
// Cache-Control: no-store on the request bypasses the cache entirely:
// no lookup, no store.
func TestMiddlewareRespectsRequestNoStore(t *testing.T) {
adapter := &adapterMock{
store: map[uint64][]byte{
generateKey("http://x/cc-req"): Response{
Value: []byte("cached"),
Expiration: time.Now().Add(1 * time.Minute),
}.Bytes(),
},
}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
ClientWithRespectCacheControl(),
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Write([]byte("fresh"))
}))
r := httptest.NewRequest(http.MethodGet, "http://x/cc-req", nil)
r.Header.Set("Cache-Control", "no-store")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if got := w.Body.String(); got != "fresh" {
t.Errorf("body = %q, want fresh", got)
}
if calls != 1 {
t.Errorf("handler calls = %d, want 1", calls)
}
}
// Cache-Control: no-cache on the request forces a revalidation (we
// treat that as a miss and serve fresh) but the response is still
// stored for subsequent callers without the no-cache header.
func TestMiddlewareRespectsRequestNoCache(t *testing.T) {
const url = "http://x/cc-req-nc"
adapter := &adapterMock{
store: map[uint64][]byte{
generateKey(url): Response{
Value: []byte("old"),
Expiration: time.Now().Add(1 * time.Minute),
}.Bytes(),
},
}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
ClientWithRespectCacheControl(),
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Write([]byte("fresh"))
}))
r := httptest.NewRequest(http.MethodGet, url, nil)
r.Header.Set("Cache-Control", "no-cache")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
if got := w.Body.String(); got != "fresh" {
t.Errorf("body = %q, want fresh", got)
}
if calls != 1 {
t.Errorf("handler calls = %d, want 1", calls)
}
// Second request without no-cache hits the freshly-stored response.
r = httptest.NewRequest(http.MethodGet, url, nil)
w = httptest.NewRecorder()
handler.ServeHTTP(w, r)
if got := w.Body.String(); got != "fresh" {
t.Errorf("second request body = %q, want fresh", got)
}
if calls != 1 {
t.Errorf("handler calls = %d, want 1 (must hit cache)", calls)
}
}