| OLD | NEW |
| 1 if (self.importScripts) { | 1 if (self.importScripts) { |
| 2 importScripts('/resources/testharness.js'); | 2 importScripts('/resources/testharness.js'); |
| 3 importScripts('/resources/testharness-helpers.js'); | 3 importScripts('/resources/testharness-helpers.js'); |
| 4 importScripts('../resources/test-helpers.js'); | 4 importScripts('../resources/test-helpers.js'); |
| 5 } | 5 } |
| 6 | 6 |
| 7 // A set of Request/Response pairs to be used with prepopulated_cache_test(). | |
| 8 var simple_entries = [ | |
| 9 { | |
| 10 name: 'a', | |
| 11 request: new Request('http://example.com/a'), | |
| 12 response: new Response('') | |
| 13 }, | |
| 14 | |
| 15 { | |
| 16 name: 'b', | |
| 17 request: new Request('http://example.com/b'), | |
| 18 response: new Response('') | |
| 19 }, | |
| 20 | |
| 21 { | |
| 22 name: 'a_with_query', | |
| 23 request: new Request('http://example.com/a?q=r'), | |
| 24 response: new Response('') | |
| 25 }, | |
| 26 | |
| 27 { | |
| 28 name: 'A', | |
| 29 request: new Request('http://example.com/A'), | |
| 30 response: new Response('') | |
| 31 }, | |
| 32 | |
| 33 { | |
| 34 name: 'a_https', | |
| 35 request: new Request('https://example.com/a'), | |
| 36 response: new Response('') | |
| 37 }, | |
| 38 | |
| 39 { | |
| 40 name: 'a_org', | |
| 41 request: new Request('http://example.org/a'), | |
| 42 response: new Response('') | |
| 43 }, | |
| 44 | |
| 45 { | |
| 46 name: 'cat', | |
| 47 request: new Request('http://example.com/cat'), | |
| 48 response: new Response('') | |
| 49 }, | |
| 50 | |
| 51 { | |
| 52 name: 'catmandu', | |
| 53 request: new Request('http://example.com/catmandu'), | |
| 54 response: new Response('') | |
| 55 }, | |
| 56 | |
| 57 { | |
| 58 name: 'cat_num_lives', | |
| 59 request: new Request('http://example.com/cat?lives=9'), | |
| 60 response: new Response('') | |
| 61 }, | |
| 62 | |
| 63 { | |
| 64 name: 'cat_in_the_hat', | |
| 65 request: new Request('http://example.com/cat/in/the/hat'), | |
| 66 response: new Response('') | |
| 67 }, | |
| 68 | |
| 69 { | |
| 70 name: 'secret_cat', | |
| 71 request: new Request('http://tom:jerry@example.com/cat'), | |
| 72 response: new Response('') | |
| 73 }, | |
| 74 | |
| 75 { | |
| 76 name: 'top_secret_cat', | |
| 77 request: new Request('http://tom:j3rry@example.com/cat'), | |
| 78 response: new Response('') | |
| 79 }, | |
| 80 { | |
| 81 name: 'non_2xx_response', | |
| 82 request: new Request('http://example.com/non2xx'), | |
| 83 response: new Response('', {status: 404, statusText: 'nope'}) | |
| 84 }, | |
| 85 | |
| 86 { | |
| 87 name: 'error_response', | |
| 88 request: new Request('http://example.com/error'), | |
| 89 response: Response.error() | |
| 90 }, | |
| 91 ]; | |
| 92 | |
| 93 // A set of Request/Response pairs to be used with prepopulated_cache_test(). | |
| 94 // These contain a mix of test cases that use Vary headers. | |
| 95 var vary_entries = [ | |
| 96 { | |
| 97 name: 'vary_cookie_is_cookie', | |
| 98 request: new Request('http://example.com/c', | |
| 99 {headers: {'Cookies': 'is-for-cookie'}}), | |
| 100 response: new Response('', | |
| 101 {headers: {'Vary': 'Cookies'}}) | |
| 102 }, | |
| 103 | |
| 104 { | |
| 105 name: 'vary_cookie_is_good', | |
| 106 request: new Request('http://example.com/c', | |
| 107 {headers: {'Cookies': 'is-good-enough-for-me'}}), | |
| 108 response: new Response('', | |
| 109 {headers: {'Vary': 'Cookies'}}) | |
| 110 }, | |
| 111 | |
| 112 { | |
| 113 name: 'vary_cookie_absent', | |
| 114 request: new Request('http://example.com/c'), | |
| 115 response: new Response('', | |
| 116 {headers: {'Vary': 'Cookies'}}) | |
| 117 }, | |
| 118 | |
| 119 { | |
| 120 name: 'vary_wildcard', | |
| 121 request: new Request('http://example.com/c', | |
| 122 {headers: {'Cookies': 'x', 'X-Key': '1'}}), | |
| 123 response: new Response('', | |
| 124 {headers: {'Vary': '*'}}) | |
| 125 } | |
| 126 ]; | |
| 127 | |
| 128 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 129 return cache.matchAll('not-present-in-the-cache') | |
| 130 .then(function(result) { | |
| 131 assert_array_equivalent( | |
| 132 result, [], | |
| 133 'Cache.matchAll should resolve with an empty array on failure.'); | |
| 134 }); | |
| 135 }, 'Cache.matchAll with no matching entries'); | |
| 136 | |
| 137 prepopulated_cache_test(simple_entries, function(cache, entries) { | 7 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 138 return cache.match('not-present-in-the-cache') | 8 return cache.match('not-present-in-the-cache') |
| 139 .then(function(result) { | 9 .then(function(result) { |
| 140 assert_equals(result, undefined, | 10 assert_equals(result, undefined, |
| 141 'Cache.match failures should resolve with undefined.'); | 11 'Cache.match failures should resolve with undefined.'); |
| 142 }); | 12 }); |
| 143 }, 'Cache.match with no matching entries'); | 13 }, 'Cache.match with no matching entries'); |
| 144 | 14 |
| 145 prepopulated_cache_test(simple_entries, function(cache, entries) { | 15 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 146 return cache.matchAll(entries.a.request.url) | |
| 147 .then(function(result) { | |
| 148 assert_array_equivalent(result, [entries.a.response], | |
| 149 'Cache.matchAll should match by URL.'); | |
| 150 }); | |
| 151 }, 'Cache.matchAll with URL'); | |
| 152 | |
| 153 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 154 return cache.match(entries.a.request.url) | 16 return cache.match(entries.a.request.url) |
| 155 .then(function(result) { | 17 .then(function(result) { |
| 156 assert_object_equals_fixed(result, entries.a.response, | 18 assert_object_equals_fixed(result, entries.a.response, |
| 157 'Cache.match should match by URL.'); | 19 'Cache.match should match by URL.'); |
| 158 }); | 20 }); |
| 159 }, 'Cache.match with URL'); | 21 }, 'Cache.match with URL'); |
| 160 | 22 |
| 161 prepopulated_cache_test(simple_entries, function(cache, entries) { | 23 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 162 return cache.matchAll(entries.a.request) | |
| 163 .then(function(result) { | |
| 164 assert_array_equivalent(result, [entries.a.response], | |
| 165 'Cache.matchAll should match by Request.'); | |
| 166 }); | |
| 167 }, 'Cache.matchAll with Request'); | |
| 168 | |
| 169 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 170 return cache.match(entries.a.request) | 24 return cache.match(entries.a.request) |
| 171 .then(function(result) { | 25 .then(function(result) { |
| 172 assert_object_equals_fixed(result, entries.a.response, | 26 assert_object_equals_fixed(result, entries.a.response, |
| 173 'Cache.match should match by Request.'); | 27 'Cache.match should match by Request.'); |
| 174 }); | 28 }); |
| 175 }, 'Cache.match with Request'); | 29 }, 'Cache.match with Request'); |
| 176 | 30 |
| 177 prepopulated_cache_test(simple_entries, function(cache, entries) { | 31 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 178 return cache.matchAll(new Request(entries.a.request.url)) | |
| 179 .then(function(result) { | |
| 180 assert_array_equivalent(result, [entries.a.response], | |
| 181 'Cache.matchAll should match by Request.'); | |
| 182 }); | |
| 183 }, 'Cache.matchAll with new Request'); | |
| 184 | |
| 185 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 186 return cache.match(new Request(entries.a.request.url)) | 32 return cache.match(new Request(entries.a.request.url)) |
| 187 .then(function(result) { | 33 .then(function(result) { |
| 188 assert_object_equals_fixed(result, entries.a.response, | 34 assert_object_equals_fixed(result, entries.a.response, |
| 189 'Cache.match should match by Request.'); | 35 'Cache.match should match by Request.'); |
| 190 }); | 36 }); |
| 191 }, 'Cache.match with new Request'); | 37 }, 'Cache.match with new Request'); |
| 192 | 38 |
| 193 prepopulated_cache_test(simple_entries, function(cache, entries) { | 39 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 194 return cache.matchAll(entries.a.request, | |
| 195 {ignoreSearch: true}) | |
| 196 .then(function(result) { | |
| 197 assert_array_equivalent( | |
| 198 result, | |
| 199 [ | |
| 200 entries.a.response, | |
| 201 entries.a_with_query.response | |
| 202 ], | |
| 203 'Cache.matchAll with ignoreSearch should ignore the ' + | |
| 204 'search parameters of cached request.'); | |
| 205 }); | |
| 206 }, | |
| 207 'Cache.matchAll with ignoreSearch option (request with no search ' + | |
| 208 'parameters)'); | |
| 209 | |
| 210 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 211 return cache.match(entries.a.request, | 40 return cache.match(entries.a.request, |
| 212 {ignoreSearch: true}) | 41 {ignoreSearch: true}) |
| 213 .then(function(result) { | 42 .then(function(result) { |
| 214 assert_object_in_array( | 43 assert_object_in_array( |
| 215 result, | 44 result, |
| 216 [ | 45 [ |
| 217 entries.a.response, | 46 entries.a.response, |
| 218 entries.a_with_query.response | 47 entries.a_with_query.response |
| 219 ], | 48 ], |
| 220 'Cache.match with ignoreSearch should ignore the ' + | 49 'Cache.match with ignoreSearch should ignore the ' + |
| 221 'search parameters of cached request.'); | 50 'search parameters of cached request.'); |
| 222 }); | 51 }); |
| 223 }, | 52 }, |
| 224 'Cache.match with ignoreSearch option (request with no search ' + | 53 'Cache.match with ignoreSearch option (request with no search ' + |
| 225 'parameters)'); | 54 'parameters)'); |
| 226 | 55 |
| 227 prepopulated_cache_test(simple_entries, function(cache, entries) { | 56 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 228 return cache.matchAll(entries.a_with_query.request, | |
| 229 {ignoreSearch: true}) | |
| 230 .then(function(result) { | |
| 231 assert_array_equivalent( | |
| 232 result, | |
| 233 [ | |
| 234 entries.a.response, | |
| 235 entries.a_with_query.response | |
| 236 ], | |
| 237 'Cache.matchAll with ignoreSearch should ignore the ' + | |
| 238 'search parameters of request.'); | |
| 239 }); | |
| 240 }, | |
| 241 'Cache.matchAll with ignoreSearch option (request with search parameter)'); | |
| 242 | |
| 243 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 244 return cache.match(entries.a_with_query.request, | 57 return cache.match(entries.a_with_query.request, |
| 245 {ignoreSearch: true}) | 58 {ignoreSearch: true}) |
| 246 .then(function(result) { | 59 .then(function(result) { |
| 247 assert_object_in_array( | 60 assert_object_in_array( |
| 248 result, | 61 result, |
| 249 [ | 62 [ |
| 250 entries.a.response, | 63 entries.a.response, |
| 251 entries.a_with_query.response | 64 entries.a_with_query.response |
| 252 ], | 65 ], |
| 253 'Cache.match with ignoreSearch should ignore the ' + | 66 'Cache.match with ignoreSearch should ignore the ' + |
| 254 'search parameters of request.'); | 67 'search parameters of request.'); |
| 255 }); | 68 }); |
| 256 }, | 69 }, |
| 257 'Cache.match with ignoreSearch option (request with search parameter)'); | 70 'Cache.match with ignoreSearch option (request with search parameter)'); |
| 258 | 71 |
| 259 prepopulated_cache_test(simple_entries, function(cache, entries) { | 72 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 260 return cache.matchAll(entries.cat.request.url + '#mouse') | |
| 261 .then(function(result) { | |
| 262 assert_array_equivalent( | |
| 263 result, | |
| 264 [ | |
| 265 entries.cat.response, | |
| 266 ], | |
| 267 'Cache.matchAll should ignore URL fragment.'); | |
| 268 }); | |
| 269 }, 'Cache.matchAll with URL containing fragment'); | |
| 270 | |
| 271 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 272 return cache.match(entries.cat.request.url + '#mouse') | 73 return cache.match(entries.cat.request.url + '#mouse') |
| 273 .then(function(result) { | 74 .then(function(result) { |
| 274 assert_object_equals_fixed(result, entries.cat.response, | 75 assert_object_equals_fixed(result, entries.cat.response, |
| 275 'Cache.match should ignore URL fragment.'); | 76 'Cache.match should ignore URL fragment.'); |
| 276 }); | 77 }); |
| 277 }, 'Cache.match with URL containing fragment'); | 78 }, 'Cache.match with URL containing fragment'); |
| 278 | 79 |
| 279 prepopulated_cache_test(simple_entries, function(cache, entries) { | 80 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 280 return cache.matchAll('http') | |
| 281 .then(function(result) { | |
| 282 assert_array_equivalent( | |
| 283 result, [], | |
| 284 'Cache.matchAll should treat query as a URL and not ' + | |
| 285 'just a string fragment.'); | |
| 286 }); | |
| 287 }, 'Cache.matchAll with string fragment "http" as query'); | |
| 288 | |
| 289 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 290 return cache.match('http') | 81 return cache.match('http') |
| 291 .then(function(result) { | 82 .then(function(result) { |
| 292 assert_equals( | 83 assert_equals( |
| 293 result, undefined, | 84 result, undefined, |
| 294 'Cache.match should treat query as a URL and not ' + | 85 'Cache.match should treat query as a URL and not ' + |
| 295 'just a string fragment.'); | 86 'just a string fragment.'); |
| 296 }); | 87 }); |
| 297 }, 'Cache.match with string fragment "http" as query'); | 88 }, 'Cache.match with string fragment "http" as query'); |
| 298 | 89 |
| 299 prepopulated_cache_test(simple_entries, function(cache, entries) { | 90 prepopulated_cache_test(simple_entries, function(cache, entries) { |
| 300 return cache.matchAll(entries.secret_cat.request.url) | |
| 301 .then(function(result) { | |
| 302 assert_array_equivalent( | |
| 303 result, [entries.secret_cat.response], | |
| 304 'Cache.matchAll should not ignore embedded credentials'); | |
| 305 }); | |
| 306 }, 'Cache.matchAll with URL containing credentials'); | |
| 307 | |
| 308 prepopulated_cache_test(simple_entries, function(cache, entries) { | |
| 309 return cache.match(entries.secret_cat.request.url) | 91 return cache.match(entries.secret_cat.request.url) |
| 310 .then(function(result) { | 92 .then(function(result) { |
| 311 assert_object_equals_fixed( | 93 assert_object_equals_fixed( |
| 312 result, entries.secret_cat.response, | 94 result, entries.secret_cat.response, |
| 313 'Cache.match should not ignore embedded credentials'); | 95 'Cache.match should not ignore embedded credentials'); |
| 314 }); | 96 }); |
| 315 }, 'Cache.match with URL containing credentials'); | 97 }, 'Cache.match with URL containing credentials'); |
| 316 | 98 |
| 317 prepopulated_cache_test(vary_entries, function(cache, entries) { | 99 prepopulated_cache_test(vary_entries, function(cache, entries) { |
| 318 return cache.matchAll('http://example.com/c') | |
| 319 .then(function(result) { | |
| 320 assert_array_equivalent( | |
| 321 result, | |
| 322 [ | |
| 323 entries.vary_wildcard.response, | |
| 324 entries.vary_cookie_absent.response | |
| 325 ], | |
| 326 'Cache.matchAll should exclude matches if a vary header is ' + | |
| 327 'missing in the query request, but is present in the cached ' + | |
| 328 'request.'); | |
| 329 }) | |
| 330 | |
| 331 .then(function() { | |
| 332 return cache.matchAll( | |
| 333 new Request('http://example.com/c', | |
| 334 {headers: {'Cookies': 'none-of-the-above'}})); | |
| 335 }) | |
| 336 .then(function(result) { | |
| 337 assert_array_equivalent( | |
| 338 result, | |
| 339 [ | |
| 340 entries.vary_wildcard.response | |
| 341 ], | |
| 342 'Cache.matchAll should exclude matches if a vary header is ' + | |
| 343 'missing in the cached request, but is present in the query ' + | |
| 344 'request.'); | |
| 345 }) | |
| 346 | |
| 347 .then(function() { | |
| 348 return cache.matchAll( | |
| 349 new Request('http://example.com/c', | |
| 350 {headers: {'Cookies': 'is-for-cookie'}})); | |
| 351 }) | |
| 352 .then(function(result) { | |
| 353 assert_array_equivalent( | |
| 354 result, | |
| 355 [entries.vary_cookie_is_cookie.response], | |
| 356 'Cache.matchAll should match the entire header if a vary header ' + | |
| 357 'is present in both the query and cached requests.'); | |
| 358 }); | |
| 359 }, 'Cache.matchAll with responses containing "Vary" header'); | |
| 360 | |
| 361 prepopulated_cache_test(vary_entries, function(cache, entries) { | |
| 362 return cache.match('http://example.com/c') | 100 return cache.match('http://example.com/c') |
| 363 .then(function(result) { | 101 .then(function(result) { |
| 364 assert_object_in_array( | 102 assert_object_in_array( |
| 365 result, | 103 result, |
| 366 [ | 104 [ |
| 367 entries.vary_wildcard.response, | 105 entries.vary_wildcard.response, |
| 368 entries.vary_cookie_absent.response | 106 entries.vary_cookie_absent.response |
| 369 ], | 107 ], |
| 370 'Cache.match should honor "Vary" header.'); | 108 'Cache.match should honor "Vary" header.'); |
| 371 }); | 109 }); |
| 372 }, 'Cache.match with responses containing "Vary" header'); | 110 }, 'Cache.match with responses containing "Vary" header'); |
| 373 | 111 |
| 374 prepopulated_cache_test(vary_entries, function(cache, entries) { | |
| 375 return cache.matchAll('http://example.com/c', | |
| 376 {ignoreVary: true}) | |
| 377 .then(function(result) { | |
| 378 assert_array_equivalent( | |
| 379 result, | |
| 380 [ | |
| 381 entries.vary_cookie_is_cookie.response, | |
| 382 entries.vary_cookie_is_good.response, | |
| 383 entries.vary_cookie_absent.response, | |
| 384 entries.vary_wildcard.response | |
| 385 ], | |
| 386 'Cache.matchAll should honor "ignoreVary" parameter.'); | |
| 387 }); | |
| 388 }, 'Cache.matchAll with "ignoreVary" parameter'); | |
| 389 | |
| 390 cache_test(function(cache) { | 112 cache_test(function(cache) { |
| 391 var request = new Request('http://example.com'); | 113 var request = new Request('http://example.com'); |
| 392 var response; | 114 var response; |
| 393 var request_url = new URL('../resources/simple.txt', location.href).href; | 115 var request_url = new URL('../resources/simple.txt', location.href).href; |
| 394 return fetch(request_url) | 116 return fetch(request_url) |
| 395 .then(function(fetch_result) { | 117 .then(function(fetch_result) { |
| 396 response = fetch_result; | 118 response = fetch_result; |
| 397 assert_equals( | 119 assert_equals( |
| 398 response.url, request_url, | 120 response.url, request_url, |
| 399 '[https://fetch.spec.whatwg.org/#dom-response-url] ' + | 121 '[https://fetch.spec.whatwg.org/#dom-response-url] ' + |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 var response = entries.error_response.response; | 193 var response = entries.error_response.response; |
| 472 return cache.match(entries.error_response.request.url) | 194 return cache.match(entries.error_response.request.url) |
| 473 .then(function(result) { | 195 .then(function(result) { |
| 474 assert_object_equals_fixed( | 196 assert_object_equals_fixed( |
| 475 result, entries.error_response.response, | 197 result, entries.error_response.response, |
| 476 'Cache.match should return a Response object that has the ' + | 198 'Cache.match should return a Response object that has the ' + |
| 477 'same properties as a stored network error response.'); | 199 'same properties as a stored network error response.'); |
| 478 }); | 200 }); |
| 479 }, 'Cache.match with a network error Response'); | 201 }, 'Cache.match with a network error Response'); |
| 480 | 202 |
| 481 // Helpers --- | |
| 482 | |
| 483 // Run |test_function| with a Cache object as its only parameter. Prior to the | |
| 484 // call, the Cache is populated by cache entries from |entries|. The latter is | |
| 485 // expected to be an Object mapping arbitrary keys to objects of the form | |
| 486 // {request: <Request object>, response: <Response object>}. There's no | |
| 487 // guarantee on the order in which entries will be added to the cache. | |
| 488 // | |
| 489 // |test_function| should return a Promise that can be used with promise_test. | |
| 490 function prepopulated_cache_test(entries, test_function, description) { | |
| 491 cache_test(function(cache) { | |
| 492 var p = Promise.resolve(); | |
| 493 var hash = {}; | |
| 494 entries.forEach(function(entry) { | |
| 495 p = p.then(function() { | |
| 496 return cache.put(entry.request.clone(), | |
| 497 entry.response.clone()) | |
| 498 .catch(function(e) { | |
| 499 assert_unreached('Test setup failed for entry ' + | |
| 500 entry.name + ': ' + e); | |
| 501 }); | |
| 502 }); | |
| 503 hash[entry.name] = entry; | |
| 504 }); | |
| 505 p = p.then(function() { | |
| 506 assert_equals(Object.keys(hash).length, entries.length); | |
| 507 }); | |
| 508 | |
| 509 return p.then(function() { | |
| 510 return test_function(cache, hash); | |
| 511 }); | |
| 512 }, description); | |
| 513 } | |
| 514 | |
| 515 done(); | 203 done(); |
| OLD | NEW |