|
Lines 1-626
a/LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/request-cache.html_sec1
|
| 1 |
<!doctype html> |
|
|
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<title>Request cache</title> |
| 6 |
<meta name="help" href="https://fetch.spec.whatwg.org/#request"> |
| 7 |
<meta name="timeout" content="long"> |
| 8 |
<script src="/resources/testharness.js"></script> |
| 9 |
<script src="/resources/testharnessreport.js"></script> |
| 10 |
<script src="/common/utils.js"></script> |
| 11 |
<script src="/common/get-host-info.sub.js"></script> |
| 12 |
</head> |
| 13 |
<body> |
| 14 |
<script> |
| 15 |
var now = new Date(); |
| 16 |
/** |
| 17 |
* Each test is run twice: once using etag/If-None-Match and once with |
| 18 |
* date/If-Modified-Since. Each test run gets its own URL and randomized |
| 19 |
* content and operates independently. |
| 20 |
* |
| 21 |
* The test steps are run with request_cache.length fetch requests issued |
| 22 |
* and their immediate results sanity-checked. The cache.py server script |
| 23 |
* stashes an entry containing any If-None-Match, If-Modified-Since, Pragma, |
| 24 |
* and Cache-Control observed headers for each request it receives. When |
| 25 |
* the test fetches have run, this state is retrieved from cache.py and the |
| 26 |
* expected_* lists are checked, including their length. |
| 27 |
* |
| 28 |
* This means that if a request_* fetch is expected to hit the cache and not |
| 29 |
* touch the network, then there will be no entry for it in the expect_* |
| 30 |
* lists. AKA (request_cache.length - expected_validation_headers.length) |
| 31 |
* should equal the number of cache hits that didn't touch the network. |
| 32 |
* |
| 33 |
* Test dictionary keys: |
| 34 |
* - state: required string that determines whether the Expires response for |
| 35 |
* the fetched document should be set in the future ("fresh") or past |
| 36 |
* ("stale"). |
| 37 |
* - vary: optional string to be passed to the server for it to quote back |
| 38 |
* in a Vary header on the response to us. |
| 39 |
* - cache_control: optional string to be passed to the server for it to |
| 40 |
* quote back in a Cache-Control header on the response to us. |
| 41 |
* - redirect: optional string "same-origin" or "cross-origin". If |
| 42 |
* provided, the server will issue an absolute redirect to the script on |
| 43 |
* the same or a different origin, as appropriate. The redirected |
| 44 |
* location is the script with the redirect parameter removed, so the |
| 45 |
* content/state/etc. will be as if you hadn't specified a redirect. |
| 46 |
* - request_cache: required array of cache modes to use (via `cache`). |
| 47 |
* - request_headers: optional array of explicit fetch `headers` arguments. |
| 48 |
* If provided, the server will log an empty dictionary for each request |
| 49 |
* instead of the request headers it would normally log. |
| 50 |
* - response: optional array of specialized response handling. Right now, |
| 51 |
* "error" array entries indicate a network error response is expected |
| 52 |
* which will reject with a TypeError. |
| 53 |
* - expected_validation_headers: required boolean array indicating whether |
| 54 |
* the server should have seen an If-None-Match/If-Modified-Since header |
| 55 |
* in the request. |
| 56 |
* - expected_no_cache_headers: required boolean array indicating whether |
| 57 |
* the server should have seen Pragma/Cache-control:no-cache headers in |
| 58 |
* the request. |
| 59 |
* - expected_max_age_headers: optional boolean array indicating whether |
| 60 |
* the server should have seen a Cache-Control:max-age=0 header in the |
| 61 |
* request. |
| 62 |
*/ |
| 63 |
var tests = [ |
| 64 |
{ |
| 65 |
name: 'RequestCache "default" mode checks the cache for previously cached content and goes to the network for stale responses', |
| 66 |
state: "stale", |
| 67 |
request_cache: ["default", "default"], |
| 68 |
expected_validation_headers: [false, true], |
| 69 |
expected_no_cache_headers: [false, false], |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: 'RequestCache "default" mode checks the cache for previously cached content and avoids going to the network if a fresh response exists', |
| 73 |
state: "fresh", |
| 74 |
request_cache: ["default", "default"], |
| 75 |
expected_validation_headers: [false], |
| 76 |
expected_no_cache_headers: [false], |
| 77 |
}, |
| 78 |
{ |
| 79 |
name: 'RequestCache "no-cache" mode revalidates stale responses found in the cache', |
| 80 |
state: "stale", |
| 81 |
request_cache: ["default", "no-cache"], |
| 82 |
expected_validation_headers: [false, true], |
| 83 |
expected_no_cache_headers: [false, false], |
| 84 |
expected_max_age_headers: [false, true], |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: 'RequestCache "no-cache" mode revalidates fresh responses found in the cache', |
| 88 |
state: "fresh", |
| 89 |
request_cache: ["default", "no-cache"], |
| 90 |
expected_validation_headers: [false, true], |
| 91 |
expected_no_cache_headers: [false, false], |
| 92 |
expected_max_age_headers: [false, true], |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and avoid revalidation for stale responses', |
| 96 |
state: "stale", |
| 97 |
request_cache: ["default", "force-cache"], |
| 98 |
expected_validation_headers: [false], |
| 99 |
expected_no_cache_headers: [false], |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and avoid revalidation for fresh responses', |
| 103 |
state: "fresh", |
| 104 |
request_cache: ["default", "force-cache"], |
| 105 |
expected_validation_headers: [false], |
| 106 |
expected_no_cache_headers: [false], |
| 107 |
}, |
| 108 |
{ |
| 109 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and goes to the network if a cached response is not found', |
| 110 |
state: "stale", |
| 111 |
request_cache: ["force-cache"], |
| 112 |
expected_validation_headers: [false], |
| 113 |
expected_no_cache_headers: [false], |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and goes to the network if a cached response is not found', |
| 117 |
state: "fresh", |
| 118 |
request_cache: ["force-cache"], |
| 119 |
expected_validation_headers: [false], |
| 120 |
expected_no_cache_headers: [false], |
| 121 |
}, |
| 122 |
{ |
| 123 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and goes to the network if a cached response would vary', |
| 124 |
state: "stale", |
| 125 |
vary: "*", |
| 126 |
request_cache: ["default", "force-cache"], |
| 127 |
expected_validation_headers: [false, true], |
| 128 |
expected_no_cache_headers: [false, false], |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: 'RequestCache "force-cache" mode checks the cache for previously cached content and goes to the network if a cached response would vary', |
| 132 |
state: "fresh", |
| 133 |
vary: "*", |
| 134 |
request_cache: ["default", "force-cache"], |
| 135 |
expected_validation_headers: [false, true], |
| 136 |
expected_no_cache_headers: [false, false], |
| 137 |
}, |
| 138 |
{ |
| 139 |
name: 'RequestCache "force-cache" stores the response in the cache if it goes to the network', |
| 140 |
state: "stale", |
| 141 |
request_cache: ["force-cache", "default"], |
| 142 |
expected_validation_headers: [false, true], |
| 143 |
expected_no_cache_headers: [false, false], |
| 144 |
}, |
| 145 |
{ |
| 146 |
name: 'RequestCache "force-cache" stores the response in the cache if it goes to the network', |
| 147 |
state: "fresh", |
| 148 |
request_cache: ["force-cache", "default"], |
| 149 |
expected_validation_headers: [false], |
| 150 |
expected_no_cache_headers: [false], |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: 'RequestCache "only-if-cached" mode checks the cache for previously cached content and avoids revalidation for stale responses', |
| 154 |
state: "stale", |
| 155 |
request_cache: ["default", "only-if-cached"], |
| 156 |
expected_validation_headers: [false], |
| 157 |
expected_no_cache_headers: [false] |
| 158 |
}, |
| 159 |
{ |
| 160 |
name: 'RequestCache "only-if-cached" mode checks the cache for previously cached content and avoids revalidation for fresh responses', |
| 161 |
state: "fresh", |
| 162 |
request_cache: ["default", "only-if-cached"], |
| 163 |
expected_validation_headers: [false], |
| 164 |
expected_no_cache_headers: [false] |
| 165 |
}, |
| 166 |
{ |
| 167 |
name: 'RequestCache "only-if-cached" mode checks the cache for previously cached content and does not go to the network if a cached response is not found', |
| 168 |
state: "fresh", |
| 169 |
request_cache: ["only-if-cached"], |
| 170 |
response: ["error"], |
| 171 |
expected_validation_headers: [], |
| 172 |
expected_no_cache_headers: [] |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: 'RequestCache "only-if-cached" (with "same-origin") uses cached same-origin redirects to same-origin content', |
| 176 |
state: "fresh", |
| 177 |
request_cache: ["default", "only-if-cached"], |
| 178 |
redirect: "same-origin", |
| 179 |
expected_validation_headers: [false, false], |
| 180 |
expected_no_cache_headers: [false, false], |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: 'RequestCache "only-if-cached" (with "same-origin") uses cached same-origin redirects to same-origin content', |
| 184 |
state: "stale", |
| 185 |
request_cache: ["default", "only-if-cached"], |
| 186 |
redirect: "same-origin", |
| 187 |
expected_validation_headers: [false, false], |
| 188 |
expected_no_cache_headers: [false, false], |
| 189 |
}, |
| 190 |
{ |
| 191 |
name: 'RequestCache "only-if-cached" (with "same-origin") does not follow redirects across origins and rejects', |
| 192 |
state: "fresh", |
| 193 |
request_cache: ["default", "only-if-cached"], |
| 194 |
redirect: "cross-origin", |
| 195 |
response: [null, "error"], |
| 196 |
expected_validation_headers: [false, false], |
| 197 |
expected_no_cache_headers: [false, false], |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: 'RequestCache "only-if-cached" (with "same-origin") does not follow redirects across origins and rejects', |
| 201 |
state: "stale", |
| 202 |
request_cache: ["default", "only-if-cached"], |
| 203 |
redirect: "cross-origin", |
| 204 |
response: [null, "error"], |
| 205 |
expected_validation_headers: [false, false], |
| 206 |
expected_no_cache_headers: [false, false], |
| 207 |
}, |
| 208 |
{ |
| 209 |
name: 'RequestCache "no-store" mode does not check the cache for previously cached content and goes to the network regardless', |
| 210 |
state: "stale", |
| 211 |
request_cache: ["default", "no-store"], |
| 212 |
expected_validation_headers: [false, false], |
| 213 |
expected_no_cache_headers: [false, true], |
| 214 |
}, |
| 215 |
{ |
| 216 |
name: 'RequestCache "no-store" mode does not check the cache for previously cached content and goes to the network regardless', |
| 217 |
state: "fresh", |
| 218 |
request_cache: ["default", "no-store"], |
| 219 |
expected_validation_headers: [false, false], |
| 220 |
expected_no_cache_headers: [false, true], |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: 'RequestCache "no-store" mode does not store the response in the cache', |
| 224 |
state: "stale", |
| 225 |
request_cache: ["no-store", "default"], |
| 226 |
expected_validation_headers: [false, false], |
| 227 |
expected_no_cache_headers: [true, false], |
| 228 |
}, |
| 229 |
{ |
| 230 |
name: 'RequestCache "no-store" mode does not store the response in the cache', |
| 231 |
state: "fresh", |
| 232 |
request_cache: ["no-store", "default"], |
| 233 |
expected_validation_headers: [false, false], |
| 234 |
expected_no_cache_headers: [true, false], |
| 235 |
}, |
| 236 |
{ |
| 237 |
name: 'RequestCache "default" mode with an If-Modified-Since header is treated similarly to "no-store"', |
| 238 |
state: "stale", |
| 239 |
request_cache: ["default", "default"], |
| 240 |
request_headers: [{}, {"If-Modified-Since": now.toGMTString()}], |
| 241 |
expected_validation_headers: [false, false], |
| 242 |
expected_no_cache_headers: [false, true], |
| 243 |
}, |
| 244 |
{ |
| 245 |
name: 'RequestCache "default" mode with an If-Modified-Since header is treated similarly to "no-store"', |
| 246 |
state: "fresh", |
| 247 |
request_cache: ["default", "default"], |
| 248 |
request_headers: [{}, {"If-Modified-Since": now.toGMTString()}], |
| 249 |
expected_validation_headers: [false, false], |
| 250 |
expected_no_cache_headers: [false, true], |
| 251 |
}, |
| 252 |
{ |
| 253 |
name: 'RequestCache "default" mode with an If-Modified-Since header is treated similarly to "no-store"', |
| 254 |
state: "stale", |
| 255 |
request_cache: ["default", "default"], |
| 256 |
request_headers: [{"If-Modified-Since": now.toGMTString()}, {}], |
| 257 |
expected_validation_headers: [false, false], |
| 258 |
expected_no_cache_headers: [true, false], |
| 259 |
}, |
| 260 |
{ |
| 261 |
name: 'RequestCache "default" mode with an If-Modified-Since header is treated similarly to "no-store"', |
| 262 |
state: "fresh", |
| 263 |
request_cache: ["default", "default"], |
| 264 |
request_headers: [{"If-Modified-Since": now.toGMTString()}, {}], |
| 265 |
expected_validation_headers: [false, false], |
| 266 |
expected_no_cache_headers: [true, false], |
| 267 |
}, |
| 268 |
{ |
| 269 |
name: 'RequestCache "default" mode with an If-None-Match header is treated similarly to "no-store"', |
| 270 |
state: "stale", |
| 271 |
request_cache: ["default", "default"], |
| 272 |
request_headers: [{}, {"If-None-Match": '"foo"'}], |
| 273 |
expected_validation_headers: [false, false], |
| 274 |
expected_no_cache_headers: [false, true], |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: 'RequestCache "default" mode with an If-None-Match header is treated similarly to "no-store"', |
| 278 |
state: "fresh", |
| 279 |
request_cache: ["default", "default"], |
| 280 |
request_headers: [{}, {"If-None-Match": '"foo"'}], |
| 281 |
expected_validation_headers: [false, false], |
| 282 |
expected_no_cache_headers: [false, true], |
| 283 |
}, |
| 284 |
{ |
| 285 |
name: 'RequestCache "default" mode with an If-None-Match header is treated similarly to "no-store"', |
| 286 |
state: "stale", |
| 287 |
request_cache: ["default", "default"], |
| 288 |
request_headers: [{"If-None-Match": '"foo"'}, {}], |
| 289 |
expected_validation_headers: [false, false], |
| 290 |
expected_no_cache_headers: [true, false], |
| 291 |
}, |
| 292 |
{ |
| 293 |
name: 'RequestCache "default" mode with an If-None-Match header is treated similarly to "no-store"', |
| 294 |
state: "fresh", |
| 295 |
request_cache: ["default", "default"], |
| 296 |
request_headers: [{"If-None-Match": '"foo"'}, {}], |
| 297 |
expected_validation_headers: [false, false], |
| 298 |
expected_no_cache_headers: [true, false], |
| 299 |
}, |
| 300 |
{ |
| 301 |
name: 'RequestCache "default" mode with an If-Unmodified-Since header is treated similarly to "no-store"', |
| 302 |
state: "stale", |
| 303 |
request_cache: ["default", "default"], |
| 304 |
request_headers: [{}, {"If-Unmodified-Since": now.toGMTString()}], |
| 305 |
expected_validation_headers: [false, false], |
| 306 |
expected_no_cache_headers: [false, true], |
| 307 |
}, |
| 308 |
{ |
| 309 |
name: 'RequestCache "default" mode with an If-Unmodified-Since header is treated similarly to "no-store"', |
| 310 |
state: "fresh", |
| 311 |
request_cache: ["default", "default"], |
| 312 |
request_headers: [{}, {"If-Unmodified-Since": now.toGMTString()}], |
| 313 |
expected_validation_headers: [false, false], |
| 314 |
expected_no_cache_headers: [false, true], |
| 315 |
}, |
| 316 |
{ |
| 317 |
name: 'RequestCache "default" mode with an If-Unmodified-Since header is treated similarly to "no-store"', |
| 318 |
state: "stale", |
| 319 |
request_cache: ["default", "default"], |
| 320 |
request_headers: [{"If-Unmodified-Since": now.toGMTString()}, {}], |
| 321 |
expected_validation_headers: [false, false], |
| 322 |
expected_no_cache_headers: [true, false], |
| 323 |
}, |
| 324 |
{ |
| 325 |
name: 'RequestCache "default" mode with an If-Unmodified-Since header is treated similarly to "no-store"', |
| 326 |
state: "fresh", |
| 327 |
request_cache: ["default", "default"], |
| 328 |
request_headers: [{"If-Unmodified-Since": now.toGMTString()}, {}], |
| 329 |
expected_validation_headers: [false, false], |
| 330 |
expected_no_cache_headers: [true, false], |
| 331 |
}, |
| 332 |
{ |
| 333 |
name: 'RequestCache "default" mode with an If-Match header is treated similarly to "no-store"', |
| 334 |
state: "stale", |
| 335 |
request_cache: ["default", "default"], |
| 336 |
request_headers: [{}, {"If-Match": '"foo"'}], |
| 337 |
expected_validation_headers: [false, false], |
| 338 |
expected_no_cache_headers: [false, true], |
| 339 |
}, |
| 340 |
{ |
| 341 |
name: 'RequestCache "default" mode with an If-Match header is treated similarly to "no-store"', |
| 342 |
state: "fresh", |
| 343 |
request_cache: ["default", "default"], |
| 344 |
request_headers: [{}, {"If-Match": '"foo"'}], |
| 345 |
expected_validation_headers: [false, false], |
| 346 |
expected_no_cache_headers: [false, true], |
| 347 |
}, |
| 348 |
{ |
| 349 |
name: 'RequestCache "default" mode with an If-Match header is treated similarly to "no-store"', |
| 350 |
state: "stale", |
| 351 |
request_cache: ["default", "default"], |
| 352 |
request_headers: [{"If-Match": '"foo"'}, {}], |
| 353 |
expected_validation_headers: [false, false], |
| 354 |
expected_no_cache_headers: [true, false], |
| 355 |
}, |
| 356 |
{ |
| 357 |
name: 'RequestCache "default" mode with an If-Match header is treated similarly to "no-store"', |
| 358 |
state: "fresh", |
| 359 |
request_cache: ["default", "default"], |
| 360 |
request_headers: [{"If-Match": '"foo"'}, {}], |
| 361 |
expected_validation_headers: [false, false], |
| 362 |
expected_no_cache_headers: [true, false], |
| 363 |
}, |
| 364 |
{ |
| 365 |
name: 'RequestCache "default" mode with an If-Range header is treated similarly to "no-store"', |
| 366 |
state: "stale", |
| 367 |
request_cache: ["default", "default"], |
| 368 |
request_headers: [{}, {"If-Range": '"foo"'}], |
| 369 |
expected_validation_headers: [false, false], |
| 370 |
expected_no_cache_headers: [false, true], |
| 371 |
}, |
| 372 |
{ |
| 373 |
name: 'RequestCache "default" mode with an If-Range header is treated similarly to "no-store"', |
| 374 |
state: "fresh", |
| 375 |
request_cache: ["default", "default"], |
| 376 |
request_headers: [{}, {"If-Range": '"foo"'}], |
| 377 |
expected_validation_headers: [false, false], |
| 378 |
expected_no_cache_headers: [false, true], |
| 379 |
}, |
| 380 |
{ |
| 381 |
name: 'RequestCache "default" mode with an If-Range header is treated similarly to "no-store"', |
| 382 |
state: "stale", |
| 383 |
request_cache: ["default", "default"], |
| 384 |
request_headers: [{"If-Range": '"foo"'}, {}], |
| 385 |
expected_validation_headers: [false, false], |
| 386 |
expected_no_cache_headers: [true, false], |
| 387 |
}, |
| 388 |
{ |
| 389 |
name: 'RequestCache "default" mode with an If-Range header is treated similarly to "no-store"', |
| 390 |
state: "fresh", |
| 391 |
request_cache: ["default", "default"], |
| 392 |
request_headers: [{"If-Range": '"foo"'}, {}], |
| 393 |
expected_validation_headers: [false, false], |
| 394 |
expected_no_cache_headers: [true, false], |
| 395 |
}, |
| 396 |
{ |
| 397 |
name: 'Responses with the "Cache-Control: no-store" header are not stored in the cache', |
| 398 |
state: "stale", |
| 399 |
cache_control: "no-store", |
| 400 |
request_cache: ["default", "default"], |
| 401 |
expected_validation_headers: [false, false], |
| 402 |
expected_no_cache_headers: [false, false], |
| 403 |
}, |
| 404 |
{ |
| 405 |
name: 'Responses with the "Cache-Control: no-store" header are not stored in the cache', |
| 406 |
state: "fresh", |
| 407 |
cache_control: "no-store", |
| 408 |
request_cache: ["default", "default"], |
| 409 |
expected_validation_headers: [false, false], |
| 410 |
expected_no_cache_headers: [false, false], |
| 411 |
}, |
| 412 |
{ |
| 413 |
name: 'RequestCache "reload" mode does not check the cache for previously cached content and goes to the network regardless', |
| 414 |
state: "stale", |
| 415 |
request_cache: ["default", "reload"], |
| 416 |
expected_validation_headers: [false, false], |
| 417 |
expected_no_cache_headers: [false, true], |
| 418 |
}, |
| 419 |
{ |
| 420 |
name: 'RequestCache "reload" mode does not check the cache for previously cached content and goes to the network regardless', |
| 421 |
state: "fresh", |
| 422 |
request_cache: ["default", "reload"], |
| 423 |
expected_validation_headers: [false, false], |
| 424 |
expected_no_cache_headers: [false, true], |
| 425 |
}, |
| 426 |
{ |
| 427 |
name: 'RequestCache "reload" mode does store the response in the cache', |
| 428 |
state: "stale", |
| 429 |
request_cache: ["reload", "default"], |
| 430 |
expected_validation_headers: [false, true], |
| 431 |
expected_no_cache_headers: [true, false], |
| 432 |
}, |
| 433 |
{ |
| 434 |
name: 'RequestCache "reload" mode does store the response in the cache', |
| 435 |
state: "fresh", |
| 436 |
request_cache: ["reload", "default"], |
| 437 |
expected_validation_headers: [false], |
| 438 |
expected_no_cache_headers: [true], |
| 439 |
}, |
| 440 |
{ |
| 441 |
name: 'RequestCache "reload" mode does store the response in the cache even if a previous response is already stored', |
| 442 |
state: "stale", |
| 443 |
request_cache: ["default", "reload", "default"], |
| 444 |
expected_validation_headers: [false, false, true], |
| 445 |
expected_no_cache_headers: [false, true, false], |
| 446 |
}, |
| 447 |
{ |
| 448 |
name: 'RequestCache "reload" mode does store the response in the cache even if a previous response is already stored', |
| 449 |
state: "fresh", |
| 450 |
request_cache: ["default", "reload", "default"], |
| 451 |
expected_validation_headers: [false, false], |
| 452 |
expected_no_cache_headers: [false, true], |
| 453 |
}, |
| 454 |
]; |
| 455 |
function base_path() { |
| 456 |
return location.pathname.replace(/\/[^\/]*$/, '/'); |
| 457 |
} |
| 458 |
function make_url(uuid, id, value, content, info) { |
| 459 |
var dates = { |
| 460 |
fresh: new Date(now.getFullYear() + 1, now.getMonth(), now.getDay()).toGMTString(), |
| 461 |
stale: new Date(now.getFullYear() - 1, now.getMonth(), now.getDay()).toGMTString(), |
| 462 |
}; |
| 463 |
var vary = ""; |
| 464 |
if ("vary" in info) { |
| 465 |
vary = "&vary=" + info.vary; |
| 466 |
} |
| 467 |
var cache_control = ""; |
| 468 |
if ("cache_control" in info) { |
| 469 |
cache_control = "&cache_control=" + info.cache_control; |
| 470 |
} |
| 471 |
var redirect = ""; |
| 472 |
|
| 473 |
var ignore_request_headers = ""; |
| 474 |
if ("request_headers" in info) { |
| 475 |
// Ignore the request headers that we send since they may be synthesized by the test. |
| 476 |
ignore_request_headers = "&ignore"; |
| 477 |
} |
| 478 |
var url_sans_redirect = "resources/cache.py?token=" + uuid + |
| 479 |
"&content=" + content + |
| 480 |
"&" + id + "=" + value + |
| 481 |
"&expires=" + dates[info.state] + |
| 482 |
vary + cache_control + ignore_request_headers; |
| 483 |
// If there's a redirect, the target is the script without any redirect at |
| 484 |
// either the same domain or a different domain. |
| 485 |
if ("redirect" in info) { |
| 486 |
var host_info = get_host_info(); |
| 487 |
var origin; |
| 488 |
switch (info.redirect) { |
| 489 |
case "same-origin": |
| 490 |
origin = host_info['HTTP_ORIGIN']; |
| 491 |
break; |
| 492 |
case "cross-origin": |
| 493 |
origin = host_info['HTTP_REMOTE_ORIGIN']; |
| 494 |
break; |
| 495 |
} |
| 496 |
var redirected_url = origin + base_path() + url_sans_redirect; |
| 497 |
return url_sans_redirect + "&redirect=" + encodeURIComponent(redirected_url); |
| 498 |
} else { |
| 499 |
return url_sans_redirect; |
| 500 |
} |
| 501 |
} |
| 502 |
function expected_status(type, identifier, init) { |
| 503 |
if (type == "date" && |
| 504 |
init.headers && |
| 505 |
init.headers["If-Modified-Since"] == identifier) { |
| 506 |
// The server will respond with a 304 in this case. |
| 507 |
return [304, "Not Modified"]; |
| 508 |
} |
| 509 |
return [200, "OK"]; |
| 510 |
} |
| 511 |
function expected_response_text(type, identifier, init, content) { |
| 512 |
if (type == "date" && |
| 513 |
init.headers && |
| 514 |
init.headers["If-Modified-Since"] == identifier) { |
| 515 |
// The server will respond with a 304 in this case. |
| 516 |
return ""; |
| 517 |
} |
| 518 |
return content; |
| 519 |
} |
| 520 |
function server_state(uuid) { |
| 521 |
return fetch("resources/cache.py?querystate&token=" + uuid) |
| 522 |
.then(function(response) { |
| 523 |
return response.text(); |
| 524 |
}).then(function(text) { |
| 525 |
// null will be returned if the server never received any requests |
| 526 |
// for the given uuid. Normalize that to an empty list consistent |
| 527 |
// with our representation. |
| 528 |
return JSON.parse(text) || []; |
| 529 |
}); |
| 530 |
} |
| 531 |
function make_test(type, info) { |
| 532 |
return function(test) { |
| 533 |
var uuid = token(); |
| 534 |
var identifier = (type == "tag" ? Math.random() : now.toGMTString()); |
| 535 |
var content = Math.random().toString(); |
| 536 |
var url = make_url(uuid, type, identifier, content, info); |
| 537 |
var fetch_functions = []; |
| 538 |
for (var i = 0; i < info.request_cache.length; ++i) { |
| 539 |
fetch_functions.push(function(idx) { |
| 540 |
var init = {cache: info.request_cache[idx]}; |
| 541 |
if ("request_headers" in info) { |
| 542 |
init.headers = info.request_headers[idx]; |
| 543 |
} |
| 544 |
if (init.cache === "only-if-cached") { |
| 545 |
// only-if-cached requires we use same-origin mode. |
| 546 |
init.mode = "same-origin"; |
| 547 |
} |
| 548 |
return fetch(url, init) |
| 549 |
.then(function(response) { |
| 550 |
if ("response" in info && info.response[idx] === "error") { |
| 551 |
assert_true(false, "fetch should have been an error"); |
| 552 |
return; |
| 553 |
} |
| 554 |
assert_array_equals([response.status, response.statusText], |
| 555 |
expected_status(type, identifier, init)); |
| 556 |
return response.text(); |
| 557 |
}).then(function(text) { |
| 558 |
assert_equals(text, expected_response_text(type, identifier, init, content)); |
| 559 |
}, function(reason) { |
| 560 |
if ("response" in info && info.response[idx] === "error") { |
| 561 |
assert_throws(new TypeError(), function() { throw reason; }); |
| 562 |
} else { |
| 563 |
throw reason; |
| 564 |
} |
| 565 |
}); |
| 566 |
}); |
| 567 |
} |
| 568 |
var i = 0; |
| 569 |
function run_next_step() { |
| 570 |
if (fetch_functions.length) { |
| 571 |
return fetch_functions.shift()(i++) |
| 572 |
.then(run_next_step); |
| 573 |
} else { |
| 574 |
return Promise.resolve(); |
| 575 |
} |
| 576 |
} |
| 577 |
return run_next_step() |
| 578 |
.then(function() { |
| 579 |
// Now, query the server state |
| 580 |
return server_state(uuid); |
| 581 |
}).then(function(state) { |
| 582 |
var expectedState = []; |
| 583 |
info.expected_validation_headers.forEach(function (validate) { |
| 584 |
if (validate) { |
| 585 |
if (type == "tag") { |
| 586 |
expectedState.push({"If-None-Match": '"' + identifier + '"'}); |
| 587 |
} else { |
| 588 |
expectedState.push({"If-Modified-Since": identifier}); |
| 589 |
} |
| 590 |
} else { |
| 591 |
expectedState.push({}); |
| 592 |
} |
| 593 |
}); |
| 594 |
for (var i = 0; i < info.expected_no_cache_headers.length; ++i) { |
| 595 |
if (info.expected_no_cache_headers[i]) { |
| 596 |
expectedState[i]["Pragma"] = "no-cache"; |
| 597 |
expectedState[i]["Cache-Control"] = "no-cache"; |
| 598 |
} |
| 599 |
} |
| 600 |
if ("expected_max_age_headers" in info) { |
| 601 |
for (var i = 0; i < info.expected_max_age_headers.length; ++i) { |
| 602 |
if (info.expected_max_age_headers[i]) { |
| 603 |
expectedState[i]["Cache-Control"] = "max-age=0"; |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
assert_equals(state.length, expectedState.length); |
| 608 |
for (var i = 0; i < state.length; ++i) { |
| 609 |
for (var header in state[i]) { |
| 610 |
assert_equals(state[i][header], expectedState[i][header]); |
| 611 |
delete expectedState[i][header]; |
| 612 |
} |
| 613 |
for (var header in expectedState[i]) { |
| 614 |
assert_false(header in state[i]); |
| 615 |
} |
| 616 |
} |
| 617 |
}); |
| 618 |
}; |
| 619 |
} |
| 620 |
tests.forEach(function(info) { |
| 621 |
promise_test(make_test("tag", info), info.name + " with Etag and " + info.state + " response"); |
| 622 |
promise_test(make_test("date", info), info.name + " with date and " + info.state + " response"); |
| 623 |
}); |
| 624 |
</script> |
| 625 |
</body> |
| 626 |
</html> |