Best JavaScript code snippet using wpt
service-worker-coep-none-proxy.https.window.js
Source:service-worker-coep-none-proxy.https.window.js
1// META: script=/common/get-host-info.sub.js2// META: script=/common/utils.js3// META: script=/common/dispatcher/dispatcher.js4// META: script=./resources/common.js5// META: script=/service-workers/service-worker/resources/test-helpers.sub.js6const same_origin = get_host_info().HTTPS_ORIGIN;7const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;8promise_test(async test => {9 const this_token_1 = token();10 const this_token_2 = token();11 // Register a COEP:none ServiceWorker.12 const sw_token = token();13 const sw_url = executor_service_worker_path + coep_none + `&uuid=${sw_token}`;14 // Executors should be controlled by the service worker.15 const scope = executor_path;16 const sw_registration =17 await service_worker_unregister_and_register(test, sw_url, scope);18 test.add_cleanup(() => sw_registration.unregister());19 await wait_for_state(test, sw_registration.installing, 'activated');20 // Configure the ServiceWorker to proxy the fetch requests. Wait for the21 // worker to be installed and activated.22 send(sw_token, `23 fetchHandler = event => {24 if (!event.request.url.includes("/proxied"))25 return;26 send("${this_token_1}", "ServiceWorker: Proxying");27 // Response with a cross-origin no-cors resource.28 const url = "${cross_origin}" + "/common/blank.html}";29 event.respondWith(new Promise(async resolve => {30 try {31 let response = await fetch(url, {32 mode: "no-cors",33 credentials: "include"34 });35 send("${this_token_1}", "ServiceWorker: Fetch success");36 resolve(response);37 } catch (error) {38 send("${this_token_1}", "ServiceWorker: Fetch failure");39 resolve(new Response("", {status: 400}));40 }41 }));42 }43 await clients.claim();44 send("${this_token_1}", serviceWorker.state);45 `)46 assert_equals(await receive(this_token_1), "activated");47 // Create a COEP:credentialless document.48 const document_token = environments["document"](coep_credentialless)[0];49 // The document fetches a same-origin no-cors resource. The requests needs to50 // be same-origin to be handled by the ServiceWorker.51 send(document_token, `52 try {53 const response = await fetch("/proxied", {54 mode: "no-cors",55 credentials: "include"56 });57 send("${this_token_2}", "Document: Fetch success");58 } catch (error) {59 send("${this_token_2}", "Document: Fetch error");60 }61 `);62 // The COEP:unsafe-none ServiceWorker is able to handle the cross-origin63 // no-cors request, requested with credentials.64 assert_equals(await receive(this_token_1), "ServiceWorker: Proxying");65 assert_equals(await receive(this_token_1), "ServiceWorker: Fetch success");66 // However, the COEP:credentialless Document is disallowed by CORP to get it.67 assert_equals(await receive(this_token_2), "Document: Fetch error");68 // test.add_cleanup doesn't allow waiting for a promise. Unregistering a69 // ServiceWorker is an asynchronous operation. It might not be completed on70 // time for the next test. Do it here for extra flakiness safety.71 await sw_registration.unregister()...
service-worker-coep-credentialless-proxy.https.window.js
Source:service-worker-coep-credentialless-proxy.https.window.js
1// META: script=/common/get-host-info.sub.js2// META: script=/common/utils.js3// META: script=/common/dispatcher/dispatcher.js4// META: script=./resources/common.js5// META: script=/service-workers/service-worker/resources/test-helpers.sub.js6const same_origin = get_host_info().HTTPS_ORIGIN;7const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;8promise_test(async test => {9 const this_token_1 = token();10 const this_token_2 = token();11 // Register a COEP:credentialless ServiceWorker.12 const sw_token = token();13 const sw_url =14 executor_service_worker_path + coep_credentialless + `&uuid=${sw_token}`;15 // Executors should be controlled by the service worker.16 const scope = executor_path;17 const sw_registration =18 await service_worker_unregister_and_register(test, sw_url, scope);19 test.add_cleanup(() => sw_registration.unregister());20 await wait_for_state(test, sw_registration.installing, 'activated');21 // Configure the ServiceWorker to proxy the fetch requests. Wait for the22 // worker to be installed and activated.23 send(sw_token, `24 fetchHandler = event => {25 if (!event.request.url.includes("/proxied"))26 return;27 send("${this_token_1}", "ServiceWorker: Proxying");28 // Response with a cross-origin no-cors resource.29 const url = "${cross_origin}" + "/common/blank.html}";30 event.respondWith(new Promise(async resolve => {31 try {32 let response = await fetch(url, {33 mode: "no-cors",34 credentials: "include"35 });36 send("${this_token_1}", "ServiceWorker: Fetch success");37 resolve(response);38 } catch (error) {39 send("${this_token_1}", "ServiceWorker: Fetch failure");40 resolve(new Response("", {status: 400}));41 }42 }));43 }44 await clients.claim();45 send("${this_token_1}", serviceWorker.state);46 `)47 assert_equals(await receive(this_token_1), "activated");48 // Create a COEP:credentialless document.49 const document_token = environments["document"](coep_credentialless)[0];50 // The document fetches a same-origin no-cors resource. The requests needs to51 // be same-origin to be handled by the ServiceWorker.52 send(document_token, `53 try {54 const response = await fetch("/proxied", { mode: "no-cors", });55 send("${this_token_2}", "Document: Fetch success");56 } catch (error) {57 send("${this_token_2}", "Document: Fetch error");58 }59 `);60 // The COEP:credentialless ServiceWorker is able to handle the cross-origin61 // no-cors request, requested with credentials.62 assert_equals(await receive(this_token_1), "ServiceWorker: Proxying");63 assert_equals(await receive(this_token_1), "ServiceWorker: Fetch success");64 // The COEP:credentialless Document is allowed by CORP to get it.65 assert_equals(await receive(this_token_2), "Document: Fetch success");66 // test.add_cleanup doesn't allow waiting for a promise. Unregistering a67 // ServiceWorker is an asynchronous operation. It might not be completed on68 // time for the next test. Do it here for extra flakiness safety.69 await sw_registration.unregister()...
service-worker-coep-credentialless-proxy.tentative.https.window.js
Source:service-worker-coep-credentialless-proxy.tentative.https.window.js
1// META: script=/common/get-host-info.sub.js2// META: script=/common/utils.js3// META: script=/common/dispatcher/dispatcher.js4// META: script=./resources/common.js5// META: script=/service-workers/service-worker/resources/test-helpers.sub.js6const same_origin = get_host_info().HTTPS_ORIGIN;7const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;8promise_test(async test => {9 const this_token_1 = token();10 const this_token_2 = token();11 // Register a COEP:credentialless ServiceWorker.12 const sw_token = token();13 const sw_url =14 executor_service_worker_path + coep_credentialless + `&uuid=${sw_token}`;15 // Executors should be controlled by the service worker.16 const scope = executor_path;17 const sw_registration =18 await service_worker_unregister_and_register(test, sw_url, scope);19 test.add_cleanup(() => sw_registration.unregister());20 await wait_for_state(test, sw_registration.installing, 'activated');21 // Configure the ServiceWorker to proxy the fetch requests. Wait for the22 // worker to be installed and activated.23 send(sw_token, `24 fetchHandler = event => {25 if (!event.request.url.includes("/proxied"))26 return;27 send("${this_token_1}", "ServiceWorker: Proxying");28 // Response with a cross-origin no-cors resource.29 const url = "${cross_origin}" + "/common/blank.html}";30 event.respondWith(new Promise(async resolve => {31 try {32 let response = await fetch(url, {33 mode: "no-cors",34 credentials: "include"35 });36 send("${this_token_1}", "ServiceWorker: Fetch success");37 resolve(response);38 } catch (error) {39 send("${this_token_1}", "ServiceWorker: Fetch failure");40 resolve(new Response("", {status: 400}));41 }42 }));43 }44 await clients.claim();45 send("${this_token_1}", serviceWorker.state);46 `)47 assert_equals(await receive(this_token_1), "activated");48 // Create a COEP:credentialless document.49 const document_token = environments["document"](coep_credentialless)[0];50 // The document fetches a same-origin no-cors resource. The requests needs to51 // be same-origin to be handled by the ServiceWorker.52 send(document_token, `53 try {54 const response = await fetch("/proxied", { mode: "no-cors", });55 send("${this_token_2}", "Document: Fetch success");56 } catch (error) {57 send("${this_token_2}", "Document: Fetch error");58 }59 `);60 // The COEP:credentialless ServiceWorker is able to handle the cross-origin61 // no-cors request, requested with credentials.62 assert_equals(await receive(this_token_1), "ServiceWorker: Proxying");63 assert_equals(await receive(this_token_1), "ServiceWorker: Fetch success");64 // The COEP:credentialless Document is allowed by CORP to get it.65 assert_equals(await receive(this_token_2), "Document: Fetch success");66 // test.add_cleanup doesn't allow waiting for a promise. Unregistering a67 // ServiceWorker is an asynchronous operation. It might not be completed on68 // time for the next test. Do it here for extra flakiness safety.69 await sw_registration.unregister()...
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var wikipage = wptools.page('John_F._Kennedy');4wikipage.get(function(err, resp) {5 var data = resp.data;6 var infobox = data.infobox;7 var infoboxJson = JSON.stringify(infobox);8 fs.writeFile('infobox.json', infoboxJson, function(err) {9 if (err) throw err;10 });11});12var wptools = require('wptools');13var fs = require('fs');14var wikipage = wptools.page('John_F._Kennedy');15wikipage.get(function(err, resp) {16 var data = resp.data;17 var infobox = data.infobox;18 var infoboxJson = JSON.stringify(infobox);19 fs.writeFile('infobox.json', infoboxJson, function(err) {20 if (err) throw err;21 });22});
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 page.getWikidata().then(function(data) {4 console.log(data);5 });6});
Using AI Code Generation
1const wpt = require('webpagetest');2const wpt = new WebPageTest('www.webpagetest.org', 'A.3c7a2e1e5f5e3f3a5a3b5f5a5a5a5a5a5');3}, function(err, data) {4 if (err) return console.error(err);5 console.log('Test status:', data.statusText);6 console.log('View your test results at', data.data.userUrl);7});8const wpt = require('webpagetest');9const wpt = new WebPageTest('www.webpagetest.org', 'A.3c7a2e1e5f5e3f3a5a3b5f5a5a5a5a5a5');10}, function(err, data) {11 if (err) return console.error(err);12 console.log('Test status:', data.statusText);13 console.log('View your test results at', data.data.userUrl);14});15const wpt = require('webpagetest');16const wpt = new WebPageTest('www.webpagetest.org', 'A.3c7a2e1e5f5e3f3a5a3b5f5a5a5a5a5a5');17}, function(err, data) {18 if (err) return console.error(err);19 console.log('Test status:', data.statusText);20 console.log('View your test results at', data.data.userUrl);21});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var webpagetest = new wpt(options);5 if (err) return console.error(err);6 console.log('Test submitted. Polling for results...');7 webpagetest.getTestResults(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log('First View (loadTime):', data.data.average.firstView.loadTime);10 console.log('Repeat View (loadTime):', data.data.average.repeatView.loadTime);11 });12});13var wpt = require('webpagetest');14var options = {15};16var webpagetest = new wpt(options);17 if (err) return console.error(err);18 console.log('Test submitted. Polling for results...');19 webpagetest.getTestResults(data.data.testId, function(err, data) {20 if (err) return console.error(err);21 console.log('First View (loadTime):', data.data.average.firstView.loadTime);22 console.log('Repeat View (loadTime):', data.data.average.repeatView.loadTime);23 });24});
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!!