Best JavaScript code snippet using wpt
cache.window.js
Source: cache.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// With COEP:credentialless, requesting a resource without credentials MUST NOT6// return a response requested with credentials. This would be a security7// issue, since COEP:credentialless can be used to enable crossOriginIsolation.8//9// The test the behavior of the HTTP cache:10// 1. b.com stores cookie.11// 2. a.com(COEP:unsafe-none): request b.com's resource.12// 3. a.com(COEP:credentialless): request b.com's resource.13//14// The first time, the resource is requested with credentials. The response is15// served with Cache-Control: max-age=31536000. It enters the cache.16// The second time, the resource is requested without credentials. The response17// in the cache must not be returned.18const cookie_key = "coep_cache_key";19const cookie_value = "coep_cache_value";20const same_origin = get_host_info().HTTPS_ORIGIN;21const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;22const GetCookie = (response) => {23 return parseCookies(JSON.parse(response))[cookie_key];24}25// "same_origin" document with COEP:unsafe-none.26const w_coep_none_token = token();27const w_coep_none_url = same_origin + executor_path + coep_none +28 `&uuid=${w_coep_none_token}`29const w_coep_none = window.open(w_coep_none_url);30add_completion_callback(() => w_coep_none.close());31// "same_origin" document with COEP:credentialles.32const w_coep_credentialless_token = token();33const w_coep_credentialless_url = same_origin + executor_path +34 coep_credentialless + `&uuid=${w_coep_credentialless_token}`35const w_coep_credentialless = window.open(w_coep_credentialless_url);36add_completion_callback(() => w_coep_credentialless.close());37const this_token = token();38// A request toward a "cross-origin" cacheable response.39const request_token = token();40const request_url = cacheableShowRequestHeaders(cross_origin, request_token);41promise_setup(async test => {42 await setCookie(cross_origin, cookie_key, cookie_value + cookie_same_site_none);43}, "Set cookie");44// The "same-origin" COEP:unsafe-none document fetchs a "cross-origin"45// resource. The request is sent with credentials.46promise_setup(async test => {47 send(w_coep_none_token, `48 await fetch("${request_url}", {49 mode : "no-cors",50 credentials: "include",51 });52 send("${this_token}", "Resource fetched");53 `);54 assert_equals(await receive(this_token), "Resource fetched");55 assert_equals(await receive(request_token).then(GetCookie), cookie_value);56}, "Cache a response requested with credentials");57// The "same-origin" COEP:credentialless document fetches the same resource58// without credentials. The HTTP cache must not be used. Instead a second59// request must be made without credentials.60promise_test(async test => {61 send(w_coep_credentialless_token, `62 await fetch("${request_url}", {63 mode : "no-cors",64 credentials: "include",65 });66 send("${this_token}", "Resource fetched");67 `);68 assert_equals(await receive(this_token), "Resource fetched");69 test.step_timeout(test.unreached_func("The HTTP cache has been used"), 1500);70 assert_equals(await receive(request_token).then(GetCookie), undefined);...
cache.tentative.window.js
Source: cache.tentative.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// With COEP:credentialless, requesting a resource without credentials MUST NOT6// return a response requested with credentials. This would be a security7// issue, since COEP:credentialless can be used to enable crossOriginIsolation.8//9// The test the behavior of the HTTP cache:10// 1. b.com stores cookie.11// 2. a.com(COEP:unsafe-none): request b.com's resource.12// 3. a.com(COEP:credentialless): request b.com's resource.13//14// The first time, the resource is requested with credentials. The response is15// served with Cache-Control: max-age=31536000. It enters the cache.16// The second time, the resource is requested without credentials. The response17// in the cache must not be returned.18const cookie_key = "coep_cache_key";19const cookie_value = "coep_cache_value";20const same_origin = get_host_info().HTTPS_ORIGIN;21const cross_origin = get_host_info().HTTPS_REMOTE_ORIGIN;22const GetCookie = (response) => {23 return parseCookies(JSON.parse(response))[cookie_key];24}25// "same_origin" document with COEP:unsafe-none.26const w_coep_none_token = token();27const w_coep_none_url = same_origin + executor_path + coep_none +28 `&uuid=${w_coep_none_token}`29const w_coep_none = window.open(w_coep_none_url);30add_completion_callback(() => w_coep_none.close());31// "same_origin" document with COEP:credentialles.32const w_coep_credentialless_token = token();33const w_coep_credentialless_url = same_origin + executor_path +34 coep_credentialless + `&uuid=${w_coep_credentialless_token}`35const w_coep_credentialless = window.open(w_coep_credentialless_url);36add_completion_callback(() => w_coep_credentialless.close());37const this_token = token();38// A request toward a "cross-origin" cacheable response.39const request_token = token();40const request_url = cacheableShowRequestHeaders(cross_origin, request_token);41promise_setup(async test => {42 await setCookie(cross_origin, cookie_key, cookie_value + cookie_same_site_none);43}, "Set cookie");44// The "same-origin" COEP:unsafe-none document fetchs a "cross-origin"45// resource. The request is sent with credentials.46promise_setup(async test => {47 send(w_coep_none_token, `48 await fetch("${request_url}", {49 mode : "no-cors",50 credentials: "include",51 });52 send("${this_token}", "Resource fetched");53 `);54 assert_equals(await receive(this_token), "Resource fetched");55 assert_equals(await receive(request_token).then(GetCookie), cookie_value);56}, "Cache a response requested with credentials");57// The "same-origin" COEP:credentialless document fetches the same resource58// without credentials. The HTTP cache must not be used. Instead a second59// request must be made without credentials.60promise_test(async test => {61 send(w_coep_credentialless_token, `62 await fetch("${request_url}", {63 mode : "no-cors",64 credentials: "include",65 });66 send("${this_token}", "Resource fetched");67 `);68 assert_equals(await receive(this_token), "Resource fetched");69 test.step_timeout(test.unreached_func("The HTTP cache has been used"), 1500);70 assert_equals(await receive(request_token).then(GetCookie), undefined);...
Using AI Code Generation
1function handler(metadata, response) {2 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");3 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");4 response.setHeader("Content-Type", "text/html");5 response.setHeader("Access-Control-Allow-Origin", "*");6 response.setHeader("Access-Control-Allow-Credentials", "true");7 response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");8 response.setHeader("Access-Control-Allow-Headers", "Content-Type");9 response.setHeader("Access-Control-Max-Age", "86400");10 response.write("ok");11}12function handler(metadata, response) {13 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");14 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");15 response.setHeader("Content-Type", "text/html");16 response.setHeader("Access-Control-Allow-Origin", "*");17 response.setHeader("Access-Control-Allow-Credentials", "true");18 response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");19 response.setHeader("Access-Control-Allow-Headers", "Content-Type");20 response.setHeader("Access-Control-Max-Age", "86400");21 response.write("ok");22}23function handler(metadata, response) {24 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");25 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");26 response.setHeader("Content-Type", "text/html");27 response.setHeader("Access-Control-Allow-Origin", "*");28 response.setHeader("Access-Control-Allow-Credentials", "true");29 response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");30 response.setHeader("Access-Control-Allow-Headers", "Content-Type");31 response.setHeader("Access-Control-Max-Age", "86400");32 response.write("ok");33}34function handler(metadata, response) {35 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");36 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");37 response.setHeader("Content-Type", "text/html");
Using AI Code Generation
1var wptserve = require('wptserve');2var response = wptserve.response;3var headers = wptserve.headers;4function w_coep_none(request, url) {5 return response(6 'content-security-policy: require-trusted-types-for "script"; trusted-types *',7 'trusted-types default; create-trusted-type-policy my-policy; trusted-type-policy my-policy createHTML "hello"',8 document.write('<script src="/wptserve.py/js-test-resources/echo-headers.py?headers=Content-Security-Policy"></script>');9 </html>`);10}11wptserve.handlers['w_coep_none'] = w_coep_none;12 var xhr = new XMLHttpRequest();13 xhr.open('GET', url);14 xhr.withCredentials = true;15 xhr.send();
Using AI Code Generation
1var wptserve = require('/wptserve.js');2var handler = wptserve.handler;3var headers = wptserve.headers;4var utils = wptserve.utils;5function w_coep_none(request, response) {6 var origin = request.GET.origin;7 var policy = request.GET.policy;8 var headers = {"Cross-Origin-Embedder-Policy": policy,9 "Cross-Origin-Opener-Policy": "same-origin"};10 return handler.return(11 request, 200, headers, origin);12}13wptserve.addHandler("w_coep_none", w_coep_none);14var wptserve = require('/wptserve.js');15var handler = wptserve.handler;16var headers = wptserve.headers;17var utils = wptserve.utils;18function w_coep_credentialless(request, response) {19 var origin = request.GET.origin;20 var policy = request.GET.policy;21 var headers = {"Cross-Origin-Embedder-Policy": policy,22 "Access-Control-Allow-Credentials": "true"};23 return handler.return(24 request, 200, headers, origin);25}26wptserve.addHandler("w_coep_credentialless", w_coep_credentialless);27var wptserve = require('/wptserve.js');28var handler = wptserve.handler;29var headers = wptserve.headers;30var utils = wptserve.utils;31function w_coep_credentialless(request, response) {32 var origin = request.GET.origin;33 var policy = request.GET.policy;34 var headers = {"Cross-Origin-Embedder-Policy": policy,35 "Access-Control-Allow-Credentials": "true"};36 return handler.return(37 request, 200, headers, origin);38}39wptserve.addHandler("w_coep_credentialless", w_coep_credentialless);40var wptserve = require('/wptserve.js');41var handler = wptserve.handler;42var headers = wptserve.headers;
Using AI Code Generation
1var wptserve = require('/tools/wptserve/wptserve.js');2var server = new wptserve.WebTestHttpd();3server.start();4server.register_handler('w_coep_none', wptserve.handler('w_coep_none'));5server.register_handler('w_coep_cors', wptserve.handler('w_coep_cors'));6server.register_handler('w_coep_cors_none', wptserve.handler('w_coep_cors_none'));7server.register_handler('w_coep_cors_cors', wptserve.handler('w_coep_cors_cors'));8server.register_handler('w_coep_cors_cors_none', wptserve.handler('w_coep_cors_cors_none'));9server.register_handler('w_coep_cors_cors_cors', wptserve.handler('w_coep_cors_cors_cors'));10server.register_handler('w_coep_cors_cors_cors_none', wptserve.handler('w_coep_cors_cors_cors_none'));11server.register_handler('w_coep_cors_cors_cors_cors', wptserve.handler('w_coep_cors_cors_cors_cors'));12server.register_handler('w_coep_cors_cors_cors_cors_none', wptserve.handler('w_coep_cors_cors_cors_cors_none'));13server.register_handler('w_coep_cors_cors_cors_cors_cors', wptserve.handler('w_coep_cors_cors_cors_cors_cors'));14server.register_handler('w_coep_cors_cors_cors_cors_cors_none', wptserve.handler('w_coep_cors_cors_cors_cors_cors_none'));15server.register_handler('w_coep_cors_cors_cors_cors_cors_cors', wptserve.handler('w_coep_cors_cors_cors_cors_cors_cors'));16server.register_handler('w_coep_cors_cors_cors_cors_cors_cors_none', wptserve.handler('w_coep_cors_cors_cors_cors_cors_cors_none'));17server.register_handler('w_coep_cors_cors_cors_cors_cors_cors_cors', wptserve.handler('w_coep_cors_cors_cors_cors_cors_cors_cors'));18server.register_handler('w_coep_cors_cors_cors_cors_cors_c
Using AI Code Generation
1promise_test(async t => {2 assert_equals(resp.headers.get("cross-origin-resource-policy"), "cross-origin");3}, "Cross-Origin-Resource-Policy header should be set to cross-origin");4def main(request, response):5 response.headers.set("Cross-Origin-Resource-Policy", request.headers.get("Cross-Origin-Resource-Policy", "none"))6 def set_header(self, name, value):7 if name.lower() == b"set-cookie":8 self.cookies.append(value)9 self.headers.set(name, value)10 def add_header(self, name, value):11 if name.lower() == b"set-cookie":12 self.cookies.append(value)13 self.headers.add(name, value)14 def set_header(self, name, value):15 if name.lower() == b"set-cookie":16 self.cookies.append(value)17 self.headers.set(name, value)18 def add_header(self, name, value):19 if name.lower() == b"set-cookie":20 self.cookies.append(value)21 self.headers.add(name, value)22 def set_header(self, name, value):23 if name.lower() == b"set-cookie":24 self.cookies.append(value)25 self.headers.set(name, value)26 def add_header(self, name, value):
Using AI Code Generation
1function run_test() {2 var server = createServer({w_coep_none: true});3 server.registerPathHandler("/w_coep_none", function(request, response) {4 response.write("Hello, world!");5 });6 server.start(-1);7 var channel = makeChannel(server.baseURI + "/w_coep_none");8 channel.asyncOpen(new ChannelListener(checkRequest, channel, CL_EXPECT_FAILURE));9 do_test_pending();10}11function checkRequest(request, data, ctx) {12 Assert.equal(request.status, Cr.NS_ERROR_CORRUPTED_CONTENT);13 do_test_finished();14}15window.addEventListener('message', e => {16 if (e.data === 'load') {17 var iframe = document.createElement('iframe');18 iframe.src = 'test.js';19 document.body.appendChild(iframe);20 } else if (e.data === 'error') {21 document.body.innerHTML = 'PASS';22 }23});24window.parent.postMessage('load', '*');25window.parent.postMessage('error', '*');26Error: NS_ERROR_CORRUPTED_CONTENT: Component returned failure code: 0x804b000d (NS_ERROR_CORRUPTED_CONTENT) [nsIHttpChannel.asyncOpen]
Using AI Code Generation
1promise_test(async (t) => {2 const response = await fetch(url, { mode: "no-cors" });3 assert_equals(response.status, 200);4 assert_equals(response.type, "opaque");5 assert_equals(response.url, url);6 assert_equals(response.redirected, false);7 assert_equals(response.body, null);8 assert_equals(response.bodyUsed, false);9 assert_equals(response.headers.get("Cross-Origin-Embedder-Policy"), "require-corp");10}, 'Cross-Origin-Embedder-Policy header is set correctly');
Check out the latest blogs from LambdaTest on this topic:
Testing is a critical step in any web application development process. However, it can be an overwhelming task if you don’t have the right tools and expertise. A large percentage of websites still launch with errors that frustrate users and negatively affect the overall success of the site. When a website faces failure after launch, it costs time and money to fix.
We launched LT Browser in 2020, and we were overwhelmed by the response as it was awarded as the #5 product of the day on the ProductHunt platform. Today, after 74,585 downloads and 7,000 total test runs with an average of 100 test runs each day, the LT Browser has continued to help developers build responsive web designs in a jiffy.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
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!!