Best JavaScript code snippet using wpt
cache-put-worker.js
Source:cache-put-worker.js
...7 return new Request(test_url);8}9// Construct a generic Response object. The URL is empty. If specified |body|10// will be set as the response body string.11function new_test_response(body) {12 body = body || 'Hello world!';13 return new Response(body, {14 status: 200,15 statusText: 'OK',16 headers: [['Content-Type', 'text/plain']]17 });18}19cache_test(function(cache) {20 var request = new_test_request();21 var response = new_test_response();22 return cache.put(request, response)23 .then(function(result) {24 assert_equals(result, undefined,25 'Cache.put should resolve with undefined on success.');26 });27 }, 'Cache.put called with simple Request and Response');28cache_test(function(cache) {29 var test_url = new URL('simple.txt', location.href).href;30 var request = new Request(test_url);31 var response;32 return fetch(test_url)33 .then(function(fetch_result) {34 response = fetch_result.clone();35 return cache.put(request, fetch_result);36 })37 .then(function() {38 return cache.match(test_url);39 })40 .then(function(result) {41 assert_object_equals(result, response,42 'Cache.put should update the cache with ' +43 'new request and response.');44 return result.text();45 })46 .then(function(body) {47 assert_equals(body, 'a simple text file\n',48 'Cache.put should store response body.');49 });50 }, 'Cache.put called with Request and Response from fetch()');51cache_test(function(cache) {52 var request = new Request(test_url, {53 method: 'GET',54 body: 'Hello'55 });56 var response = new_test_response();57 assert_false(request.bodyUsed,58 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' +59 'Request.bodyUsed should be initially false.');60 return cache.put(request, response)61 .then(function() {62 assert_false(request.bodyUsed,63 'Cache.put should not consume Request body.');64 })65 .then(function() {66 return cache.match(request);67 })68 .then(function(result) {69 assert_object_equals(result, response,70 'Cache.put should store response body.');71 });72 }, 'Cache.put with Request containing a body');73cache_test(function(cache) {74 var request = new_test_request();75 var response = new_test_response();76 return cache.put(request, response)77 .then(function() {78 return cache.match(test_url);79 })80 .then(function(result) {81 assert_object_equals(result, response,82 'Cache.put should update the cache with ' +83 'new Request and Response.');84 });85 }, 'Cache.put with a Response containing an empty URL');86cache_test(function(cache) {87 var request = new_test_request();88 var response = new Response('', {89 status: 200,90 headers: [['Content-Type', 'text/plain']]91 });92 return cache.put(request, response)93 .then(function() {94 return cache.match(test_url);95 })96 .then(function(result) {97 assert_equals(result.status, 200, 'Cache.put should store status.');98 assert_equals(result.headers.get('Content-Type'), 'text/plain',99 'Cache.put should store headers.');100 return result.text();101 })102 .then(function(body) {103 assert_equals(body, '',104 'Cache.put should store response body.');105 });106 }, 'Cache.put with an empty response body');107cache_test(function(cache) {108 var test_url = new URL('fetch-status.php?status=500', location.href).href;109 var request = new Request(test_url);110 var response;111 return fetch(test_url)112 .then(function(fetch_result) {113 response = fetch_result.clone();114 return cache.put(request, fetch_result);115 })116 .then(function() {117 return cache.match(test_url);118 })119 .then(function(result) {120 assert_object_equals(result, response,121 'Cache.put should update the cache with ' +122 'new request and response.');123 return result.text();124 })125 .then(function(body) {126 assert_equals(body, '',127 'Cache.put should store response body.');128 });129 }, 'Cache.put with HTTP 500 response');130cache_test(function(cache) {131 var alternate_response = new_test_response('Lorem ipsum');132 return cache.put(new_test_request(), new_test_response())133 .then(function() {134 return cache.put(new_test_request(), alternate_response);135 })136 .then(function() {137 return cache.match(test_url);138 })139 .then(function(result) {140 assert_object_equals(result, alternate_response,141 'Cache.put should replace existing ' +142 'response with new response.');143 });144 }, 'Cache.put called twice with same Request and different Responses');145cache_test(function(cache) {146 var url = 'http://example.com/foo';147 return cache.put(url, new_test_response('some body'))148 .then(function() { return cache.match(url); })149 .then(function(response) { return response.text(); })150 .then(function(body) {151 assert_equals(body, 'some body',152 'Cache.put should accept a string as request.');153 });154 }, 'Cache.put with an string request');155cache_test(function(cache) {156 return assert_promise_rejects(157 cache.put(new_test_request(), 'Hello world!'),158 new TypeError(),159 'Cache.put should only accept a Response object as the response.');160 }, 'Cache.put with an invalid response');161cache_test(function(cache) {162 return assert_promise_rejects(163 cache.put(new Request('file:///etc/passwd'), new_test_response()),164 new TypeError(),165 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.');166 }, 'Cache.put with a non-HTTP/HTTPS request');167cache_test(function(cache) {168 var response = new_test_response();169 return cache.put(new Request('relative-url'), response)170 .then(function() {171 return cache.match(new URL('relative-url', location.href).href);172 })173 .then(function(result) {174 assert_object_equals(result, response,175 'Cache.put should accept a relative URL ' +176 'as the request.');177 });178 }, 'Cache.put with a relative URL');179cache_test(function(cache) {180 var request = new Request('http://example.com/foo', {method: 'HEAD'});181 return assert_promise_rejects(182 cache.put(request, new_test_response()),183 new TypeError(),184 'Cache.put should throw a TypeError for non-GET requests.');185 }, 'Cache.put with a non-GET request');186cache_test(function(cache) {187 return assert_promise_rejects(188 cache.put(new_test_request(), null),189 new TypeError(),190 'Cache.put should throw a TypeError for an empty response.');...
cache-delete.js
Source:cache-delete.js
...8function new_test_request() {9 return new Request(test_url);10}11// Construct a generic Response object.12function new_test_response() {13 return new Response('Hello world!', { status: 200 });14}15cache_test(function(cache, test) {16 return promise_rejects(17 test,18 new TypeError(),19 cache.delete(),20 'Cache.delete should reject with a TypeError when called with no ' +21 'arguments.');22 }, 'Cache.delete with no arguments');23cache_test(function(cache) {24 return cache.put(new_test_request(), new_test_response())25 .then(function() {26 return cache.delete(test_url);27 })28 .then(function(result) {29 assert_true(result,30 'Cache.delete should resolve with "true" if an entry ' +31 'was successfully deleted.');32 return cache.match(test_url);33 })34 .then(function(result) {35 assert_equals(result, undefined,36 'Cache.delete should remove matching entries from cache.');37 });38 }, 'Cache.delete called with a string URL');39cache_test(function(cache) {40 var request = new Request(test_url);41 return cache.put(request, new_test_response())42 .then(function() {43 return cache.delete(request);44 })45 .then(function(result) {46 assert_true(result,47 'Cache.delete should resolve with "true" if an entry ' +48 'was successfully deleted.');49 });50 }, 'Cache.delete called with a Request object');51cache_test(function(cache) {52 var request = new Request(test_url);53 var response = new_test_response();54 return cache.put(request, response)55 .then(function() {56 return cache.delete(new Request(test_url, {method: 'HEAD'}));57 })58 .then(function(result) {59 assert_false(result,60 'Cache.delete should not match a non-GET request ' +61 'unless ignoreMethod option is set.');62 return cache.match(test_url);63 })64 .then(function(result) {65 assert_response_equals(result, response,66 'Cache.delete should leave non-matching response in the cache.');67 });...
Using AI Code Generation
1var Wpt = require('webpagetest');2var wpt = new Wpt('API_KEY');3var options = {4};5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 wpt.getTestResults(data.data.testId, function(err, data) {10 if (err) {11 console.log(err);12 } else {13 console.log(data);14 }15 });16 }17});18{ [Error: Request failed with status code 400]19 { adapter: [Function: httpAdapter],20 transformRequest: { '0': [Function: transformRequest] },21 transformResponse: { '0': [Function: transformResponse] },22 { Accept: 'application/json, text/plain, */*',23 'User-Agent': 'axios/0.8.1' },24 data: undefined },25 { status: 400,26 { 'content-length': '0',27 date: 'Sat, 02 Apr 2016 10:25:37 GMT' },28 { adapter: [Function: httpAdapter],
Using AI Code Generation
1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3 console.log(data);4});5var wpt = require('webpagetest');6var client = wpt('www.webpagetest.org');7 console.log(data);8});9var wpt = require('webpagetest');10var client = wpt('www.webpagetest.org');11 console.log(data);12});13var wpt = require('webpagetest');14var client = wpt('www.webpagetest.org');15client.get_testers(function(err, data) {16 console.log(data);17});18var wpt = require('webpagetest');19var client = wpt('www.webpagetest.org');20client.get_locations(function(err, data) {21 console.log(data);22});23var wpt = require('webpagetest');24var client = wpt('www.webpagetest.org');25client.get_status(function(err, data) {26 console.log(data);27});28var wpt = require('webpagetest');29var client = wpt('www.webpagetest.org');30client.get_testers(function(err, data) {31 console.log(data);32});33var wpt = require('webpagetest');34var client = wpt('www.webpagetest.org');35client.get_testers(function(err, data) {36 console.log(data);37});38var wpt = require('webpagetest');39var client = wpt('www.webpagetest.org');40client.get_testers(function(err, data) {
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var params = {4};5wpt.runTest(test_url, params, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11var params = {12};13wpt.run_test(test_url, params, function(err, data) {14 if (err) return console.error(err);15 console.log(data);16});17### WebPageTest(url, [options])18### WebPageTest#runTest(url, [options], callback)
Using AI Code Generation
1var wpt = new WebPageTest('www.webpagetest.org');2wpt.new_test_response = function(data) {3 console.log('New test response: ' + data);4};5wpt.newTest('www.google.com', {location: 'Dulles:Chrome'}, function(err, data) {6 if (err) {7 console.log('Error: ' + err);8 } else {9 console.log('New test response: ' + data);10 }11});12var WebPageTest = require('webpagetest');13var wpt = new WebPageTest('www.webpagetest.org');14wpt.new_test_response = function(data) {15 console.log('New test response: ' + data);16};17wpt.newTest('www.google.com', {location: 'Dulles:Chrome'}, function(err, data) {18 if (err) {19 console.log('Error: ' + err);20 } else {21 console.log('New test response: ' + data);22 }23});
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!!