summaryrefslogtreecommitdiffstats
path: root/reg-tests/cache/caching_rules.vtc
blob: a488875b30fc795ae2fa6bb58dde121fc269177b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
varnishtest "Caching rules test"
# 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)
# 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.

#REQUIRE_VERSION=2.4

feature ignore_unknown_macro

server s1 {
    rxreq
    expect req.url == "/max-age"
    txresp -hdr "Cache-Control: max-age=5" \
        -bodylen 150

    rxreq
    expect req.url == "/s-maxage"
    txresp -hdr "Cache-Control: s-maxage=5" \
        -bodylen 160

    rxreq
    expect req.url == "/last-modified"
    txresp -hdr "Last-Modified: Thu, 22 Oct 2020 16:51:12 GMT" \
        -bodylen 180

    rxreq
    expect req.url == "/etag"
    txresp -hdr "ETag: \"etag\"" \
        -bodylen 190

    rxreq
    expect req.url == "/uncacheable"
    txresp \
        -bodylen 200

    rxreq
    expect req.url == "/uncacheable"
    txresp \
        -bodylen 210

    # Age response header checks

    # Invalid age
    rxreq
    expect req.url == "/invalid_age"
    txresp -hdr "Cache-Control: max-age=5" \
        -hdr "Age: abc" -bodylen 120

    rxreq
    expect req.url == "/invalid_age"
    txresp -hdr "Cache-Control: max-age=5" \
        -hdr "Age: abc" -bodylen 120

    # Old age (greater than max age)
    rxreq
    expect req.url == "/old_age"
    txresp -hdr "Cache-Control: max-age=5" \
        -hdr "Age: 10" -bodylen 130

    rxreq
    expect req.url == "/old_age"
    txresp -hdr "Cache-Control: max-age=5" \
        -hdr "Age: 10" -bodylen 130

    # Good age
    rxreq
    expect req.url == "/good_age"
    txresp -hdr "Cache-Control: max-age=500" \
        -hdr "Age: 100" -bodylen 140


    # "Control-Cache: no-cache" on client request but still stored in cache
    rxreq
    expect req.url == "/nocache"
    txresp -hdr "Cache-Control: max-age=500" \
        -hdr "Age: 100" -bodylen 140

    rxreq
    expect req.url == "/nocache"
    txresp -hdr "Cache-Control: max-age=500" \
        -hdr "Age: 100" -bodylen 140


    # max-age=0
    rxreq
    expect req.url == "/maxage_zero"
    txresp -hdr "Cache-Control: max-age=0" \
        -bodylen 150

    rxreq
    expect req.url == "/maxage_zero"
    txresp -hdr "Cache-Control: max-age=0" \
        -bodylen 150

    # Overridden null max-age
    rxreq
    expect req.url == "/overridden"
    txresp -hdr "Cache-Control: max-age=1, s-maxage=5" \
        -bodylen 160

    rxreq
    expect req.url == "/overridden_null_maxage"
    txresp -hdr "Cache-Control: max-age=0, s-maxage=5" \
        -bodylen 190


} -start

server s2 {
    rxreq
    expect req.url == "/expires"
    # Expires header is filled directly by the expires_be backend"
    txresp \
        -bodylen 170
} -start

haproxy h1 -conf {
    global
        # WT: limit false-positives causing "HTTP header incomplete" due to
        # idle server connections being randomly used and randomly expiring
        # under us.
        tune.idle-pool.shared off

    defaults
        mode http
        timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
        timeout client  "${HAPROXY_TEST_TIMEOUT-5s}"
        timeout server  "${HAPROXY_TEST_TIMEOUT-5s}"

    frontend fe
        bind "fd@${fe}"
        use_backend expires_be if { path_beg /expires }
        default_backend test

    backend expires_be
        http-request cache-use my_cache
        server www ${s2_addr}:${s2_port}
        http-response set-header X-Cache-Hit %[res.cache_hit]
        # Expires value set in the future (current_time+5s)
        http-response set-header Expires %[date(5),http_date]
        http-response cache-store my_cache

    backend test
        http-request cache-use my_cache
        server www ${s1_addr}:${s1_port}
        http-response cache-store my_cache
        http-response set-header X-Cache-Hit %[res.cache_hit]

    cache my_cache
        total-max-size 3
        max-age 20
        max-object-size 3072
} -start


client c1 -connect ${h1_fe_sock} {
        txreq -url "/max-age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 150

        txreq -url "/max-age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 150
        expect resp.http.X-Cache-Hit == 1

        txreq -url "/s-maxage"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 160

        txreq -url "/s-maxage"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 160
        expect resp.http.X-Cache-Hit == 1

        txreq -url "/expires"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 170

        txreq -url "/expires"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 170
        expect resp.http.X-Cache-Hit == 1

        txreq -url "/last-modified"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 180

        txreq -url "/last-modified"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 180
        expect resp.http.X-Cache-Hit == 1

        txreq -url "/etag"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 190

        txreq -url "/etag"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 190
        expect resp.http.X-Cache-Hit == 1

        # The next response should not be cached
        txreq -url "/uncacheable"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 200

        txreq -url "/uncacheable"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 210
        expect resp.http.X-Cache-Hit == 0

        # Age header tests
        txreq -url "/invalid_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 120
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/invalid_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 120
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/old_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 130
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/old_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 130
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/good_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 140
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/good_age"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 140
        expect resp.http.X-Cache-Hit == 1

        # Cache-Control: no-cache
        txreq -url "/nocache" -hdr "Cache-Control: no-cache"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 140
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/nocache" -hdr "Cache-Control: no-cache"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 140
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/nocache"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 140
        expect resp.http.X-Cache-Hit == 1

        # max-age=0 (control test for the overridden null max-age test below)
        txreq -url "/maxage_zero"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 150
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/maxage_zero"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 150
        expect resp.http.X-Cache-Hit == 0

        # Overridden max-age directive
        txreq -url "/overridden"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 160
        expect resp.http.X-Cache-Hit == 0

        txreq -url "/overridden"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 160
        expect resp.http.X-Cache-Hit == 1

        txreq -url "/overridden_null_maxage"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 190
        expect resp.http.X-Cache-Hit == 0

        # The previous response should have been cached even if it had
        # a max-age=0 since it also had a positive s-maxage
        txreq -url "/overridden_null_maxage"
        rxresp
        expect resp.status == 200
        expect resp.bodylen == 190
        expect resp.http.X-Cache-Hit == 1


} -run