-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
325 lines (276 loc) · 9 KB
/
cache_test.go
File metadata and controls
325 lines (276 loc) · 9 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
package relay
import (
"net/http"
"testing"
"time"
"github.com/jhonsferg/relay/testutil"
)
func TestCache_HitSecondRequestDoesNotReachServer(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{
"Cache-Control": "max-age=3600",
"Content-Type": "text/plain",
},
Body: "cached-body",
})
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithInMemoryCache(64))
// First request: cache miss → hits server.
resp1, err := c.Execute(c.Get(srv.URL() + "/resource"))
if err != nil {
t.Fatalf("first Execute: %v", err)
}
if resp1.String() != "cached-body" {
t.Errorf("first response body: expected 'cached-body', got %q", resp1.String())
}
if srv.RequestCount() != 1 {
t.Errorf("expected 1 server request after first call, got %d", srv.RequestCount())
}
// Second request: cache hit → server should NOT be called again.
resp2, err := c.Execute(c.Get(srv.URL() + "/resource"))
if err != nil {
t.Fatalf("second Execute: %v", err)
}
if resp2.String() != "cached-body" {
t.Errorf("second response body: expected 'cached-body', got %q", resp2.String())
}
if srv.RequestCount() != 1 {
t.Errorf("expected 1 server request (cache hit), got %d", srv.RequestCount())
}
}
func TestCache_NoStoreMissesCache(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
// Two responses with Cache-Control: no-store - never cached.
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{"Cache-Control": "no-store"},
Body: "no-store-1",
})
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{"Cache-Control": "no-store"},
Body: "no-store-2",
})
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithInMemoryCache(64))
_, err := c.Execute(c.Get(srv.URL() + "/ns"))
if err != nil {
t.Fatalf("first Execute: %v", err)
}
_, err = c.Execute(c.Get(srv.URL() + "/ns"))
if err != nil {
t.Fatalf("second Execute: %v", err)
}
if srv.RequestCount() != 2 {
t.Errorf("Cache-Control: no-store should bypass cache; expected 2 server requests, got %d", srv.RequestCount())
}
}
func TestCache_RequestNoCacheBypassesCache(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{"Cache-Control": "max-age=3600"},
Body: "first",
})
// Second response served after forced revalidation.
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{"Cache-Control": "max-age=3600"},
Body: "second",
})
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithInMemoryCache(64))
// Prime the cache.
_, _ = c.Execute(c.Get(srv.URL() + "/revalidate"))
// Second request with Cache-Control: no-cache forces revalidation.
req := c.Get(srv.URL()+"/revalidate").WithHeader("Cache-Control", "no-cache")
resp, err := c.Execute(req)
if err != nil {
t.Fatalf("second Execute: %v", err)
}
if srv.RequestCount() != 2 {
t.Errorf("expected 2 server requests (no-cache forces revalidation), got %d", srv.RequestCount())
}
_ = resp
}
func TestCache_ETagRevalidation304(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
// First response: cacheable with ETag and a long max-age so the entry is stored.
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{
"Cache-Control": "max-age=3600",
"ETag": `"abc123"`,
"Content-Type": "text/plain",
},
Body: "original-body",
})
// Second response: 304 Not Modified (ETag matched).
srv.Enqueue(testutil.MockResponse{
Status: http.StatusNotModified,
})
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithInMemoryCache(64))
// First request: cache miss → stores the entry.
resp1, err := c.Execute(c.Get(srv.URL() + "/etag"))
if err != nil {
t.Fatalf("first Execute: %v", err)
}
if resp1.String() != "original-body" {
t.Errorf("expected original body, got %q", resp1.String())
}
// Second request: force revalidation via request Cache-Control: no-cache.
// This causes the caching transport to send If-None-Match.
req2 := c.Get(srv.URL()+"/etag").WithHeader("Cache-Control", "no-cache")
resp2, err := c.Execute(req2)
if err != nil {
t.Fatalf("second Execute: %v", err)
}
// Server returns 304 → client should serve the cached body.
if resp2.String() != "original-body" {
t.Errorf("304 revalidation should serve cached body, got %q", resp2.String())
}
if srv.RequestCount() != 2 {
t.Errorf("expected 2 server requests (initial + revalidation), got %d", srv.RequestCount())
}
// Verify If-None-Match was sent on second request.
srv.TakeRequest(time.Second) //nolint:errcheck
recorded2, err := srv.TakeRequest(time.Second)
if err != nil {
t.Fatalf("second recorded request not found: %v", err)
}
if recorded2.Headers.Get("If-None-Match") == "" {
t.Errorf("expected If-None-Match header on revalidation request")
}
}
func TestCache_LastModifiedRevalidation304(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
lastModified := "Wed, 21 Oct 2015 07:28:00 GMT"
// First response with Last-Modified, no ETag, stored via max-age.
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{
"Cache-Control": "max-age=3600",
"Last-Modified": lastModified,
"Content-Type": "text/plain",
},
Body: "lm-body",
})
// Second: 304 Not Modified.
srv.Enqueue(testutil.MockResponse{
Status: http.StatusNotModified,
})
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithInMemoryCache(64))
// First request: cache miss → stores the entry.
resp1, err := c.Execute(c.Get(srv.URL() + "/lastmod"))
if err != nil {
t.Fatalf("first Execute: %v", err)
}
if resp1.String() != "lm-body" {
t.Errorf("expected 'lm-body', got %q", resp1.String())
}
// Second request: force revalidation via request Cache-Control: no-cache.
req2 := c.Get(srv.URL()+"/lastmod").WithHeader("Cache-Control", "no-cache")
resp2, err := c.Execute(req2)
if err != nil {
t.Fatalf("second Execute: %v", err)
}
if resp2.String() != "lm-body" {
t.Errorf("304 revalidation should serve cached body, got %q", resp2.String())
}
// Check that If-Modified-Since was sent.
srv.TakeRequest(time.Second) //nolint:errcheck
recorded2, err := srv.TakeRequest(time.Second)
if err != nil {
t.Fatalf("second recorded request not found: %v", err)
}
if recorded2.Headers.Get("If-Modified-Since") == "" {
t.Errorf("expected If-Modified-Since header on revalidation request")
}
}
func TestCache_LRUEviction(t *testing.T) {
t.Parallel()
srv := testutil.NewMockServer()
defer srv.Close()
// cache with capacity 2
store := NewInMemoryCacheStore(2)
// Enqueue responses for 3 distinct resources + 1 for the cache-miss check.
for i := 0; i < 4; i++ {
srv.Enqueue(testutil.MockResponse{
Status: http.StatusOK,
Headers: map[string]string{"Cache-Control": "max-age=3600"},
Body: "body",
})
}
c := New(WithDisableRetry(), WithDisableCircuitBreaker(), WithCache(store))
// Fill the cache to capacity.
c.Execute(c.Get(srv.URL() + "/a")) //nolint:errcheck
c.Execute(c.Get(srv.URL() + "/b")) //nolint:errcheck
// Adding /c evicts the oldest entry (/a).
c.Execute(c.Get(srv.URL() + "/c")) //nolint:errcheck
countAfterFill := srv.RequestCount()
if countAfterFill != 3 {
t.Fatalf("expected 3 server requests to fill cache, got %d", countAfterFill)
}
// Requesting /a again should be a cache miss (it was evicted).
c.Execute(c.Get(srv.URL() + "/a")) //nolint:errcheck
if srv.RequestCount() != 4 {
t.Errorf("expected 4 server requests (/a was evicted), got %d", srv.RequestCount())
}
}
func TestInMemoryCacheStore_GetSetDelete(t *testing.T) {
t.Parallel()
store := NewInMemoryCacheStore(10)
entry := &CachedResponse{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: []byte("hello"),
}
store.Set("key1", entry)
got, ok := store.Get("key1")
if !ok {
t.Fatal("expected cache hit for key1")
}
if string(got.Body) != "hello" {
t.Errorf("expected body 'hello', got %q", string(got.Body))
}
store.Delete("key1")
_, ok = store.Get("key1")
if ok {
t.Error("expected cache miss after Delete")
}
}
func TestInMemoryCacheStore_Clear(t *testing.T) {
t.Parallel()
store := NewInMemoryCacheStore(10)
store.Set("a", &CachedResponse{StatusCode: 200})
store.Set("b", &CachedResponse{StatusCode: 200})
store.Clear()
if _, ok := store.Get("a"); ok {
t.Error("expected cache miss for 'a' after Clear")
}
if _, ok := store.Get("b"); ok {
t.Error("expected cache miss for 'b' after Clear")
}
}
func TestInMemoryCacheStore_ExpiredEntryNotReturned(t *testing.T) {
t.Parallel()
store := NewInMemoryCacheStore(10)
entry := &CachedResponse{
StatusCode: http.StatusOK,
ExpiresAt: time.Now().Add(-time.Second), // already expired
}
store.Set("expired", entry)
_, ok := store.Get("expired")
if ok {
t.Error("expired entry should not be returned")
}
}