Best JavaScript code snippet using wpt
popup-coop-by-sw.js
Source:popup-coop-by-sw.js
1const test_token = token();2const bc = new BroadcastChannel(test_token);3// Define a page served entirely from the ServiceWorker.4const popup_url = new URL("./resources/i-do-not-exist.html", location);5const popup_normal= {6 'content-type': 'text/html',7};8const popup_coop = {9 'content-type': 'text/html',10 'cross-origin-embedder-policy': 'require-corp',11 'cross-origin-opener-policy': 'same-origin',12};13const popup_body = `14 <script>15 const bc = new BroadcastChannel('${test_token}');16 if (opener)17 bc.postMessage("An opener is set");18 else19 bc.postMessage("No opener is set");20 window.close();21 </scrip`+`t>22`;23const header_coop= `|header(cross-origin-embedder-policy,same_origin)`;24const header_coep = `|header(cross-origin-opener-policy,require-corp)`;25const sw_normal = new URL("./resources/universal-worker.js?pipe=", location);26const sw_coop = sw_normal + header_coop + header_coep;27const swap_browsing_context_group = true;28const keep_browsing_context_group = false;29const SW_SCOPE = "./resources/"30// Send a message to the |worker|. Return a promise containing its response.31function executeCommandInServiceWorker(worker, command) {32 const channel = new MessageChannel();33 const response = new Promise(resolve => channel.port1.onmessage = resolve);34 worker.postMessage(command, [ channel.port2 ]);35 return response;36}37function popupCoopBySwTest(test_name,38 // Test parameters39 sw_url,40 new_window_headers,41 // Test expectations:42 expected_browing_context_group) {43 promise_test(async (t) => {44 // Create a ServiceWorker and wait for its activation.45 window.id = window.id == undefined ? 0 : window.id + 1;46 const reg =47 await service_worker_unregister_and_register(t, sw_url, SW_SCOPE);48 t.add_cleanup(() => reg.unregister());49 const worker = reg.installing || reg.waiting || reg.active;50 wait_for_state(t, worker, 'active');51 // Register a fetch handler. New documents loaded will use the52 // |new_window_headers| from now and the custom response.53 const worker_script = `54 fetchHandler = event => {55 const response = new Response(\`${popup_body}\`, {56 status: 200,57 headers: ${JSON.stringify(new_window_headers)},58 });59 event.respondWith(response);60 };61 message.ports[0].postMessage('done');62 `;63 const fetch_handler_registered =64 await executeCommandInServiceWorker(worker, worker_script);65 assert_equals(fetch_handler_registered.data, "done");66 // Create a popup. The popup document's response is provided by the67 // ServiceWorker.68 bc_response = new Promise(resolve => bc.onmessage = resolve);69 const openee = window.open(popup_url);70 const {data} = await bc_response;71 await reg.unregister();72 if (expected_browing_context_group === keep_browsing_context_group) {73 // From the openee point of view: The opener is preserved, because the74 // popup is still in the same browsing context group.75 assert_equals(data, "An opener is set");76 return;77 }78 // From the openee point of view: There are no opener, because the new79 // window lives in a different browsing context group.80 assert_equals(data, "No opener is set");81 // From the opener point of view: the openee must appear closed shortly82 // after the popup navigation commit.83 const openee_closed = new Promise(resolve => {84 setInterval(() => {85 if (openee.closed)86 resolve();87 }, 100);88 });89 await openee_closed;90 }, test_name);...
origin_response.js
Source:origin_response.js
1// defaults:2// Lambda@Edge doesn't support environment variables!3const DELETE_SRV_HEADER = "true";4const HEADER_STS = "max-age=31536000; includeSubdomains; preload";5const HEADER_EXPECTCT = "max-age=0";6const HEADER_CSP = "default-src 'self';";7const HEADER_XCTO = "nosniff";8const HEADER_XFO = "DENY";9const HEADER_XSSP = "1; mode=block";10const HEADER_RF = "strict-origin-when-cross-origin";11const HEADER_PP = "geolocation=(), microphone=(), camera=(), autoplay=(), payment=(), sync-xhr=()";12const HEADER_FP = "geolocation 'none'; microphone 'none'; camera 'none'; autoplay 'none'; payment 'none'; sync-xhr 'none';";13const HEADER_COEP = "require-corp";14const HEADER_COOP = "same-origin";15const HEADER_CORP = "same-origin";16exports.handler = async (event) => {17 //Get contents of response18 const response = event.Records[0].cf.response;19 let headers = response.headers;20 const currentHeaderKeys = Object.keys(headers);21 if (DELETE_SRV_HEADER == 'true' && 'server' in headers) {22 delete headers['server'];23 }24 if (HEADER_STS != 'NULL' && !currentHeaderKeys.includes('strict-transport-security')) {25 headers['strict-transport-security'] = [{key: 'Strict-Transport-Security', value: HEADER_STS}];26 }27 if (HEADER_EXPECTCT != 'NULL' && !currentHeaderKeys.includes('expect-ct')) {28 headers['expect-ct'] = [{key: 'Expect-CT', value: HEADER_EXPECTCT}];29 }30 if (HEADER_CSP != 'NULL' && !currentHeaderKeys.includes('content-security-policy')) {31 headers['content-security-policy'] = [{key: 'Content-Security-Policy', value: HEADER_CSP}];32 }33 if (HEADER_XCTO != 'NULL' && !currentHeaderKeys.includes('x-content-type-options')) {34 headers['x-content-type-options'] = [{key: 'X-Content-Type-Options', value: HEADER_XCTO}];35 }36 if (HEADER_XFO != 'NULL' && !currentHeaderKeys.includes('x-frame-options')) {37 headers['x-frame-options'] = [{key: 'X-Frame-Options', value: HEADER_XFO}];38 }39 if (HEADER_XSSP != 'NULL' && !currentHeaderKeys.includes('x-xss-protection')) {40 headers['x-xss-protection'] = [{key: 'X-XSS-Protection', value: HEADER_XSSP}];41 }42 if (HEADER_RF != 'NULL' && !currentHeaderKeys.includes('referrer-policy')) {43 headers['referrer-policy'] = [{key: 'Referrer-Policy', value: HEADER_RF}];44 }45 if (HEADER_PP != 'NULL' && !currentHeaderKeys.includes('permissions-policy')) {46 headers['permissions-policy'] = [{key: 'Permissions-Policy', value: HEADER_PP}];47 }48 if (HEADER_FP != 'NULL' && !currentHeaderKeys.includes('feature-policy')) {49 headers['feature-policy'] = [{key: 'Feature-Policy', value: HEADER_FP}];50 }51 if (HEADER_COEP != 'NULL' && !currentHeaderKeys.includes('cross-origin-embedder-policy')) {52 headers['cross-origin-embedder-policy'] = [{key: 'Cross-Origin-Embedder-Policy', value: HEADER_COEP}];53 }54 if (HEADER_COOP != 'NULL' && !currentHeaderKeys.includes('cross-origin-opener-policy')) {55 headers['cross-origin-opener-policy'] = [{key: 'Cross-Origin-Opener-Policy', value: HEADER_COOP}];56 }57 if (HEADER_CORP != 'NULL' && !currentHeaderKeys.includes('cross-origin-resource-policy')) {58 headers['cross-origin-resource-policy'] = [{key: 'Cross-Origin-Resource-Policy', value: HEADER_CORP}];59 }60 //Return modified response61 return response;...
Using AI Code Generation
1function handler(metadata, response)2{3 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");4 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");5 response.setHeader("Content-Type", "text/html");6 response.setHeader("Access-Control-Allow-Origin", "*");7 response.write("PASS");8}
Using AI Code Generation
1function handler(metadata, response)2{3 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp")4 response.setHeader("Cross-Origin-Opener-Policy", "same-origin")5 response.setHeader("Content-Type", "text/html");6 response.write('<!DOCTYPE html><html><body>');7 response.write('<script src="/test2.js"></script>');8 response.write('</body></html>');9}10function handler(metadata, response)11{12 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp")13 response.setHeader("Cross-Origin-Opener-Policy", "same-origin")14 response.setHeader("Content-Type", "text/javascript");15 response.write('window.opener = 1;');16}17function handler(metadata, response)18{19 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp")20 response.setHeader("Cross-Origin-Opener-Policy", "same-origin")21 response.setHeader("Content-Type", "text/html");22 response.write('<!DOCTYPE html><html><body>');23 response.write('<script src="/test.js"></script>');24 response.write('</body></html>');25}26function handler(metadata, response)27{28 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp")29 response.setHeader("Cross-Origin-Opener-Policy", "same-origin")30 response.setHeader("Content-Type", "text/html");31 response.write('<!DOCTYPE html><html><body>');32 response.write('<script src="/test5.js"></script>');33 response.write('</body></html>');34}35function handler(metadata, response)36{37 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp")38 response.setHeader("Cross-Origin-Opener-Policy", "same-origin")39 response.setHeader("Content-Type", "text/javascript");40 response.write('window.opener = 1;');41}42function handler(metadata, response)43{44 response.setHeader("Cross-Origin-Embedder-P
Using AI Code Generation
1function run_test() {2 const server = createHttpServer({hosts: ["example.com"]});3 server.registerPathHandler("/", (request, response) => {4 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");5 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");6 response.setHeader("Content-Type", "text/html");7 response.write("<script src='test.js'></script>");8 });9 server.registerPathHandler("/test.js", (request, response) => {10 response.setHeader("Content-Type", "application/javascript");11 response.write("window.opener;");12 });13 server.registerPathHandler("/test2.js", (request, response) => {14 response.setHeader("Content-Type", "application/javascript");15 response.write("window.opener;");16 });17 const browser = tab.linkedBrowser;18 browser.addEventListener("load", function onLoad() {19 browser.removeEventListener("load", onLoad, true);20 const mm = browser.messageManager;21 mm.loadFrameScript("data:,(" + function() {22 addMessageListener("test", msg => {23 const script = content.document.createElement("script");24 script.src = "test2.js";25 content.document.body.appendChild(script);26 sendAsyncMessage("test", {data: content.opener});27 });28 }.toString() + ")();", true);29 mm.addMessageListener("test", function onMessage(msg) {30 is(msg.data.data, null, "opener is null");31 mm.removeMessageListener("test", onMessage);32 BrowserTestUtils.removeTab(tab).then(finish);33 });34 mm.sendAsyncMessage("test");35 }, true);36}
Using AI Code Generation
1var headers = [ ["Cross-Origin-Embedder-Policy", "require-corp"] ];2var wptserve = require("wptserve");3var server = new wptserve.WebTestServer();4server.setResponseHeaders(headers);5server.start();6var request = new XMLHttpRequest();7request.open("GET", url, true);8request.onload = function() {9 assert_equals(request.status, 200);10 assert_equals(request.response, "");11};12request.send();
Using AI Code Generation
1var wptserve = require('wptserve');2var headers = {"Cross-Origin-Embedder-Policy": "require-corp"};3function handler(request, response) {4 return wptserve.response(200, headers, "Hello, world!");5}6var server = new wptserve.WebServer(handler);7server.start();
Using AI Code Generation
1var wptserve = require('./wptserve');2var headers = [["Content-Type", "text/html"]];3var response = wptserve.response(200, headers, "Hello World");4response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");5return response;6var wptserve = require('./wptserve');7var headers = [["Content-Type", "text/html"]];8var response = wptserve.response(200, headers, "Hello World");9response.setHeader("Cross-Origin-Embedder-Policy", "unsafe-none");10return response;
Using AI Code Generation
1function run_test() {2 var url = new URL(RESOURCES_DIR + "test.html", location.href);3 url.searchParams.set("coep", "require-corp");4 url.searchParams.set("coep_report", "coep_report");5 var url2 = new URL(RESOURCES_DIR + "test.html", location.href);6 url2.searchParams.set("coep", "require-corp");7 url2.searchParams.set("coep_report", "coep_report");8 var url3 = new URL(RESOURCES_DIR + "test.html", location.href);9 url3.searchParams.set("coep", "require-corp");10 url3.searchParams.set("coep_report", "coep_report");11 var url4 = new URL(RESOURCES_DIR + "test.html", location.href);12 url4.searchParams.set("coep", "require-corp");13 url4.searchParams.set("coep_report", "coep_report");14 var url5 = new URL(RESOURCES_DIR + "test.html", location.href);15 url5.searchParams.set("coep", "require-corp");16 url5.searchParams.set("coep_report", "coep_report");17 var url6 = new URL(RESOURCES_DIR + "test.html", location.href);18 url6.searchParams.set("coep", "require-corp");19 url6.searchParams.set("coep_report", "coep_report");20 var url7 = new URL(RESOURCES_DIR + "test.html", location.href);21 url7.searchParams.set("coep", "require-corp");22 url7.searchParams.set("coep_report", "coep_report");23 var url8 = new URL(RESOURCES_DIR + "test.html", location.href);24 url8.searchParams.set("coep", "require-corp");25 url8.searchParams.set("coep_report", "coep_report");26 var url9 = new URL(RESOURCES_DIR + "test.html", location.href);27 url9.searchParams.set("coep", "require-corp");28 url9.searchParams.set("coep_report", "coep_report");29 var url10 = new URL(RESOURCES_DIR + "test.html", location.href);30 url10.searchParams.set("coep", "require-corp");31 url10.searchParams.set("coep_report",
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!!