Best JavaScript code snippet using wpt
csp-tests.js
Source:csp-tests.js
...20// Usage:21// runContentSecurityPolicyTests("paint");22function runContentSecurityPolicyTests(workletType) {23 runSrcTests(workletType);24 runMixedContentTests(workletType);25}26// script-src and worker-src tests.27function runSrcTests(workletType) {28 const kWindowConfigs = [29 {30 'windowURL':31 'resources/addmodule-window.html?pipe=header(' +32 'Content-Security-Policy, script-src \'self\' \'unsafe-inline\')',33 'crossOriginExpectation': 'REJECTED',34 'message': 'should be blocked by the script-src \'self\' directive.'35 },36 {37 'windowURL':38 'resources/addmodule-window.html?pipe=header(' +39 'Content-Security-Policy, script-src ' + location.origin + ' ' +40 get_host_info().HTTPS_REMOTE_ORIGIN + ' \'unsafe-inline\')',41 'crossOriginExpectation': 'RESOLVED',42 'message':43 'should not be blocked because the script-src directive ' +44 'specifying the origin allows it.'45 },46 {47 'windowURL':48 'resources/addmodule-window.html?pipe=header(' +49 'Content-Security-Policy, script-src * \'unsafe-inline\')',50 'crossOriginExpectation': 'RESOLVED',51 'message':52 'should not be blocked because the script-src * directive allows it.'53 },54 {55 'windowURL':56 'resources/addmodule-window.html?pipe=header(' +57 'Content-Security-Policy, worker-src \'self\' \'unsafe-inline\')',58 'crossOriginExpectation': 'RESOLVED',59 'message':60 'should not be blocked by the worker-src directive ' +61 'because worklets obey the script-src directive.'62 }63 ];64 for (const windowConfig of kWindowConfigs) {65 promise_test(t => {66 const kScriptURL =67 get_host_info().HTTPS_REMOTE_ORIGIN +68 '/worklets/resources/empty-worklet-script-with-cors-header.js';69 return openWindowAndExpectResult(70 windowConfig.windowURL, kScriptURL, workletType,71 windowConfig.crossOriginExpectation);72 },73 'A remote-origin worklet ' + windowConfig.message);74 promise_test(t => {75 const kScriptURL = 'import-remote-origin-empty-worklet-script.sub.js';76 return openWindowAndExpectResult(77 windowConfig.windowURL, kScriptURL, workletType,78 windowConfig.crossOriginExpectation);79 },80 'A same-origin worklet importing a remote-origin script ' +81 windowConfig.message);82 promise_test(t => {83 // A worklet on HTTPS_REMOTE_ORIGIN will import a child script on84 // HTTPS_REMOTE_ORIGIN.85 const kScriptURL =86 get_host_info().HTTPS_REMOTE_ORIGIN +87 '/worklets/resources/import-empty-worklet-script-with-cors-header.js';88 return openWindowAndExpectResult(89 windowConfig.windowURL, kScriptURL, workletType,90 windowConfig.crossOriginExpectation);91 },92 'A remote-origin worklet importing a remote-origin script ' +93 windowConfig.message);94 promise_test(t => {95 const kScriptURL =96 '/common/redirect.py?location=' + encodeURIComponent(97 get_host_info().HTTPS_REMOTE_ORIGIN +98 '/worklets/resources/empty-worklet-script-with-cors-header.js');99 return openWindowAndExpectResult(100 windowConfig.windowURL, kScriptURL, workletType,101 windowConfig.crossOriginExpectation);102 },103 'A remote-origin-redirected worklet ' + windowConfig.message);104 promise_test(t => {105 const kScriptURL =106 'import-remote-origin-redirected-empty-worklet-script.sub.js';107 return openWindowAndExpectResult(108 windowConfig.windowURL, kScriptURL, workletType,109 windowConfig.crossOriginExpectation);110 },111 'A same-origin worklet importing a remote-origin-redirected script ' +112 windowConfig.message);113 }114}115// Mixed content tests.116function runMixedContentTests(workletType) {117 const kInsecureURL =118 get_host_info().HTTP_ORIGIN +119 '/worklets/resources/empty-worklet-script-with-cors-header.js';120 const kScriptConfigs = [121 {URL: kInsecureURL,122 message: 'An insecure-origin worklet'},123 {URL: '/common/redirect.py?location=' + encodeURIComponent(kInsecureURL),124 message: 'An insecure-origin-redirected worklet'},125 {URL: 'import-insecure-origin-empty-worklet-script.sub.js',126 message: 'A same-origin worklet importing an insecure-origin script'},127 {URL: 'import-insecure-origin-redirected-empty-worklet-script.sub.js',128 message: 'A same-origin worklet ' +129 'importing an insecure-origin-redirected script'}130 ];...
Using AI Code Generation
1function runMixedContentTests() {2 {3 },4 ];5varar testGener tou = async_test("Mixedrconl nt te= ");6 var reque"thttnew XMLHttpRequest();7 request.onload =vtestGenerator.step_func(function()a{8 assert_equals(request.status, 200,v"requestastatusris 200");9url3 =assert_equals(request.respo se, tests[0].expect, "request response is FAIL");10 });11 testGenerator.done();12 });13 request.onerror = testGenerator.step_func(function() {14 testGener"tor.step(function() {15 assert_unreached("request failed");16 });17 testGenerator.done();18 });19 request.send();20}
Using AI Code Generation
1function runMixedContentTests() {2 {3 },4 ];5 var testGenerator = async_test("Mixed content test");6 var request = new XMLHttpRequest();7 request.open("GET", tests[0].url, true);8 request.onload = testGenerator.step_func(function() {9 testGenerator.step(function() {10 assert_equals(request.status, 200, "request status is 200");11 assert_equals(request.response, tests[0].expect, "request response is FAIL");12 });13 testGenerator.done();14 });15 request.onerror = testGenerator.step_func(function() {16 testGenerator.step(function() {17 assert_unreached("request failed");18 });19 testGenerator.done();20 });21 request.send();22}
Using AI Code Generation
1var wptserve = require('../resources/testharness.js');2var mixed_content_utils = require('../resources/mixed-content-testharness.js');3wptserve.runMixedContentTests([4 {5 },6 {7 },8 {9 },10 {11 },12 {13 },14 {15 },16 {17 },18 {
Using AI Code Generation
1var wptserve = require('../resources/testharness.js');2var mixed_content_utils = require('../resources/mixed-content-testharness.js');3wptserve.runMixedContentTests([4 {5 },6 {7}
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!!