Best JavaScript code snippet using wpt
sriharness.js
Source:sriharness.js
1var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue, nonce) {2 this.pass = pass;3 this.name = "Script: " + name;4 this.src = src;5 this.integrityValue = integrityValue;6 this.crossoriginValue = crossoriginValue;7 this.nonce = nonce;8}9SRIScriptTest.prototype.execute = function() {10 var test = async_test(this.name);11 var e = document.createElement("script");12 e.src = this.src;13 e.setAttribute("integrity", this.integrityValue);14 if(this.crossoriginValue) {15 e.setAttribute("crossorigin", this.crossoriginValue);16 }17 if(this.nonce) {18 e.setAttribute("nonce", this.nonce);19 }20 if(this.pass) {21 e.addEventListener("load", function() {test.done()});22 e.addEventListener("error", function() {23 test.step(function(){ assert_unreached("Good load fired error handler.") })24 });25 } else {26 e.addEventListener("load", function() {27 test.step(function() { assert_unreached("Bad load succeeded.") })28 });29 e.addEventListener("error", function() {test.done()});30 }31 document.body.appendChild(e);32};33function set_extra_attributes(element, attrs) {34 // Apply the rest of the attributes, if any.35 for (const [attr_name, attr_val] of Object.entries(attrs)) {36 element[attr_name] = attr_val;37 }38}39function buildElementFromDestination(resource_url, destination, attrs) {40 // Assert: |destination| is a valid destination.41 let element;42 // The below switch is responsible for:43 // 1. Creating the correct subresource element44 // 2. Setting said element's href, src, or fetch-instigating property45 // appropriately.46 switch (destination) {47 case "script":48 element = document.createElement(destination);49 set_extra_attributes(element, attrs);50 element.src = resource_url;51 break;52 case "style":53 element = document.createElement('link');54 set_extra_attributes(element, attrs);55 element.rel = 'stylesheet';56 element.href = resource_url;57 break;58 case "image":59 element = document.createElement('img');60 set_extra_attributes(element, attrs);61 element.src = resource_url;62 break;63 default:64 assert_unreached("INVALID DESTINATION");65 }66 return element;67}68// When using SRIPreloadTest, also include /preload/resources/preload_helper.js69// |number_of_requests| is used to ensure that preload requests are actually70// reused as expected.71const SRIPreloadTest = (preload_sri_success, subresource_sri_success, name,72 number_of_requests, destination, resource_url,73 link_attrs, subresource_attrs) => {74 const test = async_test(name);75 const link = document.createElement('link');76 // Early-fail in UAs that do not support `preload` links.77 test.step_func(() => {78 assert_true(link.relList.supports('preload'),79 "This test is automatically failing because the browser does not" +80 "support `preload` links.");81 })();82 // Build up the link.83 link.rel = 'preload';84 link.as = destination;85 link.href = resource_url;86 for (const [attr_name, attr_val] of Object.entries(link_attrs)) {87 link[attr_name] = attr_val; // This may override `rel` to modulepreload.88 }89 // Preload + subresource success and failure loading functions.90 const valid_preload_failed = test.step_func(() =>91 { assert_unreached("Valid preload fired error handler.") });92 const invalid_preload_succeeded = test.step_func(() =>93 { assert_unreached("Invalid preload load succeeded.") });94 const valid_subresource_failed = test.step_func(() =>95 { assert_unreached("Valid subresource fired error handler.") });96 const invalid_subresource_succeeded = test.step_func(() =>97 { assert_unreached("Invalid subresource load succeeded.") });98 const subresource_pass = test.step_func(() => {99 verifyNumberOfResourceTimingEntries(resource_url, number_of_requests);100 test.done();101 });102 const preload_pass = test.step_func(() => {103 const subresource_element = buildElementFromDestination(104 resource_url,105 destination,106 subresource_attrs107 );108 if (subresource_sri_success) {109 subresource_element.onload = subresource_pass;110 subresource_element.onerror = valid_subresource_failed;111 } else {112 subresource_element.onload = invalid_subresource_succeeded;113 subresource_element.onerror = subresource_pass;114 }115 document.body.append(subresource_element);116 });117 if (preload_sri_success) {118 link.onload = preload_pass;119 link.onerror = valid_preload_failed;120 } else {121 link.onload = invalid_preload_succeeded;122 link.onerror = preload_pass;123 }124 document.head.append(link);125}126// <link> tests127// Style tests must be done synchronously because they rely on the presence128// and absence of global style, which can affect later tests. Thus, instead129// of executing them one at a time, the style tests are implemented as a130// queue that builds up a list of tests, and then executes them one at a131// time.132var SRIStyleTest = function(queue, pass, name, attrs, customCallback, altPassValue) {133 this.pass = pass;134 this.name = "Style: " + name;135 this.customCallback = customCallback || function () {};136 this.attrs = attrs || {};137 this.passValue = altPassValue || "rgb(255, 255, 0)";138 this.test = async_test(this.name);139 this.queue = queue;140 this.queue.push(this);141}142SRIStyleTest.prototype.execute = function() {143 var that = this;144 var container = document.getElementById("container");145 while (container.hasChildNodes()) {146 container.removeChild(container.firstChild);147 }148 var test = this.test;149 var div = document.createElement("div");150 div.className = "testdiv";151 var e = document.createElement("link");152 // The link relation is guaranteed to not be "preload" or "modulepreload".153 this.attrs.rel = this.attrs.rel || "stylesheet";154 for (var key in this.attrs) {155 if (this.attrs.hasOwnProperty(key)) {156 e.setAttribute(key, this.attrs[key]);157 }158 }159 if(this.pass) {160 e.addEventListener("load", function() {161 test.step(function() {162 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");163 assert_equals(background, that.passValue);164 test.done();165 });166 });167 e.addEventListener("error", function() {168 test.step(function(){ assert_unreached("Good load fired error handler.") })169 });170 } else {171 e.addEventListener("load", function() {172 test.step(function() { assert_unreached("Bad load succeeded.") })173 });174 e.addEventListener("error", function() {175 test.step(function() {176 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");177 assert_not_equals(background, that.passValue);178 test.done();179 });180 });181 }182 container.appendChild(div);183 container.appendChild(e);184 this.customCallback(e, container);...
aflprep_sriharness.js
Source:aflprep_sriharness.js
1var SRIScriptTest = function(pass, name, src, integrityValue, crossoriginValue, nonce) {2 this.pass = pass;3 this.name = "Script: " + name;4 this.src = src;5 this.integrityValue = integrityValue;6 this.crossoriginValue = crossoriginValue;7 this.nonce = nonce;8}9SRIScriptTest.prototype.execute = function() {10 var test = async_test(this.name);11 var e = document.createElement("script");12 e.src = this.src;13 e.setAttribute("integrity", this.integrityValue);14 if(this.crossoriginValue) {15 e.setAttribute("crossorigin", this.crossoriginValue);16 }17 if(this.nonce) {18 e.setAttribute("nonce", this.nonce);19 }20 if(this.pass) {21 e.addEventListener("load", function() {test.done()});22 e.addEventListener("error", function() {23 test.step(function(){ assert_unreached("Good load fired error handler.") })24 });25 } else {26 e.addEventListener("load", function() {27 test.step(function() { assert_unreached("Bad load succeeded.") })28 });29 e.addEventListener("error", function() {test.done()});30 }31 document.body.appendChild(e);32};33function set_extra_attributes(element, attrs) {34 for (const [attr_name, attr_val] of Object.entries(attrs)) {35 element[attr_name] = attr_val;36 }37}38function buildElementFromDestination(resource_url, destination, attrs) {39 let element;40 switch (destination) {41 case "script":42 element = document.createElement(destination);43 set_extra_attributes(element, attrs);44 element.src = resource_url;45 break;46 case "style":47 element = document.createElement('link');48 set_extra_attributes(element, attrs);49 element.rel = 'stylesheet';50 element.href = resource_url;51 break;52 case "image":53 element = document.createElement('img');54 set_extra_attributes(element, attrs);55 element.src = resource_url;56 break;57 default:58 assert_unreached("INVALID DESTINATION");59 }60 return element;61}62const SRIPreloadTest = (preload_sri_success, subresource_sri_success, name,63 number_of_requests, destination, resource_url,64 link_attrs, subresource_attrs) => {65 const test = async_test(name);66 const link = document.createElement('link');67 test.step_func(() => {68 assert_true(link.relList.supports('preload'),69 "This test is automatically failing because the browser does not" +70 "support `preload` links.");71 })();72 link.rel = 'preload';73 link.as = destination;74 link.href = resource_url;75 for (const [attr_name, attr_val] of Object.entries(link_attrs)) {76 }77 const valid_preload_failed = test.step_func(() =>78 { assert_unreached("Valid preload fired error handler.") });79 const invalid_preload_succeeded = test.step_func(() =>80 { assert_unreached("Invalid preload load succeeded.") });81 const valid_subresource_failed = test.step_func(() =>82 { assert_unreached("Valid subresource fired error handler.") });83 const invalid_subresource_succeeded = test.step_func(() =>84 { assert_unreached("Invalid subresource load succeeded.") });85 const subresource_pass = test.step_func(() => {86 verifyNumberOfResourceTimingEntries(resource_url, number_of_requests);87 test.done();88 });89 const preload_pass = test.step_func(() => {90 const subresource_element = buildElementFromDestination(91 resource_url,92 destination,93 subresource_attrs94 );95 if (subresource_sri_success) {96 subresource_element.onload = subresource_pass;97 subresource_element.onerror = valid_subresource_failed;98 } else {99 subresource_element.onload = invalid_subresource_succeeded;100 subresource_element.onerror = subresource_pass;101 }102 document.body.append(subresource_element);103 });104 if (preload_sri_success) {105 link.onload = preload_pass;106 link.onerror = valid_preload_failed;107 } else {108 link.onload = invalid_preload_succeeded;109 link.onerror = preload_pass;110 }111 document.head.append(link);112}113var SRIStyleTest = function(queue, pass, name, attrs, customCallback, altPassValue) {114 this.pass = pass;115 this.name = "Style: " + name;116 this.customCallback = customCallback || function () {};117 this.attrs = attrs || {};118 this.passValue = altPassValue || "rgb(255, 255, 0)";119 this.test = async_test(this.name);120 this.queue = queue;121 this.queue.push(this);122}123SRIStyleTest.prototype.execute = function() {124 var that = this;125 var container = document.getElementById("container");126 while (container.hasChildNodes()) {127 container.removeChild(container.firstChild);128 }129 var test = this.test;130 var div = document.createElement("div");131 div.className = "testdiv";132 var e = document.createElement("link");133 this.attrs.rel = this.attrs.rel || "stylesheet";134 for (var key in this.attrs) {135 if (this.attrs.hasOwnProperty(key)) {136 e.setAttribute(key, this.attrs[key]);137 }138 }139 if(this.pass) {140 e.addEventListener("load", function() {141 test.step(function() {142 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");143 assert_equals(background, that.passValue);144 test.done();145 });146 });147 e.addEventListener("error", function() {148 test.step(function(){ assert_unreached("Good load fired error handler.") })149 });150 } else {151 e.addEventListener("load", function() {152 test.step(function() { assert_unreached("Bad load succeeded.") })153 });154 e.addEventListener("error", function() {155 test.step(function() {156 var background = window.getComputedStyle(div, null).getPropertyValue("background-color");157 assert_not_equals(background, that.passValue);158 test.done();159 });160 });161 }162 container.appendChild(div);163 container.appendChild(e);164 this.customCallback(e, container);...
Using AI Code Generation
1function subresource_pass(request, response) {2 response.setHeader("Content-Type", "text/plain");3 response.setHeader("Cache-Control", "max-age=0");4 response.setHeader("Access-Control-Allow-Origin", "*");5 response.setHeader("Access-Control-Allow-Methods", "GET, POST");6 response.setHeader("Access-Control-Allow-Headers", "x-test");7 response.setHeader("Access-Control-Expose-Headers", "x-test");8 response.setHeader("Access-Control-Max-Age", "86400");9 response.setHeader("Access-Control-Allow-Credentials", "true");10 response.setHeader("X-Content-Type-Options", "nosniff");11 response.setHeader("X-Frame-Options", "DENY");12 response.setHeader("X-XSS-Protection", "1; mode=block");13 response.setHeader("Content-Security-Policy", "default-src 'none'");14 response.setHeader("X-WebKit-CSP", "default-src 'none'");15 response.setHeader("Referrer-Policy", "no-referrer");16 response.setHeader("Feature-Policy", "vibrate 'none'");17 response.setHeader("Permissions-Policy", "vibrate=(self 'none')");18 response.setHeader("X-Test", "PASS");19 response.write("PASS");20}
Using AI Code Generation
1function subresource_pass(request, response) {2 response.setHeader("Content-Type", "text/javascript");3 response.setHeader("Cache-Control", "max-age=0");4 response.write("postMessage('PASS');\n");5 response.done();6}
Using AI Code Generation
1function run_test() {2 var channel = setupChannel(url);3 channel.asyncOpen(new ChannelListener(checkRequest, channel), null);4 do_test_pending();5}6function checkRequest(request, data, ctx) {7 do_check_eq(request.status, Components.results.NS_OK);8 do_check_eq(data, "PASS");9 do_test_finished();10}
Using AI Code Generation
1var subresource_pass = subresource_pass || function() {};2var subresource_fail = subresource_fail || function() {};3var subresource_test = subresource_test || function() {};4var subresource_test_state = subresource_test_state || function() {};5var subresource_test_run = subresource_test_run || function() {};6var subresource_test_report = subresource_test_report || function() {};7var subresource_test_expectations = subresource_test_expectations || function() {};8var subresource_test_expectation = subresource_test_expectation || function() {};9var subresource_test_expectation_set = subresource_test_expectation_set || function() {};10var subresource_test_expectation_set_state = subresource_test_expectation_set_state || function() {};11var subresource_test_expectation_set_run = subresource_test_expectation_set_run || function() {};12var subresource_test_expectation_set_report = subresource_test_expectation_set_report || function() {};13var subresource_test_expectation_set_report_summary = subresource_test_expectation_set_report_summary || function() {};14var subresource_test_expectation_set_report_expectation = subresource_test_expectation_set_report_expectation || function() {};15var subresource_test_expectation_set_report_expectation_set = subresource_test_expectation_set_report_expectation_set || function() {};16var subresource_test_expectation_set_report_test = subresource_test_expectation_set_report_test || function() {};17var subresource_test_expectation_set_report_test_state = subresource_test_expectation_set_report_test_state || function() {};18var subresource_test_expectation_set_report_test_run = subresource_test_expectation_set_report_test_run || function() {};19var subresource_test_expectation_set_report_test_report = subresource_test_expectation_set_report_test_report || function() {};20var subresource_test_expectation_set_report_test_expectations = subresource_test_expectation_set_report_test_expectations || function() {};21var subresource_test_expectation_set_report_test_expectation = subresource_test_expectation_set_report_test_expectation || function() {};22var subresource_test_expectation_set_report_test_expectation_set = subresource_test_expectation_set_report_test_expectation_set || function() {};23var subresource_test_expectation_set_report_test_expectation_set_state = subresource_test_expectation_set_report_test_expectation_set_state || function() {};24var subresource_test_expectation_set_report_test_expectation_set_run = subresource_test_expectation_set_report_test_expectation_set_run || function() {};
Using AI Code Generation
1function handle_request(request, response)2{3 var headers = {"Content-Type": "text/html"};4 var body = "<script src='test2.js'></script>";5 return [200, headers, body];6}7function handle_request(request, response)8{9 var headers = {"Content-Type": "text/javascript"};10 var body = "document.write('PASS');";11 return [200, headers, body];12}
Using AI Code Generation
1function run_test() {2 add_tls_server_setup("BadCertServer");3 add_tls_server_setup("GoodCertServer");4 add_tls_server_setup("OCSPStaplingServer");5 add_tls_server_setup("OCSPStaplingServerWithGoodCert");6 add_tls_server_setup("OCSPStaplingServerWithExpiredCert");7 add_tls_server_setup("OCSPStaplingServerWithRevokedCert");8 add_tls_server_setup("OCSPStaplingServerWithUnknownCert");9 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCert");10 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndExpiredCert");11 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndRevokedCert");12 add_tls_server_setup("OCSPStaplingServerWithUnknownCertAndExpiredCert");13 add_tls_server_setup("OCSPStaplingServerWithUnknownCertAndRevokedCert");14 add_tls_server_setup("OCSPStaplingServerWithExpiredCertAndRevokedCert");15 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCert");16 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndRevokedCert");17 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndExpiredCertAndRevokedCert");18 add_tls_server_setup("OCSPStaplingServerWithUnknownCertAndExpiredCertAndRevokedCert");19 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCert");20 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCertWithUnknownCA");21 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCertWithExpiredCA");22 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCertWithRevokedCA");23 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCertWithUnknownCAAndExpiredCA");24 add_tls_server_setup("OCSPStaplingServerWithGoodCertAndUnknownCertAndExpiredCertAndRevokedCertWithUnknownCAAndRev
Using AI Code Generation
1importScripts("/resources/testharness.js");2async_test(function(t) {3 var url = "resources/subresource_pass.py";4 var request = new XMLHttpRequest();5 request.open("GET", url, true);6 request.setRequestHeader("Accept", "text/html");7 request.onload = t.step_func(function() {8 assert_equals(request.status, 200);9 t.done();10 });11 request.send();12}, "Test if the subresource can be loaded successfully when the request header is set to Accept: text/html");
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!!