blob: 10840e1b38518e77b967689c1a4cb968240fdb1a [file] [log] [blame]
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01001varnishtest "Caching rules test"
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +01002# A response will not be cached unless it has an explicit age (Cache-Control max-age of s-maxage, Expires) or a validator (Last-Modified, or ETag)
3# A response will not be cached either if it has an Age header that is either invalid (should be an integer) or greater than its max age.
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01004
Christopher Faulet85a81362020-12-15 17:13:39 +01005#REQUIRE_VERSION=2.4
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01006
7feature ignore_unknown_macro
8
9server s1 {
10 rxreq
11 expect req.url == "/max-age"
12 txresp -hdr "Cache-Control: max-age=5" \
13 -bodylen 150
14
15 rxreq
16 expect req.url == "/s-maxage"
17 txresp -hdr "Cache-Control: s-maxage=5" \
18 -bodylen 160
19
20 rxreq
21 expect req.url == "/last-modified"
22 txresp -hdr "Last-Modified: Thu, 22 Oct 2020 16:51:12 GMT" \
23 -bodylen 180
24
25 rxreq
26 expect req.url == "/etag"
27 txresp -hdr "ETag: \"etag\"" \
28 -bodylen 190
29
30 rxreq
31 expect req.url == "/uncacheable"
32 txresp \
33 -bodylen 200
34
35 rxreq
36 expect req.url == "/uncacheable"
37 txresp \
38 -bodylen 210
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +010039
40 # Age response header checks
41
42 # Invalid age
43 rxreq
44 expect req.url == "/invalid_age"
45 txresp -hdr "Cache-Control: max-age=5" \
46 -hdr "Age: abc" -bodylen 120
47
48 rxreq
49 expect req.url == "/invalid_age"
50 txresp -hdr "Cache-Control: max-age=5" \
51 -hdr "Age: abc" -bodylen 120
52
53 # Old age (greater than max age)
54 rxreq
55 expect req.url == "/old_age"
56 txresp -hdr "Cache-Control: max-age=5" \
57 -hdr "Age: 10" -bodylen 130
58
59 rxreq
60 expect req.url == "/old_age"
61 txresp -hdr "Cache-Control: max-age=5" \
62 -hdr "Age: 10" -bodylen 130
63
64 # Good age
65 rxreq
66 expect req.url == "/good_age"
67 txresp -hdr "Cache-Control: max-age=500" \
68 -hdr "Age: 100" -bodylen 140
69
Remi Tricot-Le Bretona59ce4f2023-02-21 11:47:17 +010070
71 # "Control-Cache: no-cache" on client request but still stored in cache
72 rxreq
73 expect req.url == "/nocache"
74 txresp -hdr "Cache-Control: max-age=500" \
75 -hdr "Age: 100" -bodylen 140
76
77 rxreq
78 expect req.url == "/nocache"
79 txresp -hdr "Cache-Control: max-age=500" \
80 -hdr "Age: 100" -bodylen 140
81
Remi Tricot-Le Bretondd270e02023-07-04 17:13:28 +020082
83 # max-age=0
84 rxreq
85 expect req.url == "/maxage_zero"
86 txresp -hdr "Cache-Control: max-age=0" \
87 -bodylen 150
88
89 rxreq
90 expect req.url == "/maxage_zero"
91 txresp -hdr "Cache-Control: max-age=0" \
92 -bodylen 150
93
94 # Overridden null max-age
95 rxreq
96 expect req.url == "/overridden"
97 txresp -hdr "Cache-Control: max-age=1, s-maxage=5" \
98 -bodylen 160
99
100 rxreq
101 expect req.url == "/overridden_null_maxage"
102 txresp -hdr "Cache-Control: max-age=0, s-maxage=5" \
103 -bodylen 190
104
105
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100106} -start
107
108server s2 {
109 rxreq
110 expect req.url == "/expires"
111 # Expires header is filled directly by the expires_be backend"
112 txresp \
113 -bodylen 170
114} -start
115
116haproxy h1 -conf {
Willy Tarreaue1465c12021-05-09 14:41:41 +0200117 global
118 # WT: limit false-positives causing "HTTP header incomplete" due to
119 # idle server connections being randomly used and randomly expiring
120 # under us.
121 tune.idle-pool.shared off
122
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100123 defaults
124 mode http
125 ${no-htx} option http-use-htx
126 timeout connect 1s
127 timeout client 1s
128 timeout server 1s
129
130 frontend fe
131 bind "fd@${fe}"
132 use_backend expires_be if { path_beg /expires }
133 default_backend test
134
135 backend expires_be
136 http-request cache-use my_cache
137 server www ${s2_addr}:${s2_port}
138 http-response set-header X-Cache-Hit %[res.cache_hit]
139 # Expires value set in the future (current_time+5s)
140 http-response set-header Expires %[date(5),http_date]
141 http-response cache-store my_cache
142
143 backend test
144 http-request cache-use my_cache
145 server www ${s1_addr}:${s1_port}
146 http-response cache-store my_cache
147 http-response set-header X-Cache-Hit %[res.cache_hit]
148
149 cache my_cache
150 total-max-size 3
151 max-age 20
152 max-object-size 3072
153} -start
154
155
156client c1 -connect ${h1_fe_sock} {
157 txreq -url "/max-age"
158 rxresp
159 expect resp.status == 200
160 expect resp.bodylen == 150
161
162 txreq -url "/max-age"
163 rxresp
164 expect resp.status == 200
165 expect resp.bodylen == 150
166 expect resp.http.X-Cache-Hit == 1
167
168 txreq -url "/s-maxage"
169 rxresp
170 expect resp.status == 200
171 expect resp.bodylen == 160
172
173 txreq -url "/s-maxage"
174 rxresp
175 expect resp.status == 200
176 expect resp.bodylen == 160
177 expect resp.http.X-Cache-Hit == 1
178
179 txreq -url "/expires"
180 rxresp
181 expect resp.status == 200
182 expect resp.bodylen == 170
183
184 txreq -url "/expires"
185 rxresp
186 expect resp.status == 200
187 expect resp.bodylen == 170
188 expect resp.http.X-Cache-Hit == 1
189
190 txreq -url "/last-modified"
191 rxresp
192 expect resp.status == 200
193 expect resp.bodylen == 180
194
195 txreq -url "/last-modified"
196 rxresp
197 expect resp.status == 200
198 expect resp.bodylen == 180
199 expect resp.http.X-Cache-Hit == 1
200
201 txreq -url "/etag"
202 rxresp
203 expect resp.status == 200
204 expect resp.bodylen == 190
205
206 txreq -url "/etag"
207 rxresp
208 expect resp.status == 200
209 expect resp.bodylen == 190
210 expect resp.http.X-Cache-Hit == 1
211
212 # The next response should not be cached
213 txreq -url "/uncacheable"
214 rxresp
215 expect resp.status == 200
216 expect resp.bodylen == 200
217
218 txreq -url "/uncacheable"
219 rxresp
220 expect resp.status == 200
221 expect resp.bodylen == 210
222 expect resp.http.X-Cache-Hit == 0
223
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100224 # Age header tests
225 txreq -url "/invalid_age"
226 rxresp
227 expect resp.status == 200
228 expect resp.bodylen == 120
229 expect resp.http.X-Cache-Hit == 0
230
231 txreq -url "/invalid_age"
232 rxresp
233 expect resp.status == 200
234 expect resp.bodylen == 120
235 expect resp.http.X-Cache-Hit == 0
236
237 txreq -url "/old_age"
238 rxresp
239 expect resp.status == 200
240 expect resp.bodylen == 130
241 expect resp.http.X-Cache-Hit == 0
242
243 txreq -url "/old_age"
244 rxresp
245 expect resp.status == 200
246 expect resp.bodylen == 130
247 expect resp.http.X-Cache-Hit == 0
248
249 txreq -url "/good_age"
250 rxresp
251 expect resp.status == 200
252 expect resp.bodylen == 140
253 expect resp.http.X-Cache-Hit == 0
254
255 txreq -url "/good_age"
256 rxresp
257 expect resp.status == 200
258 expect resp.bodylen == 140
259 expect resp.http.X-Cache-Hit == 1
260
Remi Tricot-Le Bretona59ce4f2023-02-21 11:47:17 +0100261 # Cache-Control: no-cache
262 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
263 rxresp
264 expect resp.status == 200
265 expect resp.bodylen == 140
266 expect resp.http.X-Cache-Hit == 0
267
268 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
269 rxresp
270 expect resp.status == 200
271 expect resp.bodylen == 140
272 expect resp.http.X-Cache-Hit == 0
273
274 txreq -url "/nocache"
275 rxresp
276 expect resp.status == 200
277 expect resp.bodylen == 140
278 expect resp.http.X-Cache-Hit == 1
279
Remi Tricot-Le Bretondd270e02023-07-04 17:13:28 +0200280 # max-age=0 (control test for the overridden null max-age test below)
281 txreq -url "/maxage_zero"
282 rxresp
283 expect resp.status == 200
284 expect resp.bodylen == 150
285 expect resp.http.X-Cache-Hit == 0
286
287 txreq -url "/maxage_zero"
288 rxresp
289 expect resp.status == 200
290 expect resp.bodylen == 150
291 expect resp.http.X-Cache-Hit == 0
292
293 # Overridden max-age directive
294 txreq -url "/overridden"
295 rxresp
296 expect resp.status == 200
297 expect resp.bodylen == 160
298 expect resp.http.X-Cache-Hit == 0
299
300 txreq -url "/overridden"
301 rxresp
302 expect resp.status == 200
303 expect resp.bodylen == 160
304 expect resp.http.X-Cache-Hit == 1
305
306 txreq -url "/overridden_null_maxage"
307 rxresp
308 expect resp.status == 200
309 expect resp.bodylen == 190
310 expect resp.http.X-Cache-Hit == 0
311
312 # The previous response should have been cached even if it had
313 # a max-age=0 since it also had a positive s-maxage
314 txreq -url "/overridden_null_maxage"
315 rxresp
316 expect resp.status == 200
317 expect resp.bodylen == 190
318 expect resp.http.X-Cache-Hit == 1
319
Remi Tricot-Le Bretona59ce4f2023-02-21 11:47:17 +0100320
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100321} -run