Best JavaScript code snippet using wpt
testharness-helper.js
Source:testharness-helper.js
...7}8function assert_no_event(test, obj, name) {9 obj.addEventListener(name, test.unreached_func("The '" + name + "' event should not have fired."));10}11function waitUntilCSPEventForURL(test, url) {12 return new Promise((resolve, reject) => {13 document.addEventListener("securitypolicyviolation", test.step_func(e => {14 if (e.blockedURI == url)15 resolve(e);16 }));17 });18}19function waitUntilEvent(obj, name) {20 return new Promise((resolve, reject) => {21 obj.addEventListener(name, resolve);22 });23}24// Given the URL of a worker that pings its opener upon load, this25// function builds a test that asserts that the ping is received,26// and that no CSP event fires.27function assert_worker_is_loaded(url, description) {28 async_test(t => {29 assert_no_csp_event_for_url(t, url);30 var w = new Worker(url);31 assert_no_event(t, w, "error");32 waitUntilEvent(w, "message")33 .then(t.step_func_done(e => {34 assert_equals(e.data, "ping");35 }));36 }, description);37}38function assert_shared_worker_is_loaded(url, description) {39 async_test(t => {40 assert_no_csp_event_for_url(t, url);41 var w = new SharedWorker(url);42 assert_no_event(t, w, "error");43 waitUntilEvent(w.port, "message")44 .then(t.step_func_done(e => {45 assert_equals(e.data, "ping");46 }));47 w.port.start();48 }, description);49}50function assert_service_worker_is_loaded(url, description) {51 promise_test(t => {52 assert_no_csp_event_for_url(t, url);53 return Promise.all([54 waitUntilEvent(navigator.serviceWorker, "message")55 .then(e => {56 assert_equals(e.data, "ping");57 }),58 navigator.serviceWorker.register(url, { scope: url })59 .then(r => {60 var sw = r.active || r.installing || r.waiting;61 t.add_cleanup(_ => r.unregister());62 sw.postMessage("pong?");63 })64 ]);65 }, description);66}67// Given the URL of a worker that pings its opener upon load, this68// function builds a test that asserts that the constructor throws69// a SecurityError, and that a CSP event fires.70function assert_worker_is_blocked(url, description) {71 async_test(t => {72 // If |url| is a blob, it will be stripped down to "blob" for reporting.73 var reportedURL = new URL(url).protocol == "blob:" ? "blob" : url;74 waitUntilCSPEventForURL(t, reportedURL)75 .then(t.step_func_done(e => {76 assert_equals(e.blockedURI, reportedURL);77 assert_equals(e.violatedDirective, "worker-src");78 assert_equals(e.effectiveDirective, "worker-src");79 }));80 // TODO(mkwst): We shouldn't be throwing here. We should be firing an81 // `error` event on the Worker. https://crbug.com/66329882 assert_throws("SecurityError", function () {83 var w = new Worker(url);84 });85 }, description);86}87function assert_shared_worker_is_blocked(url, description) {88 async_test(t => {89 // If |url| is a blob, it will be stripped down to "blob" for reporting.90 var reportedURL = new URL(url).protocol == "blob:" ? "blob" : url;91 waitUntilCSPEventForURL(t, reportedURL)92 .then(t.step_func_done(e => {93 assert_equals(e.blockedURI, reportedURL);94 assert_equals(e.violatedDirective, "worker-src");95 assert_equals(e.effectiveDirective, "worker-src");96 }));97 // TODO(mkwst): We shouldn't be throwing here. We should be firing an98 // `error` event on the SharedWorker. https://crbug.com/66329899 assert_throws("SecurityError", function () {100 var w = new SharedWorker(url);101 });102 }, description);103}104function assert_service_worker_is_blocked(url, description) {105 promise_test(t => {106 assert_no_event(t, navigator.serviceWorker, "message");107 // If |url| is a blob, it will be stripped down to "blob" for reporting.108 var reportedURL = new URL(url).protocol == "blob:" ? "blob" : url;109 return Promise.all([110 waitUntilCSPEventForURL(t, reportedURL)111 .then(t.step_func_done(e => {112 assert_equals(e.blockedURI, reportedURL);113 assert_equals(e.violatedDirective, "worker-src");114 assert_equals(e.effectiveDirective, "worker-src");115 })),116 promise_rejects(t, "SecurityError", navigator.serviceWorker.register(url, { scope: url }))117 ]);118 }, description);...
Using AI Code Generation
1function waitUntilCSPEventForURL(url) {2 var xhr = new XMLHttpRequest();3 xhr.open("GET", url, false);4 xhr.send();5}6 function waitUntilCSPEventForURL(url) {7 var xhr = new XMLHttpRequest();8 xhr.open("GET", url, false);9 xhr.send();10 }11This is a test to verify that the waitUntilCSPEventForURL() method of wptserve
Using AI Code Generation
1promise_test(async t => {2 const url = 'resources/csp-report.py';3 await waitUntilCSPEventForURL(url);4}, 'Test something');5import cgi6import sys7import json8def main(request, response):9 data = json.loads(request.body)10 return ('', [])11 return ('', [('Content-type', 'text/plain')], 'FAIL')12 return ('', [('Content-type', 'text/plain')], 'FAIL')13> + if (m_reportOnly) 14> + return; 15> + if (!m_reportURIs.size()) 16> + if (!m_reportURIs.size()) 17> + if (!m_reportURIs.size())
Using AI Code Generation
1function runTest() {2 var report = waitUntilCSPEventForURL(url);3 assert_equals(report["csp-report"]["referrer"], "");4 assert_equals(report["csp-report"]["violated-directive"], "script-src 'none'");5 assert_equals(report["csp-report"]["original-policy"], "script-src 'none'");6 done();7}
Using AI Code Generation
1var cspReportReceived = waitUntilCSPEventForURL(url);2var cspReportReceived = waitForCSPEvent();3var directive = 'script-src';4var cspReportReceived = waitForCSPEvent(directive, source);5var directive = 'script-src';6var columnNumber = 0;7var cspReportReceived = waitForCSPEvent(directive, source, columnNumber);8var directive = 'script-src';9var columnNumber = 0;10var lineNumber = 0;11var cspReportReceived = waitForCSPEvent(directive, source, columnNumber, lineNumber);12var directive = 'script-src';13var columnNumber = 0;14var lineNumber = 0;15var statusCode = 200;16var cspReportReceived = waitForCSPEvent(directive, source, columnNumber, lineNumber, statusCode);
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!!