blob: 0e10d83004b64db1427112ec8012ee358e82f082 [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 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
101 ${no-htx} option http-use-htx
102 timeout connect 1s
103 timeout client 1s
104 timeout server 1s
105
106 frontend fe
107 bind "fd@${fe}"
108 use_backend expires_be if { path_beg /expires }
109 default_backend test
110
111 backend expires_be
112 http-request cache-use my_cache
113 server www ${s2_addr}:${s2_port}
114 http-response set-header X-Cache-Hit %[res.cache_hit]
115 # Expires value set in the future (current_time+5s)
116 http-response set-header Expires %[date(5),http_date]
117 http-response cache-store my_cache
118
119 backend test
120 http-request cache-use my_cache
121 server www ${s1_addr}:${s1_port}
122 http-response cache-store my_cache
123 http-response set-header X-Cache-Hit %[res.cache_hit]
124
125 cache my_cache
126 total-max-size 3
127 max-age 20
128 max-object-size 3072
129} -start
130
131
132client c1 -connect ${h1_fe_sock} {
133 txreq -url "/max-age"
134 rxresp
135 expect resp.status == 200
136 expect resp.bodylen == 150
137
138 txreq -url "/max-age"
139 rxresp
140 expect resp.status == 200
141 expect resp.bodylen == 150
142 expect resp.http.X-Cache-Hit == 1
143
144 txreq -url "/s-maxage"
145 rxresp
146 expect resp.status == 200
147 expect resp.bodylen == 160
148
149 txreq -url "/s-maxage"
150 rxresp
151 expect resp.status == 200
152 expect resp.bodylen == 160
153 expect resp.http.X-Cache-Hit == 1
154
155 txreq -url "/expires"
156 rxresp
157 expect resp.status == 200
158 expect resp.bodylen == 170
159
160 txreq -url "/expires"
161 rxresp
162 expect resp.status == 200
163 expect resp.bodylen == 170
164 expect resp.http.X-Cache-Hit == 1
165
166 txreq -url "/last-modified"
167 rxresp
168 expect resp.status == 200
169 expect resp.bodylen == 180
170
171 txreq -url "/last-modified"
172 rxresp
173 expect resp.status == 200
174 expect resp.bodylen == 180
175 expect resp.http.X-Cache-Hit == 1
176
177 txreq -url "/etag"
178 rxresp
179 expect resp.status == 200
180 expect resp.bodylen == 190
181
182 txreq -url "/etag"
183 rxresp
184 expect resp.status == 200
185 expect resp.bodylen == 190
186 expect resp.http.X-Cache-Hit == 1
187
188 # The next response should not be cached
189 txreq -url "/uncacheable"
190 rxresp
191 expect resp.status == 200
192 expect resp.bodylen == 200
193
194 txreq -url "/uncacheable"
195 rxresp
196 expect resp.status == 200
197 expect resp.bodylen == 210
198 expect resp.http.X-Cache-Hit == 0
199
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100200 # Age header tests
201 txreq -url "/invalid_age"
202 rxresp
203 expect resp.status == 200
204 expect resp.bodylen == 120
205 expect resp.http.X-Cache-Hit == 0
206
207 txreq -url "/invalid_age"
208 rxresp
209 expect resp.status == 200
210 expect resp.bodylen == 120
211 expect resp.http.X-Cache-Hit == 0
212
213 txreq -url "/old_age"
214 rxresp
215 expect resp.status == 200
216 expect resp.bodylen == 130
217 expect resp.http.X-Cache-Hit == 0
218
219 txreq -url "/old_age"
220 rxresp
221 expect resp.status == 200
222 expect resp.bodylen == 130
223 expect resp.http.X-Cache-Hit == 0
224
225 txreq -url "/good_age"
226 rxresp
227 expect resp.status == 200
228 expect resp.bodylen == 140
229 expect resp.http.X-Cache-Hit == 0
230
231 txreq -url "/good_age"
232 rxresp
233 expect resp.status == 200
234 expect resp.bodylen == 140
235 expect resp.http.X-Cache-Hit == 1
236
Remi Tricot-Le Bretona59ce4f2023-02-21 11:47:17 +0100237 # Cache-Control: no-cache
238 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
239 rxresp
240 expect resp.status == 200
241 expect resp.bodylen == 140
242 expect resp.http.X-Cache-Hit == 0
243
244 txreq -url "/nocache" -hdr "Cache-Control: no-cache"
245 rxresp
246 expect resp.status == 200
247 expect resp.bodylen == 140
248 expect resp.http.X-Cache-Hit == 0
249
250 txreq -url "/nocache"
251 rxresp
252 expect resp.status == 200
253 expect resp.bodylen == 140
254 expect resp.http.X-Cache-Hit == 1
255
256
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100257} -run