blob: 1abd92439cb99404282003efc4de15f93fa212f2 [file] [log] [blame]
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01001varnishtest "Caching rules test"
2# A respnse will not be cached unless it has an explicit age (Cache-Control max-age of s-maxage, Expires, Last-Modified headers, or ETag)
3
4#REQUIRE_VERSION=1.9
5
6feature ignore_unknown_macro
7
8server s1 {
9 rxreq
10 expect req.url == "/max-age"
11 txresp -hdr "Cache-Control: max-age=5" \
12 -bodylen 150
13
14 rxreq
15 expect req.url == "/s-maxage"
16 txresp -hdr "Cache-Control: s-maxage=5" \
17 -bodylen 160
18
19 rxreq
20 expect req.url == "/last-modified"
21 txresp -hdr "Last-Modified: Thu, 22 Oct 2020 16:51:12 GMT" \
22 -bodylen 180
23
24 rxreq
25 expect req.url == "/etag"
26 txresp -hdr "ETag: \"etag\"" \
27 -bodylen 190
28
29 rxreq
30 expect req.url == "/uncacheable"
31 txresp \
32 -bodylen 200
33
34 rxreq
35 expect req.url == "/uncacheable"
36 txresp \
37 -bodylen 210
38} -start
39
40server s2 {
41 rxreq
42 expect req.url == "/expires"
43 # Expires header is filled directly by the expires_be backend"
44 txresp \
45 -bodylen 170
46} -start
47
48haproxy h1 -conf {
49 defaults
50 mode http
51 ${no-htx} option http-use-htx
52 timeout connect 1s
53 timeout client 1s
54 timeout server 1s
55
56 frontend fe
57 bind "fd@${fe}"
58 use_backend expires_be if { path_beg /expires }
59 default_backend test
60
61 backend expires_be
62 http-request cache-use my_cache
63 server www ${s2_addr}:${s2_port}
64 http-response set-header X-Cache-Hit %[res.cache_hit]
65 # Expires value set in the future (current_time+5s)
66 http-response set-header Expires %[date(5),http_date]
67 http-response cache-store my_cache
68
69 backend test
70 http-request cache-use my_cache
71 server www ${s1_addr}:${s1_port}
72 http-response cache-store my_cache
73 http-response set-header X-Cache-Hit %[res.cache_hit]
74
75 cache my_cache
76 total-max-size 3
77 max-age 20
78 max-object-size 3072
79} -start
80
81
82client c1 -connect ${h1_fe_sock} {
83 txreq -url "/max-age"
84 rxresp
85 expect resp.status == 200
86 expect resp.bodylen == 150
87
88 txreq -url "/max-age"
89 rxresp
90 expect resp.status == 200
91 expect resp.bodylen == 150
92 expect resp.http.X-Cache-Hit == 1
93
94 txreq -url "/s-maxage"
95 rxresp
96 expect resp.status == 200
97 expect resp.bodylen == 160
98
99 txreq -url "/s-maxage"
100 rxresp
101 expect resp.status == 200
102 expect resp.bodylen == 160
103 expect resp.http.X-Cache-Hit == 1
104
105 txreq -url "/expires"
106 rxresp
107 expect resp.status == 200
108 expect resp.bodylen == 170
109
110 txreq -url "/expires"
111 rxresp
112 expect resp.status == 200
113 expect resp.bodylen == 170
114 expect resp.http.X-Cache-Hit == 1
115
116 txreq -url "/last-modified"
117 rxresp
118 expect resp.status == 200
119 expect resp.bodylen == 180
120
121 txreq -url "/last-modified"
122 rxresp
123 expect resp.status == 200
124 expect resp.bodylen == 180
125 expect resp.http.X-Cache-Hit == 1
126
127 txreq -url "/etag"
128 rxresp
129 expect resp.status == 200
130 expect resp.bodylen == 190
131
132 txreq -url "/etag"
133 rxresp
134 expect resp.status == 200
135 expect resp.bodylen == 190
136 expect resp.http.X-Cache-Hit == 1
137
138 # The next response should not be cached
139 txreq -url "/uncacheable"
140 rxresp
141 expect resp.status == 200
142 expect resp.bodylen == 200
143
144 txreq -url "/uncacheable"
145 rxresp
146 expect resp.status == 200
147 expect resp.bodylen == 210
148 expect resp.http.X-Cache-Hit == 0
149
150} -run