Best JavaScript code snippet using wpt
cache-match.js
Source:cache-match.js
...12 }, 'Cache.match with no matching entries');13prepopulated_cache_test(simple_entries, function(cache, entries) {14 return cache.match(entries.a.request.url)15 .then(function(result) {16 assert_response_equals(result, entries.a.response,17 'Cache.match should match by URL.');18 });19 }, 'Cache.match with URL');20prepopulated_cache_test(simple_entries, function(cache, entries) {21 return cache.match(entries.a.request)22 .then(function(result) {23 assert_response_equals(result, entries.a.response,24 'Cache.match should match by Request.');25 });26 }, 'Cache.match with Request');27prepopulated_cache_test(simple_entries, function(cache, entries) {28 return cache.match(new Request(entries.a.request.url))29 .then(function(result) {30 assert_response_equals(result, entries.a.response,31 'Cache.match should match by Request.');32 });33 }, 'Cache.match with new Request');34prepopulated_cache_test(simple_entries, function(cache, entries) {35 return cache.match(entries.a.request,36 {ignoreSearch: true})37 .then(function(result) {38 assert_response_in_array(39 result,40 [41 entries.a.response,42 entries.a_with_query.response43 ],44 'Cache.match with ignoreSearch should ignore the ' +45 'search parameters of cached request.');46 });47 },48 'Cache.match with ignoreSearch option (request with no search ' +49 'parameters)');50prepopulated_cache_test(simple_entries, function(cache, entries) {51 return cache.match(entries.a_with_query.request,52 {ignoreSearch: true})53 .then(function(result) {54 assert_response_in_array(55 result,56 [57 entries.a.response,58 entries.a_with_query.response59 ],60 'Cache.match with ignoreSearch should ignore the ' +61 'search parameters of request.');62 });63 },64 'Cache.match with ignoreSearch option (request with search parameter)');65prepopulated_cache_test(simple_entries, function(cache, entries) {66 return cache.match(entries.cat.request.url + '#mouse')67 .then(function(result) {68 assert_response_equals(result, entries.cat.response,69 'Cache.match should ignore URL fragment.');70 });71 }, 'Cache.match with URL containing fragment');72prepopulated_cache_test(simple_entries, function(cache, entries) {73 return cache.match('http')74 .then(function(result) {75 assert_equals(76 result, undefined,77 'Cache.match should treat query as a URL and not ' +78 'just a string fragment.');79 });80 }, 'Cache.match with string fragment "http" as query');81prepopulated_cache_test(simple_entries, function(cache, entries) {82 return cache.match(entries.secret_cat.request.url)83 .then(function(result) {84 assert_response_equals(85 result, entries.secret_cat.response,86 'Cache.match should not ignore embedded credentials');87 });88 }, 'Cache.match with URL containing credentials');89prepopulated_cache_test(vary_entries, function(cache, entries) {90 return cache.match('http://example.com/c')91 .then(function(result) {92 assert_response_in_array(93 result,94 [95 entries.vary_wildcard.response,96 entries.vary_cookie_absent.response97 ],98 'Cache.match should honor "Vary" header.');99 });100 }, 'Cache.match with responses containing "Vary" header');101cache_test(function(cache) {102 var request = new Request('http://example.com');103 var response;104 var request_url = new URL('../resources/simple.txt', location.href).href;105 return fetch(request_url)106 .then(function(fetch_result) {107 response = fetch_result;108 assert_equals(109 response.url, request_url,110 '[https://fetch.spec.whatwg.org/#dom-response-url] ' +111 'Reponse.url should return the URL of the response.');112 return cache.put(request, response.clone());113 })114 .then(function() {115 return cache.match(request.url);116 })117 .then(function(result) {118 assert_response_equals(119 result, response,120 'Cache.match should return a Response object that has the same ' +121 'properties as the stored response.');122 return cache.match(response.url);123 })124 .then(function(result) {125 assert_equals(126 result, undefined,127 'Cache.match should not match cache entry based on response URL.');128 });129 }, 'Cache.match with Request and Response objects with different URLs');130cache_test(function(cache) {131 var request_url = new URL('../resources/simple.txt', location.href).href;132 return fetch(request_url)133 .then(function(fetch_result) {134 return cache.put(new Request(request_url), fetch_result);135 })136 .then(function() {137 return cache.match(request_url);138 })139 .then(function(result) {140 return result.text();141 })142 .then(function(body_text) {143 assert_equals(body_text, 'a simple text file\n',144 'Cache.match should return a Response object with a ' +145 'valid body.');146 })147 .then(function() {148 return cache.match(request_url);149 })150 .then(function(result) {151 return result.text();152 })153 .then(function(body_text) {154 assert_equals(body_text, 'a simple text file\n',155 'Cache.match should return a Response object with a ' +156 'valid body each time it is called.');157 });158 }, 'Cache.match invoked multiple times for the same Request/Response');159prepopulated_cache_test(simple_entries, function(cache, entries) {160 var request = new Request(entries.a.request.clone(), {method: 'POST'});161 return cache.match(request)162 .then(function(result) {163 assert_equals(result, undefined,164 'Cache.match should not find a match');165 });166 }, 'Cache.match with POST Request');167prepopulated_cache_test(simple_entries, function(cache, entries) {168 var response = entries.non_2xx_response.response;169 return cache.match(entries.non_2xx_response.request.url)170 .then(function(result) {171 assert_response_equals(172 result, entries.non_2xx_response.response,173 'Cache.match should return a Response object that has the ' +174 'same properties as a stored non-2xx response.');175 });176 }, 'Cache.match with a non-2xx Response');177prepopulated_cache_test(simple_entries, function(cache, entries) {178 var response = entries.error_response.response;179 return cache.match(entries.error_response.request.url)180 .then(function(result) {181 assert_response_equals(182 result, entries.error_response.response,183 'Cache.match should return a Response object that has the ' +184 'same properties as a stored network error response.');185 });186 }, 'Cache.match with a network error Response');...
testharness-helpers.js
Source:testharness-helpers.js
...44}45// Helper for testing with Response objects. Compares simple46// attributes defined on the interfaces, as well as the headers. It47// does not compare the response bodies.48function assert_response_equals(actual, expected, description) {49 assert_class_string(actual, "Response", description);50 ["type", "url", "status", "ok", "statusText"].forEach(function(attribute) {51 assert_equals(actual[attribute], expected[attribute],52 description + " Attributes differ: " + attribute + ".");53 });54 assert_header_equals(actual.headers, expected.headers, description);55}56// Assert that the two arrays |actual| and |expected| contain the same57// set of Responses as determined by assert_response_equals. The order58// is not significant.59//60// |expected| is assumed to not contain any duplicates.61function assert_response_array_equivalent(actual, expected, description) {62 assert_true(Array.isArray(actual), description);63 assert_equals(actual.length, expected.length, description);64 expected.forEach(function(expected_element) {65 // assert_response_in_array treats the first argument as being66 // 'actual', and the second as being 'expected array'. We are67 // switching them around because we want to be resilient68 // against the |actual| array containing duplicates.69 assert_response_in_array(expected_element, actual, description);70 });71}72// Asserts that two arrays |actual| and |expected| contain the same73// set of Responses as determined by assert_response_equals(). The74// corresponding elements must occupy corresponding indices in their75// respective arrays.76function assert_response_array_equals(actual, expected, description) {77 assert_true(Array.isArray(actual), description);78 assert_equals(actual.length, expected.length, description);79 actual.forEach(function(value, index) {80 assert_response_equals(value, expected[index],81 description + " : object[" + index + "]");82 });83}84// Equivalent to assert_in_array, but uses assert_response_equals.85function assert_response_in_array(actual, expected_array, description) {86 assert_true(expected_array.some(function(element) {87 try {88 assert_response_equals(actual, element);89 return true;90 } catch (e) {91 return false;92 }93 }), description);...
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!!