-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
177 lines (164 loc) · 5.34 KB
/
cache_test.go
File metadata and controls
177 lines (164 loc) · 5.34 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
package arc
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCacheResponsesMiddleware(t *testing.T) {
type in struct{}
e := New()
e.Use(CacheResponses(2 * time.Second))
counter := 0
HandleOut(e, http.MethodGet, "/cached", "cached_get", func(ctx context.Context, in *in) (*map[string]any, error) {
counter++
out := map[string]any{"n": counter}
return &out, nil
})
w1 := httptest.NewRecorder()
e.ServeHTTP(w1, httptest.NewRequest(http.MethodGet, "/cached", nil))
if w1.Code != http.StatusOK {
t.Fatalf("status1=%d", w1.Code)
}
if w1.Header().Get("X-Cache") != "MISS" {
t.Fatalf("first response should be MISS, got %s", w1.Header().Get("X-Cache"))
}
w2 := httptest.NewRecorder()
e.ServeHTTP(w2, httptest.NewRequest(http.MethodGet, "/cached", nil))
if w2.Code != http.StatusOK {
t.Fatalf("status2=%d", w2.Code)
}
if w2.Header().Get("X-Cache") != "HIT" {
t.Fatalf("second response should be HIT, got %s", w2.Header().Get("X-Cache"))
}
if counter != 1 {
t.Fatalf("handler should be called once, got %d", counter)
}
}
func TestCacheResponsesBypassForAuthorized(t *testing.T) {
type in struct{}
e := New()
e.Use(CacheResponses(2 * time.Second))
counter := 0
HandleOut(e, http.MethodGet, "/secure-cached", "secure_cached_get", func(ctx context.Context, in *in) (*map[string]any, error) {
counter++
out := map[string]any{"n": counter}
return &out, nil
})
req1 := httptest.NewRequest(http.MethodGet, "/secure-cached", nil)
req1.Header.Set("Authorization", "Bearer token")
w1 := httptest.NewRecorder()
e.ServeHTTP(w1, req1)
req2 := httptest.NewRequest(http.MethodGet, "/secure-cached", nil)
req2.Header.Set("Authorization", "Bearer token")
w2 := httptest.NewRecorder()
e.ServeHTTP(w2, req2)
if counter != 2 {
t.Fatalf("authorized requests must bypass cache, counter=%d", counter)
}
if got := w2.Header().Get("X-Cache"); got != "BYPASS" {
t.Fatalf("expected BYPASS, got %s", got)
}
}
func TestCacheResponsesETag304(t *testing.T) {
type in struct{}
store := NewResponseCache()
e := New()
e.Use(CacheResponsesWithConfig(CacheConfig{
TTL: 2 * time.Second,
Store: store,
SkipAuthorized: true,
SkipCookies: true,
}))
counter := 0
HandleOut(e, http.MethodGet, "/etagged", "etagged_get", func(ctx context.Context, in *in) (*map[string]any, error) {
counter++
out := map[string]any{"n": counter}
return &out, nil
})
w1 := httptest.NewRecorder()
e.ServeHTTP(w1, httptest.NewRequest(http.MethodGet, "/etagged", nil))
etag := w1.Header().Get("ETag")
if etag == "" {
t.Fatalf("etag is required on cached response")
}
if counter != 1 {
t.Fatalf("counter=%d", counter)
}
req2 := httptest.NewRequest(http.MethodGet, "/etagged", nil)
req2.Header.Set("If-None-Match", etag)
w2 := httptest.NewRecorder()
e.ServeHTTP(w2, req2)
if w2.Code != http.StatusNotModified {
t.Fatalf("expected 304, got %d", w2.Code)
}
if counter != 1 {
t.Fatalf("handler should not run for 304 cache hit, counter=%d", counter)
}
}
func TestCacheResponsesSkipsNoStore(t *testing.T) {
type in struct{}
e := New()
e.Use(CacheResponses(2 * time.Second))
counter := 0
Handle(e, http.MethodGet, "/nostore", "nostore_get", func(ctx context.Context, in *in) (*Response[map[string]any], error) {
counter++
resp := OK(map[string]any{"n": counter})
resp.Headers = CacheControlHeaders(0)
resp.Headers.Set("Cache-Control", "no-store")
return resp, nil
})
w1 := httptest.NewRecorder()
e.ServeHTTP(w1, httptest.NewRequest(http.MethodGet, "/nostore", nil))
w2 := httptest.NewRecorder()
e.ServeHTTP(w2, httptest.NewRequest(http.MethodGet, "/nostore", nil))
if counter != 2 {
t.Fatalf("no-store responses must not be cached, counter=%d", counter)
}
if got := w2.Header().Get("X-Cache"); got != "BYPASS" {
t.Fatalf("expected BYPASS for no-store response, got %s", got)
}
}
func TestCacheInvalidationOnWrite(t *testing.T) {
type in struct{}
type postIn struct {
ID int64 `path:"id"`
}
store := NewResponseCache()
e := New()
e.Use(CacheResponsesWithConfig(CacheConfig{TTL: 5 * time.Second, Store: store}))
e.Use(InvalidateCacheOnWrite(CacheInvalidationConfig{
Store: store,
InvalidateRequestPath: true,
}))
counter := 0
HandleOut(e, http.MethodGet, "/products", "products_get", func(ctx context.Context, in *in) (*map[string]any, error) {
counter++
out := map[string]any{"n": counter}
return &out, nil
})
HandleErr(e, http.MethodPost, "/products/{id}", "products_update", func(ctx context.Context, in *postIn) error {
return nil
})
w1 := httptest.NewRecorder()
e.ServeHTTP(w1, httptest.NewRequest(http.MethodGet, "/products", nil))
w2 := httptest.NewRecorder()
e.ServeHTTP(w2, httptest.NewRequest(http.MethodGet, "/products", nil))
if counter != 1 || w2.Header().Get("X-Cache") != "HIT" {
t.Fatalf("expected warm cache hit, counter=%d x-cache=%s", counter, w2.Header().Get("X-Cache"))
}
wp := httptest.NewRecorder()
e.ServeHTTP(wp, httptest.NewRequest(http.MethodPost, "/products/1", nil))
if wp.Code != http.StatusNoContent {
t.Fatalf("post status=%d", wp.Code)
}
w3 := httptest.NewRecorder()
e.ServeHTTP(w3, httptest.NewRequest(http.MethodGet, "/products", nil))
if counter != 2 {
t.Fatalf("cache should be invalidated after write, counter=%d", counter)
}
if got := w3.Header().Get("X-Cache"); got != "MISS" {
t.Fatalf("expected MISS after invalidation, got %s", got)
}
}