blob: 61274b4b58a8abf8d55ccf4b6179f1889968c9e5 [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 Breton879debe2023-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 Bretoncc9bf2e2020-11-12 11:14:41 +010082} -start
83
84server s2 {
85 rxreq
86 expect req.url == "/expires"
87 # Expires header is filled directly by the expires_be backend"
88 txresp \
89 -bodylen 170
90} -start
91
92haproxy h1 -conf {
Willy Tarreaue1465c12021-05-09 14:41:41 +020093 global
94 # WT: limit false-positives causing "HTTP header incomplete" due to
95 # idle server connections being randomly used and randomly expiring
96 # under us.
97 tune.idle-pool.shared off
98
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +010099 defaults
100 mode http
Willy Tarreauf6739232021-11-18 17:46:22 +0100101 timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
102 timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
103 timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100104
105 frontend fe
106 bind "fd@${fe}"
107 use_backend expires_be if { path_beg /expires }
108 default_backend test
109
110 backend expires_be
111 http-request cache-use my_cache
112 server www ${s2_addr}:${s2_port}
113 http-response set-header X-Cache-Hit %[res.cache_hit]
114 # Expires value set in the future (current_time+5s)
115 http-response set-header Expires %[date(5),http_date]
116 http-response cache-store my_cache
117
118 backend test
119 http-request cache-use my_cache
120 server www ${s1_addr}:${s1_port}
121 http-response cache-store my_cache
122 http-response set-header X-Cache-Hit %[res.cache_hit]
123
124 cache my_cache
125 total-max-size 3
126 max-age 20
127 max-object-size 3072
128} -start
129
130
131client c1 -connect ${h1_fe_sock} {
132 txreq -url "/max-age"
133 rxresp
134 expect resp.status == 200
135 expect resp.bodylen == 150
136
137 txreq -url "/max-age"
138 rxresp
139 expect resp.status == 200
140 expect resp.bodylen == 150
141 expect resp.http.X-Cache-Hit == 1
142
143 txreq -url "/s-maxage"
144 rxresp
145 expect resp.status == 200
146 expect resp.bodylen == 160
147
148 txreq -url "/s-maxage"
149 rxresp
150 expect resp.status == 200
151 expect resp.bodylen == 160
152 expect resp.http.X-Cache-Hit == 1
153
154 txreq -url "/expires"
155 rxresp
156 expect resp.status == 200
157 expect resp.bodylen == 170
158
159 txreq -url "/expires"
160 rxresp
161 expect resp.status == 200
162 expect resp.bodylen == 170
163 expect resp.http.X-Cache-Hit == 1
164
165 txreq -url "/last-modified"
166 rxresp
167 expect resp.status == 200
168 expect resp.bodylen == 180
169
170 txreq -url "/last-modified"
171 rxresp
172 expect resp.status == 200
173 expect resp.bodylen == 180
174 expect resp.http.X-Cache-Hit == 1
175
176 txreq -url "/etag"
177 rxresp
178 expect resp.status == 200
179 expect resp.bodylen == 190
180
181 txreq -url "/etag"
182 rxresp
183 expect resp.status == 200
184 expect resp.bodylen == 190
185 expect resp.http.X-Cache-Hit == 1
186
187 # The next response should not be cached
188 txreq -url "/uncacheable"
189 rxresp
190 expect resp.status == 200
191 expect resp.bodylen == 200
192
193 txreq -url "/uncacheable"
194 rxresp
195 expect resp.status == 200
196 expect resp.bodylen == 210
197 expect resp.http.X-Cache-Hit == 0
198
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100199 # Age header tests
200 txreq -url "/invalid_age"
201 rxresp
202 expect resp.status == 200
203 expect resp.bodylen == 120
204 expect resp.http.X-Cache-Hit == 0
205
206 txreq -url "/invalid_age"
207 rxresp
208 expect resp.status == 200
209 expect resp.bodylen == 120
210 expect resp.http.X-Cache-Hit == 0
211
212 txreq -url "/old_age"
213 rxresp
214 expect resp.status == 200
215 expect resp.bodylen == 130
216 expect resp.http.X-Cache-Hit == 0
217
218 txreq -url "/old_age"
219 rxresp
220 expect resp.status == 200
221 expect resp.bodylen == 130
222 expect resp.http.X-Cache-Hit == 0
223
224 txreq -url "/good_age"
225 rxresp
226 expect resp.status == 200
227 expect resp.bodylen == 140
228 expect resp.http.X-Cache-Hit == 0
229
230 txreq -url "/good_age"
231 rxresp
232 expect resp.status == 200
233 expect resp.bodylen == 140
234 expect resp.http.X-Cache-Hit == 1
235
Remi Tricot-Le Breton879debe2023-02-21 11:47:17 +0100236 # Cache-Control: no-cache
237 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
238 rxresp
239 expect resp.status == 200
240 expect resp.bodylen == 140
241 expect resp.http.X-Cache-Hit == 0
242
243 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
244 rxresp
245 expect resp.status == 200
246 expect resp.bodylen == 140
247 expect resp.http.X-Cache-Hit == 0
248
249 txreq -url "/nocache"
250 rxresp
251 expect resp.status == 200
252 expect resp.bodylen == 140
253 expect resp.http.X-Cache-Hit == 1
254
255
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100256} -run