blob: c65bc4ba5d7a810666d8df1cd32afb316c2044b9 [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 Bretoncc9bf2e2020-11-12 11:14:41 +010070} -start
71
72server s2 {
73 rxreq
74 expect req.url == "/expires"
75 # Expires header is filled directly by the expires_be backend"
76 txresp \
77 -bodylen 170
78} -start
79
80haproxy h1 -conf {
Willy Tarreaue1465c12021-05-09 14:41:41 +020081 global
82 # WT: limit false-positives causing "HTTP header incomplete" due to
83 # idle server connections being randomly used and randomly expiring
84 # under us.
85 tune.idle-pool.shared off
86
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +010087 defaults
88 mode http
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +010089 timeout connect 1s
90 timeout client 1s
91 timeout server 1s
92
93 frontend fe
94 bind "fd@${fe}"
95 use_backend expires_be if { path_beg /expires }
96 default_backend test
97
98 backend expires_be
99 http-request cache-use my_cache
100 server www ${s2_addr}:${s2_port}
101 http-response set-header X-Cache-Hit %[res.cache_hit]
102 # Expires value set in the future (current_time+5s)
103 http-response set-header Expires %[date(5),http_date]
104 http-response cache-store my_cache
105
106 backend test
107 http-request cache-use my_cache
108 server www ${s1_addr}:${s1_port}
109 http-response cache-store my_cache
110 http-response set-header X-Cache-Hit %[res.cache_hit]
111
112 cache my_cache
113 total-max-size 3
114 max-age 20
115 max-object-size 3072
116} -start
117
118
119client c1 -connect ${h1_fe_sock} {
120 txreq -url "/max-age"
121 rxresp
122 expect resp.status == 200
123 expect resp.bodylen == 150
124
125 txreq -url "/max-age"
126 rxresp
127 expect resp.status == 200
128 expect resp.bodylen == 150
129 expect resp.http.X-Cache-Hit == 1
130
131 txreq -url "/s-maxage"
132 rxresp
133 expect resp.status == 200
134 expect resp.bodylen == 160
135
136 txreq -url "/s-maxage"
137 rxresp
138 expect resp.status == 200
139 expect resp.bodylen == 160
140 expect resp.http.X-Cache-Hit == 1
141
142 txreq -url "/expires"
143 rxresp
144 expect resp.status == 200
145 expect resp.bodylen == 170
146
147 txreq -url "/expires"
148 rxresp
149 expect resp.status == 200
150 expect resp.bodylen == 170
151 expect resp.http.X-Cache-Hit == 1
152
153 txreq -url "/last-modified"
154 rxresp
155 expect resp.status == 200
156 expect resp.bodylen == 180
157
158 txreq -url "/last-modified"
159 rxresp
160 expect resp.status == 200
161 expect resp.bodylen == 180
162 expect resp.http.X-Cache-Hit == 1
163
164 txreq -url "/etag"
165 rxresp
166 expect resp.status == 200
167 expect resp.bodylen == 190
168
169 txreq -url "/etag"
170 rxresp
171 expect resp.status == 200
172 expect resp.bodylen == 190
173 expect resp.http.X-Cache-Hit == 1
174
175 # The next response should not be cached
176 txreq -url "/uncacheable"
177 rxresp
178 expect resp.status == 200
179 expect resp.bodylen == 200
180
181 txreq -url "/uncacheable"
182 rxresp
183 expect resp.status == 200
184 expect resp.bodylen == 210
185 expect resp.http.X-Cache-Hit == 0
186
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100187 # Age header tests
188 txreq -url "/invalid_age"
189 rxresp
190 expect resp.status == 200
191 expect resp.bodylen == 120
192 expect resp.http.X-Cache-Hit == 0
193
194 txreq -url "/invalid_age"
195 rxresp
196 expect resp.status == 200
197 expect resp.bodylen == 120
198 expect resp.http.X-Cache-Hit == 0
199
200 txreq -url "/old_age"
201 rxresp
202 expect resp.status == 200
203 expect resp.bodylen == 130
204 expect resp.http.X-Cache-Hit == 0
205
206 txreq -url "/old_age"
207 rxresp
208 expect resp.status == 200
209 expect resp.bodylen == 130
210 expect resp.http.X-Cache-Hit == 0
211
212 txreq -url "/good_age"
213 rxresp
214 expect resp.status == 200
215 expect resp.bodylen == 140
216 expect resp.http.X-Cache-Hit == 0
217
218 txreq -url "/good_age"
219 rxresp
220 expect resp.status == 200
221 expect resp.bodylen == 140
222 expect resp.http.X-Cache-Hit == 1
223
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100224} -run