-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathhttpcache_test.go
239 lines (214 loc) · 5.45 KB
/
httpcache_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
package runtime
import (
"context"
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.starlark.net/starlark"
)
func TestInitHTTP(t *testing.T) {
c := NewInMemoryCache()
InitHTTP(c)
b, err := os.ReadFile("testdata/httpcache.star")
assert.NoError(t, err)
app, err := NewApplet("httpcache.star", b)
assert.NoError(t, err)
assert.NotNil(t, app)
screens, err := app.Run(context.Background())
assert.NoError(t, err)
assert.NotNil(t, screens)
}
// TestDetermineTTL tests the DetermineTTL function.
func TestDetermineTTL(t *testing.T) {
type test struct {
ttl int
retryAfter int
resHeader string
statusCode int
method string
expectedTTL time.Duration
}
tests := map[string]test{
"test request cache control headers": {
ttl: 3600,
resHeader: "",
statusCode: 200,
method: "GET",
expectedTTL: 3600 * time.Second,
},
"test response cache control headers": {
ttl: 0,
resHeader: "public, max-age=3600, s-maxage=7200, no-transform",
statusCode: 200,
method: "GET",
expectedTTL: 3600 * time.Second,
},
"test too long response cache control headers": {
ttl: 0,
resHeader: "max-age=604800",
statusCode: 200,
method: "GET",
expectedTTL: 3600 * time.Second,
},
"test max-age of zero": {
ttl: 0,
resHeader: "max-age=0",
statusCode: 200,
method: "GET",
expectedTTL: 5 * time.Second,
},
"test both request and response cache control headers": {
ttl: 3600,
resHeader: "public, max-age=60, s-maxage=7200, no-transform",
statusCode: 200,
method: "GET",
expectedTTL: 3600 * time.Second,
},
"test 500 response code": {
ttl: 3600,
resHeader: "",
statusCode: 500,
method: "GET",
expectedTTL: 5 * time.Second,
},
"test too low ttl": {
ttl: 3,
resHeader: "",
statusCode: 200,
method: "GET",
expectedTTL: 5 * time.Second,
},
"test DELETE request": {
ttl: 0,
resHeader: "",
statusCode: 200,
method: "DELETE",
expectedTTL: 5 * time.Second,
},
"test POST request configured with TTL": {
ttl: 30,
resHeader: "",
statusCode: 200,
method: "POST",
expectedTTL: 30 * time.Second,
},
"test POST request without configured TTL": {
ttl: 0,
resHeader: "",
statusCode: 200,
method: "POST",
expectedTTL: 5 * time.Second,
},
"test 429 request": {
ttl: 30,
retryAfter: 60,
resHeader: "",
statusCode: 429,
method: "GET",
expectedTTL: 60 * time.Second,
},
"test 429 request below minimum": {
ttl: 30,
retryAfter: 3,
resHeader: "",
statusCode: 429,
method: "GET",
expectedTTL: 5 * time.Second,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
req := &http.Request{
Header: map[string][]string{
"X-Tidbyt-Cache-Seconds": {fmt.Sprintf("%d", tc.ttl)},
},
Method: tc.method,
}
res := &http.Response{
Header: map[string][]string{
"Cache-Control": {tc.resHeader},
},
StatusCode: tc.statusCode,
}
if tc.retryAfter > 0 {
res.Header.Set("Retry-After", fmt.Sprintf("%d", tc.retryAfter))
}
ttl := determineTTL(req, res)
assert.Equal(t, tc.expectedTTL, ttl)
})
}
}
func TestDetermineTTLJitter(t *testing.T) {
req := &http.Request{
Header: map[string][]string{
"X-Tidbyt-Cache-Seconds": {"60"},
},
Method: "GET",
}
res := &http.Response{
StatusCode: 200,
}
rand.Seed(50)
ttl := DetermineTTL(req, res)
assert.Equal(t, 64, int(ttl.Seconds()))
}
func TestDetermineTTLNoHeaders(t *testing.T) {
req := &http.Request{
Method: "GET",
}
res := &http.Response{
StatusCode: 200,
}
ttl := DetermineTTL(req, res)
assert.Equal(t, MinRequestTTL, ttl)
}
func TestSetCookieOnRedirect(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Requests to "/#" set a cookie and redirect to /destination
if strings.HasSuffix(r.URL.Path, "/#") {
if len(r.Cookies()) != 0 {
t.Errorf("Cookie was already set on initial call")
}
w.Header().Set("Set-Cookie", "doodad=foobar; path=/; HttpOnly")
w.Header().Set("Location", "/destination")
w.WriteHeader(302)
return
}
// Requests to /destination must have cookie set
if strings.HasSuffix(r.URL.Path, "/destination") {
c, err := r.Cookie("doodad")
if err != nil {
t.Errorf("Expected cookie `doodad` not present") // Occurs if client has no cookie jar
}
if c.Value != "foobar" {
t.Errorf("Cookie `doodad` value mismatch. Expected foobar, got %s", c.Value)
}
if _, err := w.Write([]byte(`{"hello":"world"}`)); err != nil {
t.Fatal(err)
}
return
}
t.Errorf("Unexpected path requested: %s", r.URL.Path)
}))
starlark.Universe["test_server_url"] = starlark.String(ts.URL)
c := NewInMemoryCache()
InitHTTP(c)
b, err := os.ReadFile("testdata/httpredirect.star")
assert.NoError(t, err)
app, err := NewApplet("httpredirect.star", b)
assert.NoError(t, err)
_, err = app.Run(context.Background())
assert.NoError(t, err)
// Run it again and check that we're not using the same cookie jar
// across executions. If we were, the first request would error out.
app2, err := NewApplet("httpredirect.star", b)
assert.NoError(t, err)
_, err = app2.Run(context.Background())
assert.NoError(t, err)
}