Best JavaScript code snippet using wpt
cache-match-worker.js
Source:cache-match-worker.js
1importScripts('worker-testharness.js');2importScripts('/resources/testharness-helpers.js');3importScripts('override_assert_object_equals.js');4// A set of Request/Response pairs to be used with prepopulated_cache_test().5var simple_entries = [6 {7 name: 'a',8 request: new Request('http://example.com/a'),9 response: new Response('')10 },11 {12 name: 'b',13 request: new Request('http://example.com/b'),14 response: new Response('')15 },16 {17 name: 'a_with_query',18 request: new Request('http://example.com/a?q=r'),19 response: new Response('')20 },21 {22 name: 'A',23 request: new Request('http://example.com/A'),24 response: new Response('')25 },26 {27 name: 'a_https',28 request: new Request('https://example.com/a'),29 response: new Response('')30 },31 {32 name: 'a_org',33 request: new Request('http://example.org/a'),34 response: new Response('')35 },36 {37 name: 'cat',38 request: new Request('http://example.com/cat'),39 response: new Response('')40 },41 {42 name: 'catmandu',43 request: new Request('http://example.com/catmandu'),44 response: new Response('')45 },46 {47 name: 'cat_num_lives',48 request: new Request('http://example.com/cat?lives=9'),49 response: new Response('')50 },51 {52 name: 'cat_in_the_hat',53 request: new Request('http://example.com/cat/in/the/hat'),54 response: new Response('')55 },56 {57 name: 'secret_cat',58 request: new Request('http://tom:jerry@example.com/cat'),59 response: new Response('')60 },61 {62 name: 'top_secret_cat',63 request: new Request('http://tom:j3rry@example.com/cat'),64 response: new Response('')65 }66];67// A set of Request/Response pairs to be used with prepopulated_cache_test().68// These contain a mix of test cases that use Vary headers.69var vary_entries = [70 {71 name: 'vary_cookie_is_cookie',72 request: new Request('http://example.com/c',73 {headers: {'Cookies': 'is-for-cookie'}}),74 response: new Response('',75 {headers: {'Vary': 'Cookies'}})76 },77 {78 name: 'vary_cookie_is_good',79 request: new Request('http://example.com/c',80 {headers: {'Cookies': 'is-good-enough-for-me'}}),81 response: new Response('',82 {headers: {'Vary': 'Cookies'}})83 },84 {85 name: 'vary_cookie_absent',86 request: new Request('http://example.com/c'),87 response: new Response('',88 {headers: {'Vary': 'Cookies'}})89 },90 {91 name: 'vary_wildcard',92 request: new Request('http://example.com/c',93 {headers: {'Cookies': 'x', 'X-Key': '1'}}),94 response: new Response('',95 {headers: {'Vary': '*'}})96 }97];98prepopulated_cache_test(simple_entries, function(cache, entries) {99 return cache.matchAll('not-present-in-the-cache')100 .then(function(result) {101 assert_array_equivalent(102 result, [],103 'Cache.matchAll should resolve with an empty array on failure.');104 });105 }, 'Cache.matchAll with no matching entries');106prepopulated_cache_test(simple_entries, function(cache, entries) {107 return cache.match('not-present-in-the-cache')108 .then(function(result) {109 assert_equals(result, undefined,110 'Cache.match failures should resolve with undefined.');111 });112 }, 'Cache.match with no matching entries');113prepopulated_cache_test(simple_entries, function(cache, entries) {114 return cache.matchAll(entries.a.request.url)115 .then(function(result) {116 assert_array_objects_equals(result, [entries.a.response],117 'Cache.matchAll should match by URL.');118 });119 }, 'Cache.matchAll with URL');120prepopulated_cache_test(simple_entries, function(cache, entries) {121 return cache.match(entries.a.request.url)122 .then(function(result) {123 assert_object_equals(result, entries.a.response,124 'Cache.match should match by URL.');125 });126 }, 'Cache.match with URL');127prepopulated_cache_test(simple_entries, function(cache, entries) {128 return cache.matchAll(entries.a.request)129 .then(function(result) {130 assert_array_objects_equals(131 result, [entries.a.response],132 'Cache.matchAll should match by Request.');133 });134 }, 'Cache.matchAll with Request');135prepopulated_cache_test(simple_entries, function(cache, entries) {136 return cache.match(entries.a.request)137 .then(function(result) {138 assert_object_equals(result, entries.a.response,139 'Cache.match should match by Request.');140 });141 }, 'Cache.match with Request');142prepopulated_cache_test(simple_entries, function(cache, entries) {143 return cache.matchAll(new Request(entries.a.request.url))144 .then(function(result) {145 assert_array_objects_equals(146 result, [entries.a.response],147 'Cache.matchAll should match by Request.');148 });149 }, 'Cache.matchAll with new Request');150prepopulated_cache_test(simple_entries, function(cache, entries) {151 return cache.match(new Request(entries.a.request.url))152 .then(function(result) {153 assert_object_equals(result, entries.a.response,154 'Cache.match should match by Request.');155 });156 }, 'Cache.match with new Request');157cache_test(function(cache) {158 var request = new Request('https://example.com/foo', {159 method: 'POST',160 body: 'Hello world!'161 });162 var response = new Response('Booyah!', {163 status: 200,164 headers: {'Content-Type': 'text/plain'}165 });166 return cache.put(request.clone(), response.clone())167 .then(function() {168 assert_false(169 request.bodyUsed,170 '[https://fetch.spec.whatwg.org/#concept-body-used-flag] ' +171 'Request.bodyUsed flag should be initially false.');172 })173 .then(function() {174 return cache.match(request);175 })176 .then(function(result) {177 assert_false(request.bodyUsed,178 'Cache.match should not consume Request body.');179 });180 }, 'Cache.match with Request containing non-empty body');181prepopulated_cache_test(simple_entries, function(cache, entries) {182 return cache.matchAll(entries.a.request,183 {ignoreSearch: true})184 .then(function(result) {185 assert_array_equivalent(186 result,187 [188 entries.a.response,189 entries.a_with_query.response190 ],191 'Cache.matchAll with ignoreSearch should ignore the ' +192 'search parameters of cached request.');193 });194 },195 'Cache.matchAll with ignoreSearch option (request with no search ' +196 'parameters)');197prepopulated_cache_test(simple_entries, function(cache, entries) {198 return cache.match(entries.a.request,199 {ignoreSearch: true})200 .then(function(result) {201 assert_object_in_array(202 result,203 [204 entries.a.response,205 entries.a_with_query.response206 ],207 'Cache.match with ignoreSearch should ignore the ' +208 'search parameters of cached request.');209 });210 },211 'Cache.match with ignoreSearch option (request with no search ' +212 'parameters)');213prepopulated_cache_test(simple_entries, function(cache, entries) {214 return cache.matchAll(entries.a_with_query.request,215 {ignoreSearch: true})216 .then(function(result) {217 assert_array_equivalent(218 result,219 [220 entries.a.response,221 entries.a_with_query.response222 ],223 'Cache.matchAll with ignoreSearch should ignore the ' +224 'search parameters of request.');225 });226 },227 'Cache.matchAll with ignoreSearch option (request with search parameter)');228prepopulated_cache_test(simple_entries, function(cache, entries) {229 return cache.match(entries.a_with_query.request,230 {ignoreSearch: true})231 .then(function(result) {232 assert_object_in_array(233 result,234 [235 entries.a.response,236 entries.a_with_query.response237 ],238 'Cache.match with ignoreSearch should ignore the ' +239 'search parameters of request.');240 });241 },242 'Cache.match with ignoreSearch option (request with search parameter)');243prepopulated_cache_test(simple_entries, function(cache, entries) {244 return cache.matchAll(entries.cat.request.url + '#mouse')245 .then(function(result) {246 assert_array_equivalent(247 result,248 [249 entries.cat.response,250 ],251 'Cache.matchAll should ignore URL fragment.');252 });253 }, 'Cache.matchAll with URL containing fragment');254prepopulated_cache_test(simple_entries, function(cache, entries) {255 return cache.match(entries.cat.request.url + '#mouse')256 .then(function(result) {257 assert_object_equals(result, entries.cat.response,258 'Cache.match should ignore URL fragment.');259 });260 }, 'Cache.match with URL containing fragment');261prepopulated_cache_test(simple_entries, function(cache, entries) {262 return cache.matchAll('http')263 .then(function(result) {264 assert_array_equivalent(265 result, [],266 'Cache.matchAll should treat query as a URL and not ' +267 'just a string fragment.');268 });269 }, 'Cache.matchAll with string fragment "http" as query');270prepopulated_cache_test(simple_entries, function(cache, entries) {271 return cache.match('http')272 .then(function(result) {273 assert_equals(274 result, undefined,275 'Cache.match should treat query as a URL and not ' +276 'just a string fragment.');277 });278 }, 'Cache.match with string fragment "http" as query');279prepopulated_cache_test(simple_entries, function(cache, entries) {280 return cache.matchAll('http://example.com/cat',281 {prefixMatch: true})282 .then(function(result) {283 assert_array_equivalent(284 result,285 [286 entries.cat.response,287 entries.catmandu.response,288 entries.cat_num_lives.response,289 entries.cat_in_the_hat.response290 ],291 'Cache.matchAll should honor prefixMatch.');292 });293 }, 'Cache.matchAll with prefixMatch option');294prepopulated_cache_test(simple_entries, function(cache, entries) {295 return cache.match('http://example.com/cat',296 {prefixMatch: true})297 .then(function(result) {298 assert_object_in_array(299 result,300 [301 entries.cat.response,302 entries.catmandu.response,303 entries.cat_num_lives.response,304 entries.cat_in_the_hat.response305 ],306 'Cache.match should honor prefixMatch.');307 });308 }, 'Cache.match with prefixMatch option');309prepopulated_cache_test(simple_entries, function(cache, entries) {310 return cache.matchAll('http://example.com/cat/',311 {prefixMatch: true})312 .then(function(result) {313 assert_array_equivalent(314 result, [entries.cat_in_the_hat.response],315 'Cache.matchAll should honor prefixMatch.');316 });317 }, 'Cache.matchAll with prefixMatch option (URL ending with path delimiter)');318prepopulated_cache_test(simple_entries, function(cache, entries) {319 return cache.match('http://example.com/cat/',320 {prefixMatch: true})321 .then(function(result) {322 assert_object_equals(323 result, entries.cat_in_the_hat.response,324 'Cache.match should honor prefixMatch.');325 });326 }, 'Cache.match with prefixMatch option (URL ending with path delimiter)');327prepopulated_cache_test(simple_entries, function(cache, entries) {328 return cache.matchAll('http://tom:jerry@example.com', {prefixMatch: true})329 .then(function(result) {330 assert_array_equivalent(331 result,332 [333 entries.secret_cat.response,334 ],335 'Cache.matchAll should honor prefixMatch.');336 });337 }, 'Cache.matchAll with prefixMatch option (URL with embedded credentials)');338prepopulated_cache_test(simple_entries, function(cache, entries) {339 return cache.match('http://tom:jerry@example.com', {prefixMatch: true})340 .then(function(result) {341 assert_object_equals(342 result, entries.secret_cat.response,343 'Cache.match should honor prefixMatch.');344 });345 }, 'Cache.match with prefixMatch option (URL with embedded credentials)');346prepopulated_cache_test(simple_entries, function(cache, entries) {347 // The string 'http://tom' should be converted to a URL and then serialized348 // yielding 'http://tom/'. The trailing slash prevents the URL from matching349 // the embedded credentials in the entries already in the cache.350 return cache.matchAll('http://tom', {prefixMatch: true})351 .then(function(result) {352 assert_array_equivalent(353 result, [],354 'Cache.matchAll should honor prefixMatch.');355 });356 },357 'Cache.matchAll with prefixMatch option (URL matching embedded credentials)');358prepopulated_cache_test(simple_entries, function(cache, entries) {359 // The string 'http://tom' should be converted to a URL and then serialized360 // yielding 'http://tom/'. The trailing slash prevents the URL from matching361 // the embedded credentials in the entries already in the cache.362 return cache.match('http://tom', {prefixMatch: true})363 .then(function(result) {364 assert_equals(result, undefined,365 'Cache.match should honor prefixMatch.');366 });367 },368 'Cache.match with prefixMatch option (URL matching embedded credentials)');369prepopulated_cache_test(simple_entries, function(cache, entries) {370 return cache.matchAll(entries.secret_cat.request.url)371 .then(function(result) {372 assert_array_equivalent(373 result, [entries.secret_cat.response],374 'Cache.matchAll should not ignore embedded credentials');375 });376 }, 'Cache.matchAll with URL containing credentials');377prepopulated_cache_test(simple_entries, function(cache, entries) {378 return cache.match(entries.secret_cat.request.url)379 .then(function(result) {380 assert_object_equals(381 result, entries.secret_cat.response,382 'Cache.match should not ignore embedded credentials');383 });384 }, 'Cache.match with URL containing credentials');385prepopulated_cache_test(vary_entries, function(cache, entries) {386 return cache.matchAll('http://example.com/c')387 .then(function(result) {388 assert_array_equivalent(389 result,390 [391 entries.vary_wildcard.response,392 entries.vary_cookie_absent.response393 ],394 'Cache.matchAll should exclude matches if a vary header is ' +395 'missing in the query request, but is present in the cached ' +396 'request.');397 })398 .then(function() {399 return cache.matchAll(400 new Request('http://example.com/c',401 {headers: {'Cookies': 'none-of-the-above'}}));402 })403 .then(function(result) {404 assert_array_equivalent(405 result,406 [407 entries.vary_wildcard.response408 ],409 'Cache.matchAll should exclude matches if a vary header is ' +410 'missing in the cached request, but is present in the query ' +411 'request.');412 })413 .then(function() {414 return cache.matchAll(415 new Request('http://example.com/c',416 {headers: {'Cookies': 'is-for-cookie'}}));417 })418 .then(function(result) {419 assert_array_equivalent(420 result,421 [entries.vary_cookie_is_cookie.response],422 'Cache.matchAll should match the entire header if a vary header ' +423 'is present in both the query and cached requests.');424 });425 }, 'Cache.matchAll with responses containing "Vary" header');426prepopulated_cache_test(vary_entries, function(cache, entries) {427 return cache.match('http://example.com/c')428 .then(function(result) {429 assert_object_in_array(430 result,431 [432 entries.vary_wildcard.response,433 entries.vary_cookie_absent.response434 ],435 'Cache.match should honor "Vary" header.');436 });437 }, 'Cache.match with responses containing "Vary" header');438prepopulated_cache_test(vary_entries, function(cache, entries) {439 return cache.matchAll('http://example.com/c',440 {ignoreVary: true})441 .then(function(result) {442 assert_array_equivalent(443 result,444 [445 entries.vary_cookie_is_cookie.response,446 entries.vary_cookie_is_good.response,447 entries.vary_cookie_absent.response,448 entries.vary_wildcard.response449 ],450 'Cache.matchAll should honor "ignoreVary" parameter.');451 });452 }, 'Cache.matchAll with "ignoreVary" parameter');453cache_test(function(cache) {454 var request = new Request('http://example.com');455 var response;456 var request_url = new URL('simple.txt', location.href).href;457 return fetch(request_url)458 .then(function(fetch_result) {459 response = fetch_result;460 assert_equals(461 response.url, request_url,462 '[https://fetch.spec.whatwg.org/#dom-response-url] ' +463 'Reponse.url should return the URL of the response.');464 return cache.put(request, response.clone());465 })466 .then(function() {467 return cache.match(request.url);468 })469 .then(function(result) {470 assert_object_equals(471 result, response,472 'Cache.match should return a Response object that has the same ' +473 'properties as the stored response.');474 return cache.match(response.url);475 })476 .then(function(result) {477 assert_equals(478 result, undefined,479 'Cache.match should not match cache entry based on response URL.');480 });481 }, 'Cache.match with Request and Response objects with different URLs');482cache_test(function(cache) {483 var request_url = new URL('simple.txt', location.href).href;484 return fetch(request_url)485 .then(function(fetch_result) {486 return cache.put(new Request(request_url), fetch_result);487 })488 .then(function() {489 return cache.match(request_url);490 })491 .then(function(result) {492 return result.text();493 })494 .then(function(body_text) {495 assert_equals(body_text, 'a simple text file\n',496 'Cache.match should return a Response object with a ' +497 'valid body.');498 })499 .then(function() {500 return cache.match(request_url);501 })502 .then(function(result) {503 return result.text();504 })505 .then(function(body_text) {506 assert_equals(body_text, 'a simple text file\n',507 'Cache.match should return a Response object with a ' +508 'valid body each time it is called.');509 });510 }, 'Cache.match invoked multiple times for the same Request/Response');511// Helpers ---512// Run |test_function| with a Cache object as its only parameter. Prior to the513// call, the Cache is populated by cache entries from |entries|. The latter is514// expected to be an Object mapping arbitrary keys to objects of the form515// {request: <Request object>, response: <Response object>}. There's no516// guarantee on the order in which entries will be added to the cache.517//518// |test_function| should return a Promise that can be used with promise_test.519function prepopulated_cache_test(entries, test_function, description) {520 cache_test(function(cache) {521 var p = Promise.resolve();522 var hash = {};523 entries.forEach(function(entry) {524 p = p.then(function() {525 return cache.put(entry.request.clone(),526 entry.response.clone())527 .catch(function(e) {528 assert_unreached('Test setup failed for entry ' +529 entry.name + ': ' + e);530 });531 });532 hash[entry.name] = entry;533 });...
cache-match.js
Source:cache-match.js
2 importScripts('/resources/testharness.js');3 importScripts('../resources/testharness-helpers.js');4 importScripts('../resources/test-helpers.js');5}6// A set of Request/Response pairs to be used with prepopulated_cache_test().7var simple_entries = [8 {9 name: 'a',10 request: new Request('http://example.com/a'),11 response: new Response('')12 },13 {14 name: 'b',15 request: new Request('http://example.com/b'),16 response: new Response('')17 },18 {19 name: 'a_with_query',20 request: new Request('http://example.com/a?q=r'),21 response: new Response('')22 },23 {24 name: 'A',25 request: new Request('http://example.com/A'),26 response: new Response('')27 },28 {29 name: 'a_https',30 request: new Request('https://example.com/a'),31 response: new Response('')32 },33 {34 name: 'a_org',35 request: new Request('http://example.org/a'),36 response: new Response('')37 },38 {39 name: 'cat',40 request: new Request('http://example.com/cat'),41 response: new Response('')42 },43 {44 name: 'catmandu',45 request: new Request('http://example.com/catmandu'),46 response: new Response('')47 },48 {49 name: 'cat_num_lives',50 request: new Request('http://example.com/cat?lives=9'),51 response: new Response('')52 },53 {54 name: 'cat_in_the_hat',55 request: new Request('http://example.com/cat/in/the/hat'),56 response: new Response('')57 }58];59// A set of Request/Response pairs to be used with prepopulated_cache_test().60// These contain a mix of test cases that use Vary headers.61var vary_entries = [62 {63 name: 'vary_cookie_is_cookie',64 request: new Request('http://example.com/c',65 {headers: {'Cookies': 'is-for-cookie'}}),66 response: new Response('',67 {headers: {'Vary': 'Cookies'}})68 },69 {70 name: 'vary_cookie_is_good',71 request: new Request('http://example.com/c',72 {headers: {'Cookies': 'is-good-enough-for-me'}}),73 response: new Response('',74 {headers: {'Vary': 'Cookies'}})75 },76 {77 name: 'vary_cookie_absent',78 request: new Request('http://example.com/c'),79 response: new Response('',80 {headers: {'Vary': 'Cookies'}})81 }82];83prepopulated_cache_test(simple_entries, function(cache, entries) {84 return cache.matchAll('not-present-in-the-cache')85 .then(function(result) {86 assert_response_array_equivalent(87 result, [],88 'Cache.matchAll should resolve with an empty array on failure.');89 });90 }, 'Cache.matchAll with no matching entries');91prepopulated_cache_test(simple_entries, function(cache, entries) {92 return cache.match('not-present-in-the-cache')93 .then(function(result) {94 assert_equals(result, undefined,95 'Cache.match failures should resolve with undefined.');96 });97 }, 'Cache.match with no matching entries');98prepopulated_cache_test(simple_entries, function(cache, entries) {99 return cache.matchAll(entries.a.request.url)100 .then(function(result) {101 assert_response_array_equals(result, [entries.a.response],102 'Cache.matchAll should match by URL.');103 });104 }, 'Cache.matchAll with URL');105prepopulated_cache_test(simple_entries, function(cache, entries) {106 return cache.match(entries.a.request.url)107 .then(function(result) {108 assert_response_equals(result, entries.a.response,109 'Cache.match should match by URL.');110 });111 }, 'Cache.match with URL');112prepopulated_cache_test(simple_entries, function(cache, entries) {113 return cache.matchAll(entries.a.request)114 .then(function(result) {115 assert_response_array_equals(116 result, [entries.a.response],117 'Cache.matchAll should match by Request.');118 });119 }, 'Cache.matchAll with Request');120prepopulated_cache_test(simple_entries, function(cache, entries) {121 return cache.match(entries.a.request)122 .then(function(result) {123 assert_response_equals(result, entries.a.response,124 'Cache.match should match by Request.');125 });126 }, 'Cache.match with Request');127prepopulated_cache_test(simple_entries, function(cache, entries) {128 return cache.matchAll(new Request(entries.a.request.url))129 .then(function(result) {130 assert_response_array_equals(131 result, [entries.a.response],132 'Cache.matchAll should match by Request.');133 });134 }, 'Cache.matchAll with new Request');135prepopulated_cache_test(simple_entries, function(cache, entries) {136 return cache.match(new Request(entries.a.request.url))137 .then(function(result) {138 assert_response_equals(result, entries.a.response,139 'Cache.match should match by Request.');140 });141 }, 'Cache.match with new Request');142prepopulated_cache_test(simple_entries, function(cache, entries) {143 return cache.matchAll(entries.a.request,144 {ignoreSearch: true})145 .then(function(result) {146 assert_response_array_equivalent(147 result,148 [149 entries.a.response,150 entries.a_with_query.response151 ],152 'Cache.matchAll with ignoreSearch should ignore the ' +153 'search parameters of cached request.');154 });155 },156 'Cache.matchAll with ignoreSearch option (request with no search ' +157 'parameters)');158prepopulated_cache_test(simple_entries, function(cache, entries) {159 return cache.match(entries.a.request,160 {ignoreSearch: true})161 .then(function(result) {162 assert_response_in_array(163 result,164 [165 entries.a.response,166 entries.a_with_query.response167 ],168 'Cache.match with ignoreSearch should ignore the ' +169 'search parameters of cached request.');170 });171 },172 'Cache.match with ignoreSearch option (request with no search ' +173 'parameters)');174prepopulated_cache_test(simple_entries, function(cache, entries) {175 return cache.matchAll(entries.a_with_query.request,176 {ignoreSearch: true})177 .then(function(result) {178 assert_response_array_equivalent(179 result,180 [181 entries.a.response,182 entries.a_with_query.response183 ],184 'Cache.matchAll with ignoreSearch should ignore the ' +185 'search parameters of request.');186 });187 },188 'Cache.matchAll with ignoreSearch option (request with search parameter)');189prepopulated_cache_test(simple_entries, function(cache, entries) {190 return cache.match(entries.a_with_query.request,191 {ignoreSearch: true})192 .then(function(result) {193 assert_response_in_array(194 result,195 [196 entries.a.response,197 entries.a_with_query.response198 ],199 'Cache.match with ignoreSearch should ignore the ' +200 'search parameters of request.');201 });202 },203 'Cache.match with ignoreSearch option (request with search parameter)');204prepopulated_cache_test(simple_entries, function(cache, entries) {205 return cache.matchAll(entries.cat.request.url + '#mouse')206 .then(function(result) {207 assert_response_array_equivalent(208 result,209 [210 entries.cat.response,211 ],212 'Cache.matchAll should ignore URL fragment.');213 });214 }, 'Cache.matchAll with URL containing fragment');215prepopulated_cache_test(simple_entries, function(cache, entries) {216 return cache.match(entries.cat.request.url + '#mouse')217 .then(function(result) {218 assert_response_equals(result, entries.cat.response,219 'Cache.match should ignore URL fragment.');220 });221 }, 'Cache.match with URL containing fragment');222prepopulated_cache_test(simple_entries, function(cache, entries) {223 return cache.matchAll('http')224 .then(function(result) {225 assert_response_array_equivalent(226 result, [],227 'Cache.matchAll should treat query as a URL and not ' +228 'just a string fragment.');229 });230 }, 'Cache.matchAll with string fragment "http" as query');231prepopulated_cache_test(simple_entries, function(cache, entries) {232 return cache.match('http')233 .then(function(result) {234 assert_equals(235 result, undefined,236 'Cache.match should treat query as a URL and not ' +237 'just a string fragment.');238 });239 }, 'Cache.match with string fragment "http" as query');240prepopulated_cache_test(vary_entries, function(cache, entries) {241 return cache.matchAll('http://example.com/c')242 .then(function(result) {243 assert_response_array_equivalent(244 result,245 [246 entries.vary_cookie_absent.response247 ],248 'Cache.matchAll should exclude matches if a vary header is ' +249 'missing in the query request, but is present in the cached ' +250 'request.');251 })252 .then(function() {253 return cache.matchAll(254 new Request('http://example.com/c',255 {headers: {'Cookies': 'none-of-the-above'}}));256 })257 .then(function(result) {258 assert_response_array_equivalent(259 result,260 [261 ],262 'Cache.matchAll should exclude matches if a vary header is ' +263 'missing in the cached request, but is present in the query ' +264 'request.');265 })266 .then(function() {267 return cache.matchAll(268 new Request('http://example.com/c',269 {headers: {'Cookies': 'is-for-cookie'}}));270 })271 .then(function(result) {272 assert_response_array_equivalent(273 result,274 [entries.vary_cookie_is_cookie.response],275 'Cache.matchAll should match the entire header if a vary header ' +276 'is present in both the query and cached requests.');277 });278 }, 'Cache.matchAll with responses containing "Vary" header');279prepopulated_cache_test(vary_entries, function(cache, entries) {280 return cache.match('http://example.com/c')281 .then(function(result) {282 assert_response_in_array(283 result,284 [285 entries.vary_cookie_absent.response286 ],287 'Cache.match should honor "Vary" header.');288 });289 }, 'Cache.match with responses containing "Vary" header');290prepopulated_cache_test(vary_entries, function(cache, entries) {291 return cache.matchAll('http://example.com/c',292 {ignoreVary: true})293 .then(function(result) {294 assert_response_array_equivalent(295 result,296 [297 entries.vary_cookie_is_cookie.response,298 entries.vary_cookie_is_good.response,299 entries.vary_cookie_absent.response,300 ],301 'Cache.matchAll should honor "ignoreVary" parameter.');302 });303 }, 'Cache.matchAll with "ignoreVary" parameter');304cache_test(function(cache) {305 var request = new Request('http://example.com');306 var response;307 var request_url = new URL('../resources/simple.txt', location.href).href;308 return fetch(request_url)309 .then(function(fetch_result) {310 response = fetch_result;311 assert_equals(312 response.url, request_url,313 '[https://fetch.spec.whatwg.org/#dom-response-url] ' +314 'Reponse.url should return the URL of the response.');315 return cache.put(request, response.clone());316 })317 .then(function() {318 return cache.match(request.url);319 })320 .then(function(result) {321 assert_response_equals(322 result, response,323 'Cache.match should return a Response object that has the same ' +324 'properties as the stored response.');325 return cache.match(response.url);326 })327 .then(function(result) {328 assert_equals(329 result, undefined,330 'Cache.match should not match cache entry based on response URL.');331 });332 }, 'Cache.match with Request and Response objects with different URLs');333cache_test(function(cache) {334 var request_url = new URL('../resources/simple.txt', location.href).href;335 return fetch(request_url)336 .then(function(fetch_result) {337 return cache.put(new Request(request_url), fetch_result);338 })339 .then(function() {340 return cache.match(request_url);341 })342 .then(function(result) {343 return result.text();344 })345 .then(function(body_text) {346 assert_equals(body_text, 'a simple text file\n',347 'Cache.match should return a Response object with a ' +348 'valid body.');349 })350 .then(function() {351 return cache.match(request_url);352 })353 .then(function(result) {354 return result.text();355 })356 .then(function(body_text) {357 assert_equals(body_text, 'a simple text file\n',358 'Cache.match should return a Response object with a ' +359 'valid body each time it is called.');360 });361 }, 'Cache.match invoked multiple times for the same Request/Response');362prepopulated_cache_test(simple_entries, function(cache, entries) {363 var request = new Request(entries.a.request, { method: 'POST' });364 return cache.match(request)365 .then(function(result) {366 assert_equals(result, undefined,367 'Cache.match should not find a match');368 });369 }, 'Cache.match with POST Request');370// Helpers ---371// Run |test_function| with a Cache object as its only parameter. Prior to the372// call, the Cache is populated by cache entries from |entries|. The latter is373// expected to be an Object mapping arbitrary keys to objects of the form374// {request: <Request object>, response: <Response object>}. There's no375// guarantee on the order in which entries will be added to the cache.376//377// |test_function| should return a Promise that can be used with promise_test.378function prepopulated_cache_test(entries, test_function, description) {379 cache_test(function(cache) {380 var p = Promise.resolve();381 var hash = {};382 entries.forEach(function(entry) {383 p = p.then(function() {384 return cache.put(entry.request.clone(),385 entry.response.clone())386 .catch(function(e) {387 assert_unreached('Test setup failed for entry ' +388 entry.name + ': ' + e);389 });390 });391 hash[entry.name] = entry;392 });...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!