FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cache-handler/httpcache_test.go at master · suryatmodulus/cache-handler · GitHub
suryatmodulus
cache-handler
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cache-handler
/
httpcache_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
331 lines (297 loc) · 11.1 KB
Breadcrumbs
cache-handler
/
httpcache_test.go
Copy path
File metadata and controls
331 lines (297 loc) · 11.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
326
327
328
329
330
331
package
httpcache
import
(
"net/http"
"testing"
"time"
"github.com/caddyserver/caddy/v2/caddytest"
)
func
TestMinimal
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
order cache before rewrite
http_port 9080
https_port 9443
cache
}
localhost:9080 {
route /cache-default {
cache
respond "Hello, default!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-default`
,
200
,
"Hello, default!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-default"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp1
.
Header
)
}
resp2
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-default`
,
200
,
"Hello, default!"
)
if
resp2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=119; key=GET-http-localhost:9080-/cache-default"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp2
.
Header
.
Get
(
"Cache-Status"
))
}
time
.
Sleep
(
2
*
time
.
Second
)
resp3
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-default`
,
200
,
"Hello, default!"
)
if
resp3
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=117; key=GET-http-localhost:9080-/cache-default"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp3
.
Header
.
Get
(
"Cache-Status"
))
}
}
func
TestQueryString
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
order cache before rewrite
http_port 9080
https_port 9443
cache {
key {
disable_query
}
}
}
localhost:9080 {
route /query-string {
cache {
key {
disable_query
}
}
respond "Hello, query string!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/query-string?query=string`
,
200
,
"Hello, query string!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/query-string"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp1
.
Header
)
}
}
func
TestMaxAge
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
order cache before rewrite
http_port 9080
https_port 9443
cache
}
localhost:9080 {
route /cache-max-age {
cache
header Cache-Control "max-age=60"
respond "Hello, max-age!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-max-age`
,
200
,
"Hello, max-age!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-max-age"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp1
.
Header
)
}
resp2
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-max-age`
,
200
,
"Hello, max-age!"
)
if
resp2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=59; key=GET-http-localhost:9080-/cache-max-age"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp2
.
Header
.
Get
(
"Cache-Status"
))
}
time
.
Sleep
(
2
*
time
.
Second
)
resp3
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-max-age`
,
200
,
"Hello, max-age!"
)
if
resp3
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=57; key=GET-http-localhost:9080-/cache-max-age"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp3
.
Header
.
Get
(
"Cache-Status"
))
}
}
func
TestMaxStale
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
order cache before rewrite
http_port 9080
https_port 9443
cache {
stale 5s
}
}
localhost:9080 {
route /cache-max-stale {
cache
header Cache-Control "max-age=3"
respond "Hello, max-stale!"
}
}`
,
"caddyfile"
)
maxStaleURL
:=
"http://localhost:9080/cache-max-stale"
resp1
,
_
:=
tester
.
AssertGetResponse
(
maxStaleURL
,
200
,
"Hello, max-stale!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-max-stale"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp1
.
Header
)
}
resp2
,
_
:=
tester
.
AssertGetResponse
(
maxStaleURL
,
200
,
"Hello, max-stale!"
)
if
resp2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=2; key=GET-http-localhost:9080-/cache-max-stale"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp2
.
Header
.
Get
(
"Cache-Status"
))
}
time
.
Sleep
(
3
*
time
.
Second
)
reqMaxStale
,
_
:=
http
.
NewRequest
(
http
.
MethodGet
,
maxStaleURL
,
nil
)
reqMaxStale
.
Header
=
http.
Header
{
"Cache-Control"
: []
string
{
"max-stale=3"
}}
resp3
,
_
:=
tester
.
AssertResponse
(
reqMaxStale
,
200
,
"Hello, max-stale!"
)
if
resp3
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=-1; key=GET-http-localhost:9080-/cache-max-stale; fwd=stale"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp3
.
Header
.
Get
(
"Cache-Status"
))
}
time
.
Sleep
(
3
*
time
.
Second
)
resp4
,
_
:=
tester
.
AssertResponse
(
reqMaxStale
,
200
,
"Hello, max-stale!"
)
if
resp4
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-max-stale"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp4
.
Header
.
Get
(
"Cache-Status"
))
}
}
func
TestSMaxAge
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /cache-s-maxage {
cache
header Cache-Control "s-maxage=5"
respond "Hello, s-maxage!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-s-maxage`
,
200
,
"Hello, s-maxage!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/cache-s-maxage"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
resp1
.
Header
.
Get
(
"Cache-Status"
))
}
resp2
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/cache-s-maxage`
,
200
,
"Hello, s-maxage!"
)
if
resp2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=4; key=GET-http-localhost:9080-/cache-s-maxage"
{
t
.
Errorf
(
"unexpected Cache-Status header with %v"
,
resp2
.
Header
.
Get
(
"Cache-Status"
))
}
}
func
TestAgeHeader
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /age-header {
cache
header Cache-Control "max-age=60"
respond "Hello, Age header!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/age-header`
,
200
,
"Hello, Age header!"
)
if
resp1
.
Header
.
Get
(
"Age"
)
!=
""
{
t
.
Errorf
(
"unexpected Age header %v"
,
resp1
.
Header
.
Get
(
"Age"
))
}
resp2
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/age-header`
,
200
,
"Hello, Age header!"
)
if
resp2
.
Header
.
Get
(
"Age"
)
==
""
{
t
.
Error
(
"Age header should be present"
)
}
if
resp2
.
Header
.
Get
(
"Age"
)
!=
"1"
{
t
.
Error
(
"Age header should be present"
)
}
time
.
Sleep
(
10
*
time
.
Second
)
resp3
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/age-header`
,
200
,
"Hello, Age header!"
)
if
resp3
.
Header
.
Get
(
"Age"
)
!=
"11"
{
t
.
Error
(
"Age header should be present"
)
}
}
func
TestNotHandledRoute
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
regex {
exclude ".*handled"
}
}
}
localhost:9080 {
route /not-handled {
cache
header Cache-Control "max-age=60"
header Age "max-age=5"
respond "Hello, Age header!"
}
}`
,
"caddyfile"
)
resp1
,
_
:=
tester
.
AssertGetResponse
(
`http://localhost:9080/not-handled`
,
200
,
"Hello, Age header!"
)
if
resp1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; detail=EXCLUDED-REQUEST-URI"
{
t
.
Errorf
(
"unexpected Cache-Status header value %v"
,
resp1
.
Header
.
Get
(
"Cache-Status"
))
}
}
func
TestAuthenticatedRoute
(
t
*
testing.
T
) {
tester
:=
caddytest
.
NewTester
(
t
)
tester
.
InitServer
(
`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
ttl 1000s
}
}
localhost:9080 {
route /no-auth-bypass {
cache
respond "Hello, auth {http.request.header.Authorization}!"
}
route /auth-bypass {
cache {
key {
headers Authorization Content-Type
}
}
header Cache-Control "private, s-maxage=5"
respond "Hello, auth bypass {http.request.header.Authorization}!"
}
route /auth-bypass-vary {
cache {
key {
headers Authorization Content-Type
}
}
header Cache-Control "private, s-maxage=5"
header Vary "Content-Type, Authorization"
respond "Hello, auth vary bypass {http.request.header.Authorization}!"
}
}`
,
"caddyfile"
)
getRequestFor
:=
func
(
endpoint
,
user
string
)
*
http.
Request
{
rq
,
_
:=
http
.
NewRequest
(
http
.
MethodGet
,
"http://localhost:9080"
+
endpoint
,
nil
)
rq
.
Header
=
http.
Header
{
"Authorization"
: []
string
{
"Bearer "
+
user
},
"Content-Type"
: []
string
{
"text/plain"
}}
return
rq
}
respNoAuthBypass
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/no-auth-bypass"
,
"Alice"
),
200
,
"Hello, auth Bearer Alice!"
)
if
respNoAuthBypass
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; key=GET-http-localhost:9080-/no-auth-bypass; detail=PRIVATE-OR-AUTHENTICATED-RESPONSE"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respNoAuthBypass
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthBypassAlice1
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass"
,
"Alice"
),
200
,
"Hello, auth bypass Bearer Alice!"
)
if
respAuthBypassAlice1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/auth-bypass-Bearer Alice-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthBypassAlice1
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthBypassAlice2
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass"
,
"Alice"
),
200
,
"Hello, auth bypass Bearer Alice!"
)
if
respAuthBypassAlice2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=4; key=GET-http-localhost:9080-/auth-bypass-Bearer Alice-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthBypassAlice2
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthBypassBob1
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass"
,
"Bob"
),
200
,
"Hello, auth bypass Bearer Bob!"
)
if
respAuthBypassBob1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/auth-bypass-Bearer Bob-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthBypassBob1
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthBypassBob2
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass"
,
"Bob"
),
200
,
"Hello, auth bypass Bearer Bob!"
)
if
respAuthBypassBob2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=4; key=GET-http-localhost:9080-/auth-bypass-Bearer Bob-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthBypassBob2
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthVaryBypassAlice1
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass-vary"
,
"Alice"
),
200
,
"Hello, auth vary bypass Bearer Alice!"
)
if
respAuthVaryBypassAlice1
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/auth-bypass-vary-Bearer Alice-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthVaryBypassAlice1
.
Header
.
Get
(
"Cache-Status"
))
}
respAuthVaryBypassAlice2
,
_
:=
tester
.
AssertResponse
(
getRequestFor
(
"/auth-bypass-vary"
,
"Alice"
),
200
,
"Hello, auth vary bypass Bearer Alice!"
)
if
respAuthVaryBypassAlice2
.
Header
.
Get
(
"Cache-Status"
)
!=
"Souin; hit; ttl=4; key=GET-http-localhost:9080-/auth-bypass-vary-Bearer Alice-text/plain"
{
t
.
Errorf
(
"unexpected Cache-Status header %v"
,
respAuthVaryBypassAlice2
.
Header
.
Get
(
"Cache-Status"
))
}
}
Back
|
FazBrowse Home
|
New Git URL