Best JavaScript code snippet using wpt
reporting-subresource-corp.https.window.js
Source:reporting-subresource-corp.https.window.js
1// META: timeout=long2// META: script=/common/utils.js3// META: script=/common/get-host-info.sub.js4// META: script=/service-workers/service-worker/resources/test-helpers.sub.js5const {ORIGIN, REMOTE_ORIGIN} = get_host_info();6const BASE = "/html/cross-origin-embedder-policy/resources";7const REPORTING_FRAME_URL = `${ORIGIN}${BASE}/reporting-empty-frame.html` +8 '?pipe=header(cross-origin-embedder-policy,credentialless)' +9 '&token=${token()}';10async function observeReports(global, expected_count) {11 const reports = [];12 const receivedEveryReports = new Promise(resolve => {13 if (expected_count == 0)14 resolve();15 const observer = new global.ReportingObserver((rs) => {16 for (const r of rs) {17 reports.push(r.toJSON());18 }19 if (expected_count <= reports.length)20 resolve();21 });22 observer.observe();23 });24 await receivedEveryReports;25 // Wait 1000ms more to catch additionnal unexpected reports.26 await new Promise(r => step_timeout(r, 1000));27 return reports;28}29async function fetchInFrame(t, frameUrl, url, expected_count) {30 const frame = await with_iframe(frameUrl);31 t.add_cleanup(() => frame.remove());32 const init = { mode: 'no-cors', cache: 'no-store' };33 let future_reports = observeReports(frame.contentWindow, expected_count);34 await frame.contentWindow.fetch(url, init).catch(() => {});35 return await future_reports;36}37function checkReport(report, contextUrl, blockedUrl, disposition, destination) {38 assert_equals(report.type, 'coep');39 assert_equals(report.url, contextUrl);40 assert_equals(report.body.type, 'corp');41 assert_equals(report.body.blockedURL, blockedUrl);42 assert_equals(report.body.disposition, disposition);43 assert_equals(report.body.destination, destination);44}45// A redirection is used, so that the initial request is same-origin and is46// proxyied through the service worker. The ServiceWorker is COEP:unsafe-none,47// so it will make the cross-origin request with credentials. The fetch will48// succeed, but the response will be blocked by CORP when entering the49// COEP:credentialless document.50// https://github.com/w3c/ServiceWorker/issues/159251promise_test(async (t) => {52 const url = `${ORIGIN}/common/redirect.py?location=` +53 encodeURIComponent(`${REMOTE_ORIGIN}/common/text-plain.txt`);54 const WORKER_URL = `${ORIGIN}${BASE}/sw.js`;55 const reg = await service_worker_unregister_and_register(56 t, WORKER_URL, REPORTING_FRAME_URL);57 t.add_cleanup(() => reg.unregister());58 const worker = reg.installing || reg.waiting || reg.active;59 worker.addEventListener('error', t.unreached_func('Worker.onerror'));60 await wait_for_state(t, worker, 'activated');61 const reports = await fetchInFrame(t, REPORTING_FRAME_URL, url, 1);62 assert_equals(reports.length, 1);63 checkReport(reports[0], REPORTING_FRAME_URL, url, 'enforce', '');...
reporting-subresource-corp.tentative.https.window.js
Source:reporting-subresource-corp.tentative.https.window.js
1// META: timeout=long2// META: script=/common/utils.js3// META: script=/common/get-host-info.sub.js4// META: script=/service-workers/service-worker/resources/test-helpers.sub.js5const {ORIGIN, REMOTE_ORIGIN} = get_host_info();6const BASE = "/html/cross-origin-embedder-policy/resources";7const REPORTING_FRAME_URL = `${ORIGIN}${BASE}/reporting-empty-frame.html` +8 '?pipe=header(cross-origin-embedder-policy,credentialless)' +9 '&token=${token()}';10async function observeReports(global, expected_count) {11 const reports = [];12 const receivedEveryReports = new Promise(resolve => {13 if (expected_count == 0)14 resolve();15 const observer = new global.ReportingObserver((rs) => {16 for (const r of rs) {17 reports.push(r.toJSON());18 }19 if (expected_count <= reports.length)20 resolve();21 });22 observer.observe();23 });24 await receivedEveryReports;25 // Wait 1000ms more to catch additionnal unexpected reports.26 await new Promise(r => step_timeout(r, 1000));27 return reports;28}29async function fetchInFrame(t, frameUrl, url, expected_count) {30 const frame = await with_iframe(frameUrl);31 t.add_cleanup(() => frame.remove());32 const init = { mode: 'no-cors', cache: 'no-store' };33 let future_reports = observeReports(frame.contentWindow, expected_count);34 await frame.contentWindow.fetch(url, init).catch(() => {});35 return await future_reports;36}37function checkReport(report, contextUrl, blockedUrl, disposition, destination) {38 assert_equals(report.type, 'coep');39 assert_equals(report.url, contextUrl);40 assert_equals(report.body.type, 'corp');41 assert_equals(report.body.blockedURL, blockedUrl);42 assert_equals(report.body.disposition, disposition);43 assert_equals(report.body.destination, destination);44}45// A redirection is used, so that the initial request is same-origin and is46// proxyied through the service worker. The ServiceWorker is COEP:unsafe-none,47// so it will make the cross-origin request with credentials. The fetch will48// succeed, but the response will be blocked by CORP when entering the49// COEP:credentialless document.50// https://github.com/w3c/ServiceWorker/issues/159251promise_test(async (t) => {52 const url = `${ORIGIN}/common/redirect.py?location=` +53 encodeURIComponent(`${REMOTE_ORIGIN}/common/text-plain.txt`);54 const WORKER_URL = `${ORIGIN}${BASE}/sw.js`;55 const reg = await service_worker_unregister_and_register(56 t, WORKER_URL, REPORTING_FRAME_URL);57 t.add_cleanup(() => reg.unregister());58 const worker = reg.installing || reg.waiting || reg.active;59 worker.addEventListener('error', t.unreached_func('Worker.onerror'));60 await wait_for_state(t, worker, 'activated');61 const reports = await fetchInFrame(t, REPORTING_FRAME_URL, url, 1);62 assert_equals(reports.length, 1);63 checkReport(reports[0], REPORTING_FRAME_URL, url, 'enforce', '');...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');3wpt.getLocations(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10### WebPageTest(host, apiKey)11### .runTest(url, options, callback)
Using AI Code Generation
1const wptService = require('./services/wptService');2wptService.receivedEveryReports().then((result) => {3 console.log(result);4}).catch((error) => {5 console.log(error);6});7const wptService = require('./services/wptService');8wptService.receivedEveryReports().then((result) => {9 console.log(result);10}).catch((error) => {11 console.log(error);12});13const wptService = require('./services/wptService');14wptService.receivedEveryReports().then((result) => {15 console.log(result);16}).catch((error) => {17 console.log(error);18});19const wptService = require('./services/wptService');20wptService.receivedEveryReports().then((result) => {21 console.log(result);22}).catch((error) => {23 console.log(error);24});25const wptService = require('./services/wptService');26wptService.receivedEveryReports().then((result) => {27 console.log(result);28}).catch((error) => {29 console.log(error);30});31const wptService = require('./services/wptService');32wptService.receivedEveryReports().then((result) => {33 console.log(result);34}).catch((error) => {35 console.log(error);36});37const wptService = require('./services/wptService');38wptService.receivedEveryReports().then((result) => {39 console.log(result);40}).catch((error) => {41 console.log(error);42});43const wptService = require('./services/wptService');44wptService.receivedEveryReports().then((result) => {45 console.log(result);46}).catch((error
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getLocations(function(err, data) {4 var location = data.data[0].location;5 }, function(err, data) {6 wpt.receivedEveryReports(data.data.testId, function(err, reports) {7 if (err) {8 console.log(err);9 } else {10 console.log(reports);11 }12 });13 });14});15WebPageTest.prototype.receivedEveryReports = function(testId, callback) {16 this.getTestResults(testId, function(err, data) {17 if (err) {18 callback(err);19 } else {20 if (data.statusCode === 200) {21 callback(null, data);22 } else {23 setTimeout(function() {24 this.receivedEveryReports(testId, callback);25 }.bind(this), 1000);26 }27 }28 });29};30WebPageTest.prototype.getTestResults = function(testId, callback) {31 this.apiRequest('testStatus.php', {32 }, callback);33};34WebPageTest.prototype.apiRequest = function(path, params, callback) {35 if (typeof params === 'function') {36 callback = params;37 params = {};38 }39 var url = this.apiRoot + path;40 if (Object.keys(params).length > 0) {41 url += '?' + querystring.stringify(params);42 }43 request.get(url, function(err, response, body) {44 if (err) {45 return callback(err);46 }47 var data = JSON.parse(body);48 callback(null, data);49 });50};51WebPageTest.prototype.getLocations = function(callback) {52 this.apiRequest('getLocations.php', callback);53};54WebPageTest.prototype.runTest = function(url, options, callback) {55 if (typeof options === 'function') {
Using AI Code Generation
1var wptService = require('wptService');2wptService.receivedEveryReports( function (err, result) {3 if (err) {4 console.log(err);5 }6 else {7 console.log(result);8 }9});10var wptService = require('wptService');11wptService.getReport( function (err, result) {12 if (err) {13 console.log(err);14 }15 else {16 console.log(result);17 }18});19var wptService = require('wptService');20wptService.getReport( function (err, result) {21 if (err) {22 console.log(err);23 }24 else {25 console.log(result);26 }27});28var wptService = require('wptService');29wptService.getReport( function (err, result) {30 if (err) {31 console.log(err);32 }33 else {34 console.log(result);35 }36});37var wptService = require('wptService');38wptService.getReport( function (err, result) {39 if (err) {40 console.log(err);41 }42 else {43 console.log(result);44 }45});46var wptService = require('wptService');47wptService.getReport( function (err, result) {48 if (err) {49 console.log(err);50 }51 else {52 console.log(result);53 }54});55var wptService = require('wptService');56wptService.getReport( function (err, result) {57 if (err) {58 console.log(err);59 }60 else {61 console.log(result);62 }63});64var wptService = require('wptService');65wptService.getReport( function (err, result) {66 if (err) {67 console.log(err);68 }69 else {70 console.log(result);71 }72});73var wptService = require('wptService');
Using AI Code Generation
1var wpt = require('./wpt.js');2var wptObj = new wpt('API_KEY');3wptObj.receivedEveryReports('test1', function(data){4 console.log(data);5});6var request = require('request');7var Wpt = function(apiKey){8 this.apiKey = apiKey;9 this.request = request;10};11Wpt.prototype.receivedEveryReports = function(testId, callback){12 var url = this.baseURL + 'jsonResult.php?test=' + testId + '&f=json';13 this.request(url, function(err, response, body){14 if(err){15 callback(err);16 }else{17 var data = JSON.parse(body);18 callback(data);19 }20 });21};22module.exports = Wpt;
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!!