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 handleRequest(request, response) {2 response.setHeader("Cross-Origin-Embedder-Policy", "require-corp");3 response.setHeader("Cross-Origin-Opener-Policy", "same-origin");4 response.setHeader("Access-Control-Allow-Credentials", "true");5 response.setHeader("Access-Control-Allow-Headers", "content-type");6 response.setHeader("Access-Control-Allow-Methods", "POST");7 response.setHeader("Content-Type", "text/html");8 response.setHeader("Vary", "Origin");9 response.setHeader("Access-Control-Max-Age", "86400");10 response.setHeader("X-Content-Type-Options", "nosniff");11 response.setHeader("X-Frame-Options", "DENY");12 response.setHeader("X-XSS-Protection", "1");13 response.setHeader("Content-Security-Policy", "frame-ancestors 'none'");14 response.setHeader("Content-Length", "0");15 response.setHeader("Date", "Tue, 22 Sep 2020 08:08:01 GMT");16 response.setHeader("Server", "wptserve");17 response.setHeader("Cache-Control", "max-age=0, must-revalidate, no-cache, no-store");18 response.setHeader("Expires", "Tue, 22 Sep 2020 08:08:01 GMT");19 response.setHeader("X-Request-URL", request.url);20 response.setHeader("X-Request-Method", request.method);21 response.setHeader("X-Request-Body", request.body);22 response.setHeader("X-Request-Header-Content-Type", request.headers.get("content-type"));23 response.setHeader("X-Request-Header-Accept", request.headers.get("accept"));24 response.setHeader("X-Request-Header-Accept-Encoding", request.headers.get("accept-encoding"));25 response.setHeader("X-Request-Header-Accept-Language", request.headers.get("accept-language"));26 response.setHeader("X-Request-Header-Accept-DPR", request.headers.get("dpr"));27 response.setHeader("X-Request-Header-Accept-Width", request.headers.get("width"));28 response.setHeader("X-Request-Header-Accept-Viewport-Width", request.headers.get("viewport-width"));29 response.setHeader("X-Request-Header-Accept-Downlink", request
Using AI Code Generation
1url.searchParams.set("coep", "credentialless");2url.searchParams.set("allow", "true");3var request = new Request(url, { mode: "no-cors" });4fetch(request).then(function (response) {5 response.text().then(function (text) {6 console.log(text);7 });8});
Using AI Code Generation
1await w_coep_credentialless.runTest();2const results = w_coep_credentialless.getResults();3console.log(results);4console.log(JSON.stringify(results));5console.log(w_coep_credentialless.getResultsAsCSV());6w_coep_credentialless.clearResults();7console.log(w_coep_credentialless.getResults());8console.log(JSON.stringify(w_coep_credentialless.getResults()));9console.log(w_coep_credentialless.getResultsAsCSV());10await w_coep_credentialless.runTest();11const results = w_coep_credentialless.getResults();12console.log(results);13console.log(JSON.stringify(results));14console.log(w_coep_credentialless.getResultsAsCSV());15w_coep_credentialless.clearResults();16console.log(w_coep_credentialless.getResults());17console.log(JSON.stringify(w_coep_credentialless.getResults()));18console.log(w_coep_credentialless.getResultsAsCSV());19await w_coep_credentialless.runTest();20const results = w_coep_credentialless.getResults();21console.log(results);22console.log(JSON.stringify(results));23console.log(w_coep_credentialless.getResultsAsCSV());24w_coep_credentialless.clearResults();25console.log(w_coep_credentialless.getResults());26console.log(JSON.stringify(w_coep_credentialless.getResults()));27console.log(w_coep_credentialless.getResultsAsCSV());
Using AI Code Generation
1import { wptserve } from "wptserve";2import { w_coep_credentialless } from "wptserve";3var server = new wptserve.Server();4server.add_handler("/", function(request, response) {5 response.add_header("Content-Type", "text/html");6 return "Hello World";7});8server.add_handler("/credentialless", w_coep_credentialless(function(request, response) {9 response.add_header("Content-Type", "text/html");10 return "Hello World";11}));12server.start();13server.run();14server.stop();15from wptserve import wptserve, w_coep_credentialless16server = wptserve.Server()17server.add_handler("/", lambda request, response: "Hello World")18server.add_handler("/credentialless", w_coep_credentialless(lambda request, response: "Hello World"))19server.start()20server.run()21server.stop()
Using AI Code Generation
1function test() {2 var xhr = new XMLHttpRequest();3 xhr.open("GET", url, true);4 xhr.send();5 xhr.onload = function() {6 console.log(xhr.responseText);7 }8}9function getWptServerUrl() {10 var xhr = new XMLHttpRequest();11 xhr.open("GET", url, true);12 xhr.send();13 xhr.onload = function() {14 console.log(xhr.responseText);15 }16}17function getWptServerUrl() {18 var xhr = new XMLHttpRequest();19 xhr.open("GET", url, true);20 xhr.send();21 xhr.onload = function() {22 console.log(xhr.responseText);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!!