blob: c20a720e3cc2fac4a7d17c4e0ba7a2f993bfd5e0 [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 {
81 defaults
82 mode http
83 ${no-htx} option http-use-htx
84 timeout connect 1s
85 timeout client 1s
86 timeout server 1s
87
88 frontend fe
89 bind "fd@${fe}"
90 use_backend expires_be if { path_beg /expires }
91 default_backend test
92
93 backend expires_be
94 http-request cache-use my_cache
95 server www ${s2_addr}:${s2_port}
96 http-response set-header X-Cache-Hit %[res.cache_hit]
97 # Expires value set in the future (current_time+5s)
98 http-response set-header Expires %[date(5),http_date]
99 http-response cache-store my_cache
100
101 backend test
102 http-request cache-use my_cache
103 server www ${s1_addr}:${s1_port}
104 http-response cache-store my_cache
105 http-response set-header X-Cache-Hit %[res.cache_hit]
106
107 cache my_cache
108 total-max-size 3
109 max-age 20
110 max-object-size 3072
111} -start
112
113
114client c1 -connect ${h1_fe_sock} {
115 txreq -url "/max-age"
116 rxresp
117 expect resp.status == 200
118 expect resp.bodylen == 150
119
120 txreq -url "/max-age"
121 rxresp
122 expect resp.status == 200
123 expect resp.bodylen == 150
124 expect resp.http.X-Cache-Hit == 1
125
126 txreq -url "/s-maxage"
127 rxresp
128 expect resp.status == 200
129 expect resp.bodylen == 160
130
131 txreq -url "/s-maxage"
132 rxresp
133 expect resp.status == 200
134 expect resp.bodylen == 160
135 expect resp.http.X-Cache-Hit == 1
136
137 txreq -url "/expires"
138 rxresp
139 expect resp.status == 200
140 expect resp.bodylen == 170
141
142 txreq -url "/expires"
143 rxresp
144 expect resp.status == 200
145 expect resp.bodylen == 170
146 expect resp.http.X-Cache-Hit == 1
147
148 txreq -url "/last-modified"
149 rxresp
150 expect resp.status == 200
151 expect resp.bodylen == 180
152
153 txreq -url "/last-modified"
154 rxresp
155 expect resp.status == 200
156 expect resp.bodylen == 180
157 expect resp.http.X-Cache-Hit == 1
158
159 txreq -url "/etag"
160 rxresp
161 expect resp.status == 200
162 expect resp.bodylen == 190
163
164 txreq -url "/etag"
165 rxresp
166 expect resp.status == 200
167 expect resp.bodylen == 190
168 expect resp.http.X-Cache-Hit == 1
169
170 # The next response should not be cached
171 txreq -url "/uncacheable"
172 rxresp
173 expect resp.status == 200
174 expect resp.bodylen == 200
175
176 txreq -url "/uncacheable"
177 rxresp
178 expect resp.status == 200
179 expect resp.bodylen == 210
180 expect resp.http.X-Cache-Hit == 0
181
Remi Tricot-Le Breton795e1412020-12-03 18:19:29 +0100182 # Age header tests
183 txreq -url "/invalid_age"
184 rxresp
185 expect resp.status == 200
186 expect resp.bodylen == 120
187 expect resp.http.X-Cache-Hit == 0
188
189 txreq -url "/invalid_age"
190 rxresp
191 expect resp.status == 200
192 expect resp.bodylen == 120
193 expect resp.http.X-Cache-Hit == 0
194
195 txreq -url "/old_age"
196 rxresp
197 expect resp.status == 200
198 expect resp.bodylen == 130
199 expect resp.http.X-Cache-Hit == 0
200
201 txreq -url "/old_age"
202 rxresp
203 expect resp.status == 200
204 expect resp.bodylen == 130
205 expect resp.http.X-Cache-Hit == 0
206
207 txreq -url "/good_age"
208 rxresp
209 expect resp.status == 200
210 expect resp.bodylen == 140
211 expect resp.http.X-Cache-Hit == 0
212
213 txreq -url "/good_age"
214 rxresp
215 expect resp.status == 200
216 expect resp.bodylen == 140
217 expect resp.http.X-Cache-Hit == 1
218
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +0100219} -run