Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 1 | varnishtest "Caching rules test" |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 2 | # 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 Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 4 | |
Christopher Faulet | 85a8136 | 2020-12-15 17:13:39 +0100 | [diff] [blame] | 5 | #REQUIRE_VERSION=2.4 |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 6 | |
| 7 | feature ignore_unknown_macro |
| 8 | |
| 9 | server 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 Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 39 | |
| 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 Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 70 | } -start |
| 71 | |
| 72 | server 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 | |
| 80 | haproxy h1 -conf { |
Willy Tarreau | e1465c1 | 2021-05-09 14:41:41 +0200 | [diff] [blame] | 81 | 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 Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 87 | defaults |
| 88 | mode http |
| 89 | ${no-htx} option http-use-htx |
| 90 | timeout connect 1s |
| 91 | timeout client 1s |
| 92 | timeout server 1s |
| 93 | |
| 94 | frontend fe |
| 95 | bind "fd@${fe}" |
| 96 | use_backend expires_be if { path_beg /expires } |
| 97 | default_backend test |
| 98 | |
| 99 | backend expires_be |
| 100 | http-request cache-use my_cache |
| 101 | server www ${s2_addr}:${s2_port} |
| 102 | http-response set-header X-Cache-Hit %[res.cache_hit] |
| 103 | # Expires value set in the future (current_time+5s) |
| 104 | http-response set-header Expires %[date(5),http_date] |
| 105 | http-response cache-store my_cache |
| 106 | |
| 107 | backend test |
| 108 | http-request cache-use my_cache |
| 109 | server www ${s1_addr}:${s1_port} |
| 110 | http-response cache-store my_cache |
| 111 | http-response set-header X-Cache-Hit %[res.cache_hit] |
| 112 | |
| 113 | cache my_cache |
| 114 | total-max-size 3 |
| 115 | max-age 20 |
| 116 | max-object-size 3072 |
| 117 | } -start |
| 118 | |
| 119 | |
| 120 | client c1 -connect ${h1_fe_sock} { |
| 121 | txreq -url "/max-age" |
| 122 | rxresp |
| 123 | expect resp.status == 200 |
| 124 | expect resp.bodylen == 150 |
| 125 | |
| 126 | txreq -url "/max-age" |
| 127 | rxresp |
| 128 | expect resp.status == 200 |
| 129 | expect resp.bodylen == 150 |
| 130 | expect resp.http.X-Cache-Hit == 1 |
| 131 | |
| 132 | txreq -url "/s-maxage" |
| 133 | rxresp |
| 134 | expect resp.status == 200 |
| 135 | expect resp.bodylen == 160 |
| 136 | |
| 137 | txreq -url "/s-maxage" |
| 138 | rxresp |
| 139 | expect resp.status == 200 |
| 140 | expect resp.bodylen == 160 |
| 141 | expect resp.http.X-Cache-Hit == 1 |
| 142 | |
| 143 | txreq -url "/expires" |
| 144 | rxresp |
| 145 | expect resp.status == 200 |
| 146 | expect resp.bodylen == 170 |
| 147 | |
| 148 | txreq -url "/expires" |
| 149 | rxresp |
| 150 | expect resp.status == 200 |
| 151 | expect resp.bodylen == 170 |
| 152 | expect resp.http.X-Cache-Hit == 1 |
| 153 | |
| 154 | txreq -url "/last-modified" |
| 155 | rxresp |
| 156 | expect resp.status == 200 |
| 157 | expect resp.bodylen == 180 |
| 158 | |
| 159 | txreq -url "/last-modified" |
| 160 | rxresp |
| 161 | expect resp.status == 200 |
| 162 | expect resp.bodylen == 180 |
| 163 | expect resp.http.X-Cache-Hit == 1 |
| 164 | |
| 165 | txreq -url "/etag" |
| 166 | rxresp |
| 167 | expect resp.status == 200 |
| 168 | expect resp.bodylen == 190 |
| 169 | |
| 170 | txreq -url "/etag" |
| 171 | rxresp |
| 172 | expect resp.status == 200 |
| 173 | expect resp.bodylen == 190 |
| 174 | expect resp.http.X-Cache-Hit == 1 |
| 175 | |
| 176 | # The next response should not be cached |
| 177 | txreq -url "/uncacheable" |
| 178 | rxresp |
| 179 | expect resp.status == 200 |
| 180 | expect resp.bodylen == 200 |
| 181 | |
| 182 | txreq -url "/uncacheable" |
| 183 | rxresp |
| 184 | expect resp.status == 200 |
| 185 | expect resp.bodylen == 210 |
| 186 | expect resp.http.X-Cache-Hit == 0 |
| 187 | |
Remi Tricot-Le Breton | 795e141 | 2020-12-03 18:19:29 +0100 | [diff] [blame] | 188 | # Age header tests |
| 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 "/invalid_age" |
| 196 | rxresp |
| 197 | expect resp.status == 200 |
| 198 | expect resp.bodylen == 120 |
| 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 "/old_age" |
| 208 | rxresp |
| 209 | expect resp.status == 200 |
| 210 | expect resp.bodylen == 130 |
| 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 == 0 |
| 218 | |
| 219 | txreq -url "/good_age" |
| 220 | rxresp |
| 221 | expect resp.status == 200 |
| 222 | expect resp.bodylen == 140 |
| 223 | expect resp.http.X-Cache-Hit == 1 |
| 224 | |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 225 | } -run |