Best JavaScript code snippet using wpt
cache-put.js
Source:cache-put.js
...4 importScripts('../resources/test-helpers.js');5}6var test_url = 'https://example.com/foo';7var test_body = 'Hello world!';8cache_test(function(cache) {9 var request = new Request(test_url);10 var response = new Response(test_body);11 return cache.put(request, response)12 .then(function(result) {13 assert_equals(result, undefined,14 'Cache.put should resolve with undefined on success.');15 });16 }, 'Cache.put called with simple Request and Response');17cache_test(function(cache) {18 var test_url = new URL('../resources/simple.txt', location.href).href;19 var request = new Request(test_url);20 var response;21 return fetch(test_url)22 .then(function(fetch_result) {23 response = fetch_result.clone();24 return cache.put(request, fetch_result);25 })26 .then(function() {27 return cache.match(test_url);28 })29 .then(function(result) {30 assert_response_equals(result, response,31 'Cache.put should update the cache with ' +32 'new request and response.');33 return result.text();34 })35 .then(function(body) {36 assert_equals(body, 'a simple text file\n',37 'Cache.put should store response body.');38 });39 }, 'Cache.put called with Request and Response from fetch()');40cache_test(function(cache) {41 var request = new Request(test_url);42 var response = new Response(test_body);43 assert_false(request.bodyUsed,44 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +45 'Request.bodyUsed should be initially false.');46 return cache.put(request, response)47 .then(function() {48 assert_false(request.bodyUsed,49 'Cache.put should not mark empty request\'s body used');50 });51 }, 'Cache.put with Request without a body');52cache_test(function(cache) {53 var request = new Request(test_url);54 var response = new Response();55 assert_false(response.bodyUsed,56 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +57 'Response.bodyUsed should be initially false.');58 return cache.put(request, response)59 .then(function() {60 assert_false(response.bodyUsed,61 'Cache.put should not mark empty response\'s body used');62 });63 }, 'Cache.put with Response without a body');64cache_test(function(cache) {65 var request = new Request(test_url);66 var response = new Response(test_body);67 return cache.put(request, response.clone())68 .then(function() {69 return cache.match(test_url);70 })71 .then(function(result) {72 assert_response_equals(result, response,73 'Cache.put should update the cache with ' +74 'new Request and Response.');75 });76 }, 'Cache.put with a Response containing an empty URL');77cache_test(function(cache) {78 var request = new Request(test_url);79 var response = new Response('', {80 status: 200,81 headers: [['Content-Type', 'text/plain']]82 });83 return cache.put(request, response)84 .then(function() {85 return cache.match(test_url);86 })87 .then(function(result) {88 assert_equals(result.status, 200, 'Cache.put should store status.');89 assert_equals(result.headers.get('Content-Type'), 'text/plain',90 'Cache.put should store headers.');91 return result.text();92 })93 .then(function(body) {94 assert_equals(body, '',95 'Cache.put should store response body.');96 });97 }, 'Cache.put with an empty response body');98cache_test(function(cache) {99 var test_url = new URL('../resources/fetch-status.py?status=500', location.href).href;100 var request = new Request(test_url);101 var response;102 return fetch(test_url)103 .then(function(fetch_result) {104 assert_equals(fetch_result.status, 500,105 'Test framework error: The status code should be 500.');106 response = fetch_result.clone();107 return cache.put(request, fetch_result);108 })109 .then(function() {110 return cache.match(test_url);111 })112 .then(function(result) {113 assert_response_equals(result, response,114 'Cache.put should update the cache with ' +115 'new request and response.');116 return result.text();117 })118 .then(function(body) {119 assert_equals(body, '',120 'Cache.put should store response body.');121 });122 }, 'Cache.put with HTTP 500 response');123cache_test(function(cache) {124 var alternate_response_body = 'New body';125 var alternate_response = new Response(alternate_response_body,126 { statusText: 'New status' });127 return cache.put(new Request(test_url),128 new Response('Old body', { statusText: 'Old status' }))129 .then(function() {130 return cache.put(new Request(test_url), alternate_response.clone());131 })132 .then(function() {133 return cache.match(test_url);134 })135 .then(function(result) {136 assert_response_equals(result, alternate_response,137 'Cache.put should replace existing ' +138 'response with new response.');139 return result.text();140 })141 .then(function(body) {142 assert_equals(body, alternate_response_body,143 'Cache put should store new response body.');144 });145 }, 'Cache.put called twice with matching Requests and different Responses');146cache_test(function(cache) {147 var first_url = test_url;148 var second_url = first_url + '#(O_o)';149 var alternate_response_body = 'New body';150 var alternate_response = new Response(alternate_response_body,151 { statusText: 'New status' });152 return cache.put(new Request(first_url),153 new Response('Old body', { statusText: 'Old status' }))154 .then(function() {155 return cache.put(new Request(second_url), alternate_response.clone());156 })157 .then(function() {158 return cache.match(test_url);159 })160 .then(function(result) {161 assert_response_equals(result, alternate_response,162 'Cache.put should replace existing ' +163 'response with new response.');164 return result.text();165 })166 .then(function(body) {167 assert_equals(body, alternate_response_body,168 'Cache put should store new response body.');169 });170 }, 'Cache.put called twice with request URLs that differ only by a fragment');171cache_test(function(cache) {172 var url = 'http://example.com/foo';173 return cache.put(url, new Response('some body'))174 .then(function() { return cache.match(url); })175 .then(function(response) { return response.text(); })176 .then(function(body) {177 assert_equals(body, 'some body',178 'Cache.put should accept a string as request.');179 });180 }, 'Cache.put with a string request');181cache_test(function(cache) {182 return assert_promise_rejects(183 cache.put(new Request(test_url), 'Hello world!'),184 new TypeError(),185 'Cache.put should only accept a Response object as the response.');186 }, 'Cache.put with an invalid response');187cache_test(function(cache) {188 return assert_promise_rejects(189 cache.put(new Request('file:///etc/passwd'),190 new Response(test_body)),191 new TypeError(),192 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.');193 }, 'Cache.put with a non-HTTP/HTTPS request');194cache_test(function(cache) {195 var response = new Response(test_body);196 return cache.put(new Request('relative-url'), response.clone())197 .then(function() {198 return cache.match(new URL('relative-url', location.href).href);199 })200 .then(function(result) {201 assert_response_equals(result, response,202 'Cache.put should accept a relative URL ' +203 'as the request.');204 });205 }, 'Cache.put with a relative URL');206cache_test(function(cache) {207 var request = new Request('http://example.com/foo', { method: 'HEAD' });208 return assert_promise_rejects(209 cache.put(request, new Response(test_body)),210 new TypeError(),211 'Cache.put should throw a TypeError for non-GET requests.');212 }, 'Cache.put with a non-GET request');213cache_test(function(cache) {214 return assert_promise_rejects(215 cache.put(new Request(test_url), null),216 new TypeError(),217 'Cache.put should throw a TypeError for a null response.');218 }, 'Cache.put with a null response');219cache_test(function(cache) {220 var request = new Request(test_url, {method: 'POST', body: test_body});221 return assert_promise_rejects(222 cache.put(request, new Response(test_body)),223 new TypeError(),224 'Cache.put should throw a TypeError for a POST request.');225 }, 'Cache.put with a POST request');226cache_test(function(cache) {227 var response = new Response(test_body);228 assert_false(response.bodyUsed,229 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +230 'Response.bodyUsed should be initially false.');231 return response.text().then(function() {232 assert_true(233 response.bodyUsed,234 '[https://fetch.spec.whatwg.org/#concept-body-consume-body] ' +235 'The text() method should make the body disturbed.');236 var request = new Request(test_url);237 return cache.put(request, response).then(() => {238 assert_unreached('cache.put should be rejected');239 }, () => {});240 });241 }, 'Cache.put with a used response body');242cache_test(function(cache) {243 var response = new Response(test_body);244 return cache.put(new Request(test_url), response)245 .then(function() {246 assert_throws(new TypeError(), () => response.body.getReader());247 });248 }, 'getReader() after Cache.put');249cache_test(function(cache) {250 return assert_promise_rejects(251 cache.put(new Request(test_url),252 new Response(test_body, { headers: { VARY: '*' }})),253 new TypeError(),254 'Cache.put should reject VARY:* Responses with a TypeError.');255 }, 'Cache.put with a VARY:* Response');256cache_test(function(cache) {257 return assert_promise_rejects(258 cache.put(new Request(test_url),259 new Response(test_body,260 { headers: { VARY: 'Accept-Language,*' }})),261 new TypeError(),262 'Cache.put should reject Responses with an embedded VARY:* with a ' +263 'TypeError.');264 }, 'Cache.put with an embedded VARY:* Response');...
cache-add.js
Source:cache-add.js
2 importScripts('/resources/testharness.js');3 importScripts('../resources/testharness-helpers.js');4 importScripts('../resources/test-helpers.js');5}6cache_test(function(cache) {7 return assert_promise_rejects(8 cache.add(),9 new TypeError(),10 'Cache.add should throw a TypeError when no arguments are given.');11 }, 'Cache.add called with no arguments');12cache_test(function(cache) {13 return cache.add('../resources/simple.txt')14 .then(function(result) {15 assert_equals(result, undefined,16 'Cache.add should resolve with undefined on success.');17 return cache.match('../resources/simple.txt');18 })19 .then(function(response) {20 assert_class_string(response, 'Response',21 'Cache.add should put a resource in the cache.');22 return response.text();23 })24 .then(function(body) {25 assert_equals(body, 'a simple text file\n',26 'Cache.add should retrieve the correct body.');27 });28 }, 'Cache.add called with relative URL specified as a string');29cache_test(function(cache) {30 return assert_promise_rejects(31 cache.add('javascript://this-is-not-http-mmkay'),32 new TypeError(),33 'Cache.add should throw a TypeError for non-HTTP/HTTPS URLs.');34 }, 'Cache.add called with non-HTTP/HTTPS URL');35cache_test(function(cache) {36 var request = new Request('../resources/simple.txt');37 return cache.add(request)38 .then(function(result) {39 assert_equals(result, undefined,40 'Cache.add should resolve with undefined on success.');41 });42 }, 'Cache.add called with Request object');43cache_test(function(cache) {44 var request = new Request('../resources/simple.txt',45 {method: 'POST', body: 'This is a body.'});46 return assert_promise_rejects(47 cache.add(request),48 new TypeError(),49 'Cache.add should throw a TypeError for non-GET requests.');50 }, 'Cache.add called with POST request');51cache_test(function(cache) {52 var request = new Request('../resources/simple.txt');53 return cache.add(request)54 .then(function(result) {55 assert_equals(result, undefined,56 'Cache.add should resolve with undefined on success.');57 })58 .then(function() {59 return cache.add(request);60 })61 .then(function(result) {62 assert_equals(result, undefined,63 'Cache.add should resolve with undefined on success.');64 });65 }, 'Cache.add called twice with the same Request object');66cache_test(function(cache) {67 var request = new Request('../resources/simple.txt');68 return request.text()69 .then(function() {70 assert_false(request.bodyUsed);71 })72 .then(function() {73 return cache.add(request);74 });75 }, 'Cache.add with request with null body (not consumed)');76cache_test(function(cache) {77 return assert_promise_rejects(78 cache.add('this-does-not-exist-please-dont-create-it'),79 new TypeError(),80 'Cache.add should reject if response is !ok');81 }, 'Cache.add with request that results in a status of 404');82cache_test(function(cache) {83 return assert_promise_rejects(84 cache.add('../resources/fetch-status.php?status=500'),85 new TypeError(),86 'Cache.add should reject if response is !ok');87 }, 'Cache.add with request that results in a status of 500');88cache_test(function(cache) {89 return assert_promise_rejects(90 cache.addAll(),91 new TypeError(),92 'Cache.addAll with no arguments should throw TypeError.');93 }, 'Cache.addAll with no arguments');94cache_test(function(cache) {95 // Assumes the existence of ../resources/simple.txt and ../resources/blank.html96 var urls = ['../resources/simple.txt', undefined, '../resources/blank.html'];97 return assert_promise_rejects(98 cache.addAll(),99 new TypeError(),100 'Cache.addAll should throw TypeError for an undefined argument.');101 }, 'Cache.addAll with a mix of valid and undefined arguments');102cache_test(function(cache) {103 return cache.addAll([])104 .then(function(result) {105 assert_equals(result, undefined,106 'Cache.addAll should resolve with undefined on ' +107 'success.');108 return cache.keys();109 })110 .then(function(result) {111 assert_equals(result.length, 0,112 'There should be no entry in the cache.');113 });114 }, 'Cache.addAll with an empty array');115cache_test(function(cache) {116 // Assumes the existence of ../resources/simple.txt and117 // ../resources/blank.html118 var urls = ['../resources/simple.txt',119 self.location.href,120 '../resources/blank.html'];121 return cache.addAll(urls)122 .then(function(result) {123 assert_equals(result, undefined,124 'Cache.addAll should resolve with undefined on ' +125 'success.');126 return Promise.all(127 urls.map(function(url) { return cache.match(url); }));128 })129 .then(function(responses) {130 assert_class_string(131 responses[0], 'Response',132 'Cache.addAll should put a resource in the cache.');133 assert_class_string(134 responses[1], 'Response',135 'Cache.addAll should put a resource in the cache.');136 assert_class_string(137 responses[2], 'Response',138 'Cache.addAll should put a resource in the cache.');139 return Promise.all(140 responses.map(function(response) { return response.text(); }));141 })142 .then(function(bodies) {143 assert_equals(144 bodies[0], 'a simple text file\n',145 'Cache.add should retrieve the correct body.');146 assert_equals(147 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',148 'Cache.add should retrieve the correct body.');149 });150 }, 'Cache.addAll with string URL arguments');151cache_test(function(cache) {152 // Assumes the existence of ../resources/simple.txt and153 // ../resources/blank.html154 var urls = ['../resources/simple.txt',155 self.location.href,156 '../resources/blank.html'];157 var requests = urls.map(function(url) {158 return new Request(url);159 });160 return cache.addAll(requests)161 .then(function(result) {162 assert_equals(result, undefined,163 'Cache.addAll should resolve with undefined on ' +164 'success.');165 return Promise.all(166 urls.map(function(url) { return cache.match(url); }));167 })168 .then(function(responses) {169 assert_class_string(170 responses[0], 'Response',171 'Cache.addAll should put a resource in the cache.');172 assert_class_string(173 responses[1], 'Response',174 'Cache.addAll should put a resource in the cache.');175 assert_class_string(176 responses[2], 'Response',177 'Cache.addAll should put a resource in the cache.');178 return Promise.all(179 responses.map(function(response) { return response.text(); }));180 })181 .then(function(bodies) {182 assert_equals(183 bodies[0], 'a simple text file\n',184 'Cache.add should retrieve the correct body.');185 assert_equals(186 bodies[2], '<!DOCTYPE html>\n<title>Empty doc</title>\n',187 'Cache.add should retrieve the correct body.');188 });189 }, 'Cache.addAll with Request arguments');190cache_test(function(cache) {191 // Assumes that ../resources/simple.txt and ../resources/blank.html exist.192 // The second resource does not.193 var urls = ['../resources/simple.txt',194 'this-resource-should-not-exist',195 '../resources/blank.html'];196 var requests = urls.map(function(url) {197 return new Request(url);198 });199 return assert_promise_rejects(200 cache.addAll(requests),201 new TypeError(),202 'Cache.addAll should reject with TypeError if any request fails')203 .then(function() {204 return Promise.all(urls.map(function(url) { return cache.match(url); }));205 })206 .then(function(matches) {207 assert_array_equals(208 matches,209 [undefined, undefined, undefined],210 'If any response fails, no response should be added to cache');211 });212 }, 'Cache.addAll with a mix of succeeding and failing requests');213cache_test(function(cache) {214 var request = new Request('../resources/simple.txt');215 return assert_promise_rejects(216 cache.addAll([request, request]),217 'InvalidStateError',218 'Cache.addAll should throw InvalidStateError if the same request is added ' +219 'twice.');220 }, 'Cache.addAll called with the same Request object specified twice');...
Using AI Code Generation
1var wpt = require('wpt');2 if (err) {3 console.log(err);4 } else {5 console.log(data);6 }7});8var wpt = require('wpt');9wpt.get_locations(function(err, data) {10 if (err) {11 console.log(err);12 } else {13 console.log(data);14 }15});16var wpt = require('wpt');17wpt.get_testers(function(err, data) {18 if (err) {19 console.log(err);20 } else {21 console.log(data);22 }23});24var wpt = require('wpt');25 if (err) {26 console.log(err);27 } else {28 console.log(data);29 }30});31var wpt = require('wpt');32 if (err) {33 console.log(err);34 } else {35 console.log(data);36 }37});38var wpt = require('wpt');39 if (err) {40 console.log(err);41 } else {42 console.log(data);43 }44});45var wpt = require('wpt');46 if (err) {47 console.log(err);48 } else {49 console.log(data);50 }51});52var wpt = require('wpt');
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.log(err);4 console.log('Test Results: ' + data.data.testId);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.log(err);7 console.log(data.data.median.firstView);8 });9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org');12 if (err) return console.log(err);13 console.log('Test Results: ' + data.data.testId);14 wpt.getTestResults(data.data.testId, function(err, data) {15 if (err) return console.log(err);16 console.log(data.data.median.firstView);17 });18});19var wpt = require('webpagetest');20var wpt = new WebPageTest('www.webpagetest.org');21 if (err) return console.log(err);22 console.log('Test Results: ' + data.data.testId);23 wpt.getTestResults(data.data.testId, function(err, data) {24 if (err) return console.log(err);25 console.log(data.data.median.firstView);26 });27});28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org');30 if (err) return console.log(err);31 console.log('Test Results: ' + data.data.testId);32 wpt.getTestResults(data.data.testId, function(err, data) {33 if (err) return console.log(err);34 console.log(data.data.median.firstView);35 });36});37var wpt = require('webpagetest');38var wpt = new WebPageTest('www.webpagetest.org');
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3 if(err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');11 if(err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');19 if(err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25{ statusCode: 400,26 { statusCode: 400,27 data: 'Invalid test ID' } }28var wpt = require('wpt');29var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');30 if(err) {31 console.log(err);32 } else {33 console.log(data);34 }35});
Using AI Code Generation
1var cache = require('wpt-cache');2cache.cache_test(function (err, data) {3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var cache = require('wpt-cache');10cache.cache_test(function (err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var cache = require('wpt-cache');18cache.cache_test(function (err, data) {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var cache = require('wpt-cache');26cache.cache_test(function (err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var cache = require('wpt-cache');34cache.cache_test(function (err, data) {35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var cache = require('wpt-cache');42cache.cache_test(function (err, data) {43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var cache = require('wpt-cache');50cache.cache_test(function (err, data) {51 if (err) {52 console.log(err);53 } else {54 console.log(data);55 }56});57var cache = require('wpt-cache');58cache.cache_test(function (err, data) {59 if (err) {60 console.log(err);61 } else {62 console.log(data);63 }64});65var cache = require('wpt-cache');66cache.cache_test(function (err, data
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log(data);5});6WebPageTest.prototype.cache_test = function(url, options, callback) {7 var self = this;8 if (typeof options === 'function') {9 callback = options;10 options = {};11 }12 options.url = url;13 options.location = options.location || 'Dulles:Chrome';14 options.connectivity = options.connectivity || 'Cable';15 options.label = options.label || '';16 options.video = options.video || true;17 options.runs = options.runs || 1;18 self._post('cacheTest.php', options, function(err, data) {19 if (err) return callback(err);20 callback(null, data.data);21 });22};
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Barack Obama');3wiki.get(function(err, resp) {4 console.log(resp);5});6var wptools = require('wptools');7var wiki = wptools.page('Barack Obama');8wiki.get(function(err, resp) {9 console.log(resp);10});11var wptools = require('wptools');12var wiki = wptools.page('Barack Obama');13wiki.get(function(err, resp) {14 console.log(resp);15});16var wptools = require('wptools');17var wiki = wptools.page('Barack Obama');18wiki.get(function(err, resp) {19 console.log(resp);20});
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!!