Best JavaScript code snippet using wpt
fetch.https.window.js
Source:fetch.https.window.js
...21});22// Source: secure local context.23//24// All fetches unaffected by Private Network Access.25subsetTestByKey("from-local", promise_test, t => fetchTest(t, {26 source: { server: Server.HTTPS_LOCAL },27 target: { server: Server.HTTPS_LOCAL },28 expected: FetchTestResult.SUCCESS,29}), "local to local: no preflight required.");30subsetTestByKey("from-local", promise_test, t => fetchTest(t, {31 source: { server: Server.HTTPS_LOCAL },32 target: {33 server: Server.HTTPS_PRIVATE,34 behavior: { response: ResponseBehavior.allowCrossOrigin() },35 },36 expected: FetchTestResult.SUCCESS,37}), "local to private: no preflight required.");38subsetTestByKey("from-local", promise_test, t => fetchTest(t, {39 source: { server: Server.HTTPS_LOCAL },40 target: {41 server: Server.HTTPS_PUBLIC,42 behavior: { response: ResponseBehavior.allowCrossOrigin() },43 },44 expected: FetchTestResult.SUCCESS,45}), "local to public: no preflight required.");46// Strictly speaking, the following two tests do not exercise PNA-specific47// logic, but they serve as a baseline for comparison, ensuring that non-PNA48// preflight requests are sent and handled as expected.49subsetTestByKey("baseline", promise_test, t => fetchTest(t, {50 source: { server: Server.HTTPS_LOCAL },51 target: {52 server: Server.HTTPS_PUBLIC,53 behavior: {54 preflight: PreflightBehavior.failure(),55 response: ResponseBehavior.allowCrossOrigin(),56 },57 },58 fetchOptions: { method: "PUT" },59 expected: FetchTestResult.FAILURE,60}), "local to public: PUT preflight failure.");61subsetTestByKey("baseline", promise_test, t => fetchTest(t, {62 source: { server: Server.HTTPS_LOCAL },63 target: {64 server: Server.HTTPS_PUBLIC,65 behavior: {66 preflight: PreflightBehavior.success(token()),67 response: ResponseBehavior.allowCrossOrigin(),68 }69 },70 fetchOptions: { method: "PUT" },71 expected: FetchTestResult.SUCCESS,72}), "local to public: PUT preflight success,");73// Generates tests of preflight behavior for a single (source, target) pair.74//75// Scenarios:76//77// - cors mode:78// - preflight response has non-2xx HTTP code79// - preflight response is missing CORS headers80// - preflight response is missing the PNA-specific `Access-Control` header81// - final response is missing CORS headers82// - success83// - success with PUT method (non-"simple" request)84// - no-cors mode:85// - preflight response has non-2xx HTTP code86// - preflight response is missing CORS headers87// - preflight response is missing the PNA-specific `Access-Control` header88// - success89//90function makePreflightTests({91 subsetKey,92 source,93 sourceDescription,94 targetServer,95 targetDescription,96}) {97 const prefix =98 `${sourceDescription} to ${targetDescription}: `;99 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {100 source,101 target: {102 server: targetServer,103 behavior: {104 preflight: PreflightBehavior.failure(),105 response: ResponseBehavior.allowCrossOrigin(),106 },107 },108 expected: FetchTestResult.FAILURE,109 }), prefix + "failed preflight.");110 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {111 source,112 target: {113 server: targetServer,114 behavior: {115 preflight: PreflightBehavior.noCorsHeader(token()),116 response: ResponseBehavior.allowCrossOrigin(),117 },118 },119 expected: FetchTestResult.FAILURE,120 }), prefix + "missing CORS headers on preflight response.");121 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {122 source,123 target: {124 server: targetServer,125 behavior: {126 preflight: PreflightBehavior.noPnaHeader(token()),127 response: ResponseBehavior.allowCrossOrigin(),128 },129 },130 expected: FetchTestResult.FAILURE,131 }), prefix + "missing PNA header on preflight response.");132 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {133 source,134 target: {135 server: targetServer,136 behavior: { preflight: PreflightBehavior.success(token()) },137 },138 expected: FetchTestResult.FAILURE,139 }), prefix + "missing CORS headers on final response.");140 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {141 source,142 target: {143 server: targetServer,144 behavior: {145 preflight: PreflightBehavior.success(token()),146 response: ResponseBehavior.allowCrossOrigin(),147 },148 },149 expected: FetchTestResult.SUCCESS,150 }), prefix + "success.");151 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {152 source,153 target: {154 server: targetServer,155 behavior: {156 preflight: PreflightBehavior.success(token()),157 response: ResponseBehavior.allowCrossOrigin(),158 },159 },160 fetchOptions: { method: "PUT" },161 expected: FetchTestResult.SUCCESS,162 }), prefix + "PUT success.");163 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {164 source,165 target: { server: targetServer },166 fetchOptions: { mode: "no-cors" },167 expected: FetchTestResult.FAILURE,168 }), prefix + "no-CORS mode failed preflight.");169 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {170 source,171 target: {172 server: targetServer,173 behavior: { preflight: PreflightBehavior.noCorsHeader(token()) },174 },175 fetchOptions: { mode: "no-cors" },176 expected: FetchTestResult.FAILURE,177 }), prefix + "no-CORS mode missing CORS headers on preflight response.");178 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {179 source,180 target: {181 server: targetServer,182 behavior: { preflight: PreflightBehavior.noPnaHeader(token()) },183 },184 fetchOptions: { mode: "no-cors" },185 expected: FetchTestResult.FAILURE,186 }), prefix + "no-CORS mode missing PNA header on preflight response.");187 subsetTestByKey(subsetKey, promise_test, t => fetchTest(t, {188 source,189 target: {190 server: targetServer,191 behavior: { preflight: PreflightBehavior.success(token()) },192 },193 fetchOptions: { mode: "no-cors" },194 expected: FetchTestResult.OPAQUE,195 }), prefix + "no-CORS mode success.");196}197// Source: private secure context.198//199// Fetches to the local address space require a successful preflight response200// carrying a PNA-specific header.201makePreflightTests({202 subsetKey: "from-private",203 source: { server: Server.HTTPS_PRIVATE },204 sourceDescription: "private",205 targetServer: Server.HTTPS_LOCAL,206 targetDescription: "local",207});208subsetTestByKey("from-private", promise_test, t => fetchTest(t, {209 source: { server: Server.HTTPS_PRIVATE },210 target: { server: Server.HTTPS_PRIVATE },211 expected: FetchTestResult.SUCCESS,212}), "private to private: no preflight required.");213subsetTestByKey("from-private", promise_test, t => fetchTest(t, {214 source: { server: Server.HTTPS_PRIVATE },215 target: {216 server: Server.HTTPS_PRIVATE,217 behavior: { response: ResponseBehavior.allowCrossOrigin() },218 },219 expected: FetchTestResult.SUCCESS,220}), "private to public: no preflight required.");221// Source: public secure context.222//223// Fetches to the local and private address spaces require a successful224// preflight response carrying a PNA-specific header.225makePreflightTests({226 subsetKey: "from-public",227 source: { server: Server.HTTPS_PUBLIC },228 sourceDescription: "public",229 targetServer: Server.HTTPS_LOCAL,230 targetDescription: "local",231});232makePreflightTests({233 subsetKey: "from-public",234 source: { server: Server.HTTPS_PUBLIC },235 sourceDescription: "public",236 targetServer: Server.HTTPS_PRIVATE,237 targetDescription: "private",238});239subsetTestByKey("from-public", promise_test, t => fetchTest(t, {240 source: { server: Server.HTTPS_PUBLIC },241 target: { server: Server.HTTPS_PUBLIC },242 expected: FetchTestResult.SUCCESS,243}), "public to public: no preflight required.");244// These tests verify that documents fetched from the `local` address space yet245// carrying the `treat-as-public-address` CSP directive are treated as if they246// had been fetched from the `public` address space.247subsetTestByKey("from-treat-as-public", promise_test, t => fetchTest(t, {248 source: {249 server: Server.HTTPS_LOCAL,250 treatAsPublic: true,251 },252 target: { server: Server.HTTPS_LOCAL },253 expected: FetchTestResult.FAILURE,254}), "treat-as-public-address to local: failed preflight.");255subsetTestByKey("from-treat-as-public", promise_test, t => fetchTest(t, {256 source: {257 server: Server.HTTPS_LOCAL,258 treatAsPublic: true,259 },260 target: {261 server: Server.HTTPS_LOCAL,262 behavior: {263 preflight: PreflightBehavior.success(token()),264 // Interesting: no need for CORS headers on same-origin final response.265 },266 },267 expected: FetchTestResult.SUCCESS,268}), "treat-as-public-address to local: success.");269subsetTestByKey("from-treat-as-public", promise_test, t => fetchTest(t, {270 source: {271 server: Server.HTTPS_LOCAL,272 treatAsPublic: true,273 },274 target: { server: Server.HTTPS_PRIVATE },275 expected: FetchTestResult.FAILURE,276}), "treat-as-public-address to private: failed preflight.");277subsetTestByKey("from-treat-as-public", promise_test, t => fetchTest(t, {278 source: {279 server: Server.HTTPS_LOCAL,280 treatAsPublic: true,281 },282 target: {283 server: Server.HTTPS_PRIVATE,284 behavior: {285 preflight: PreflightBehavior.success(token()),286 response: ResponseBehavior.allowCrossOrigin(),287 },288 },289 expected: FetchTestResult.SUCCESS,290}), "treat-as-public-address to private: success.");291subsetTestByKey("from-treat-as-public", promise_test, t => fetchTest(t, {292 source: {293 server: Server.HTTPS_LOCAL,294 treatAsPublic: true,295 },296 target: {297 server: Server.HTTPS_PUBLIC,298 behavior: { response: ResponseBehavior.allowCrossOrigin() },299 },300 expected: FetchTestResult.SUCCESS,...
fetch.window.js
Source:fetch.window.js
...11setup(() => {12 // Making sure we are in a non secure context, as expected.13 assert_false(window.isSecureContext);14});15promise_test(t => fetchTest(t, {16 source: { server: Server.HTTP_LOCAL },17 target: { server: Server.HTTP_LOCAL },18 expected: FetchTestResult.SUCCESS,19}), "local to local: no preflight required.");20promise_test(t => fetchTest(t, {21 source: { server: Server.HTTP_LOCAL },22 target: {23 server: Server.HTTP_PRIVATE,24 behavior: { response: ResponseBehavior.allowCrossOrigin() },25 },26 expected: FetchTestResult.SUCCESS,27}), "local to private: no preflight required.");28promise_test(t => fetchTest(t, {29 source: { server: Server.HTTP_LOCAL },30 target: {31 server: Server.HTTP_PUBLIC,32 behavior: { response: ResponseBehavior.allowCrossOrigin() },33 },34 expected: FetchTestResult.SUCCESS,35}), "local to public: no preflight required.");36promise_test(t => fetchTest(t, {37 source: { server: Server.HTTP_PRIVATE },38 target: {39 server: Server.HTTP_LOCAL,40 behavior: {41 preflight: PreflightBehavior.success(token()),42 response: ResponseBehavior.allowCrossOrigin(),43 },44 },45 expected: FetchTestResult.FAILURE,46}), "private to local: failure.");47promise_test(t => fetchTest(t, {48 source: { server: Server.HTTP_PRIVATE },49 target: { server: Server.HTTP_PRIVATE },50 expected: FetchTestResult.SUCCESS,51}), "private to private: no preflight required.");52promise_test(t => fetchTest(t, {53 source: { server: Server.HTTP_PRIVATE },54 target: {55 server: Server.HTTP_PUBLIC,56 behavior: { response: ResponseBehavior.allowCrossOrigin() },57 },58 expected: FetchTestResult.SUCCESS,59}), "private to public: no preflight required.");60promise_test(t => fetchTest(t, {61 source: { server: Server.HTTP_PUBLIC },62 target: {63 server: Server.HTTP_LOCAL,64 behavior: {65 preflight: PreflightBehavior.success(token()),66 response: ResponseBehavior.allowCrossOrigin(),67 },68 },69 expected: FetchTestResult.FAILURE,70}), "public to local: failure.");71promise_test(t => fetchTest(t, {72 source: { server: Server.HTTP_PUBLIC },73 target: {74 server: Server.HTTP_PRIVATE,75 behavior: {76 preflight: PreflightBehavior.success(token()),77 response: ResponseBehavior.allowCrossOrigin(),78 },79 },80 expected: FetchTestResult.FAILURE,81}), "public to private: failure.");82promise_test(t => fetchTest(t, {83 source: { server: Server.HTTP_PUBLIC },84 target: { server: Server.HTTP_PUBLIC },85 expected: FetchTestResult.SUCCESS,86}), "public to public: no preflight required.");87// These tests verify that documents fetched from the `local` address space yet88// carrying the `treat-as-public-address` CSP directive are treated as if they89// had been fetched from the `public` address space.90promise_test(t => fetchTest(t, {91 source: {92 server: Server.HTTP_LOCAL,93 treatAsPublic: true,94 },95 target: {96 server: Server.HTTP_LOCAL,97 behavior: {98 preflight: PreflightBehavior.success(token()),99 response: ResponseBehavior.allowCrossOrigin(),100 },101 },102 expected: FetchTestResult.FAILURE,103}), "treat-as-public-address to local: failure.");104promise_test(t => fetchTest(t, {105 source: {106 server: Server.HTTP_LOCAL,107 treatAsPublic: true,108 },109 target: {110 server: Server.HTTP_PRIVATE,111 behavior: {112 preflight: PreflightBehavior.success(token()),113 response: ResponseBehavior.allowCrossOrigin(),114 },115 },116 expected: FetchTestResult.FAILURE,117}), "treat-as-public-address to private: failure.");118promise_test(t => fetchTest(t, {119 source: {120 server: Server.HTTP_LOCAL,121 treatAsPublic: true,122 },123 target: {124 server: Server.HTTP_PUBLIC,125 behavior: { response: ResponseBehavior.allowCrossOrigin() },126 },127 expected: FetchTestResult.SUCCESS,128}), "treat-as-public-address to public: no preflight required.");129// These tests verify that HTTPS iframes embedded in an HTTP top-level document130// cannot fetch subresources from less-public address spaces. Indeed, even131// though the iframes have HTTPS origins, they are non-secure contexts because132// their parent is a non-secure context.133promise_test(t => fetchTest(t, {134 source: { server: Server.HTTPS_PRIVATE },135 target: {136 server: Server.HTTPS_LOCAL,137 behavior: {138 preflight: PreflightBehavior.success(token()),139 response: ResponseBehavior.allowCrossOrigin(),140 },141 },142 expected: FetchTestResult.FAILURE,143}), "private https to local: failure.");144promise_test(t => fetchTest(t, {145 source: { server: Server.HTTPS_PUBLIC },146 target: {147 server: Server.HTTPS_LOCAL,148 behavior: {149 preflight: PreflightBehavior.success(token()),150 response: ResponseBehavior.allowCrossOrigin(),151 },152 },153 expected: FetchTestResult.FAILURE,154}), "public https to local: failure.");155promise_test(t => fetchTest(t, {156 source: { server: Server.HTTPS_PUBLIC },157 target: {158 server: Server.HTTPS_PRIVATE,159 behavior: {160 preflight: PreflightBehavior.success(token()),161 response: ResponseBehavior.allowCrossOrigin(),162 },163 },164 expected: FetchTestResult.FAILURE,...
fetch.tentative.https.window.js
Source:fetch.tentative.https.window.js
...56 "coep:credentialless => ");57 }, `fetch ${description}`)58 };59 // Cookies are never sent with credentials='omit'60 fetchTest("same-origin + no-cors + credentials:omit",61 same_origin, 'no-cors', 'omit',62 undefined,63 undefined);64 fetchTest("same-origin + cors + credentials:omit",65 same_origin, 'cors', 'omit',66 undefined,67 undefined);68 fetchTest("cross-origin + no-cors + credentials:omit",69 cross_origin, 'no-cors', 'omit',70 undefined,71 undefined);72 fetchTest("cross-origin + cors + credentials:omit",73 cross_origin, 'cors', 'omit',74 undefined,75 undefined);76 // Same-origin request contains Cookies.77 fetchTest("same-origin + no-cors + credentials:include",78 same_origin, 'no-cors', 'include',79 cookie_same_origin,80 cookie_same_origin);81 fetchTest("same-origin + cors + credentials:include",82 same_origin, 'cors', 'include',83 cookie_same_origin,84 cookie_same_origin);85 fetchTest("same-origin + no-cors + credentials:same-origin",86 same_origin, 'no-cors', 'same-origin',87 cookie_same_origin,88 cookie_same_origin);89 fetchTest("same-origin + cors + credentials:same-origin",90 same_origin, 'cors', 'same-origin',91 cookie_same_origin,92 cookie_same_origin);93 // Cross-origin CORS requests contains Cookies, if credentials mode is set to94 // 'include'. This does not depends on COEP.95 fetchTest("cross-origin + cors + credentials:include",96 cross_origin, 'cors', 'include',97 cookie_cross_origin,98 cookie_cross_origin);99 fetchTest("cross-origin + cors + same-origin-credentials",100 cross_origin, 'cors', 'same-origin',101 undefined,102 undefined);103 // Cross-origin no-CORS requests includes Cookies when:104 // 1. credentials mode is 'include'105 // 2. COEP: is not credentialless.106 fetchTest("cross-origin + no-cors + credentials:include",107 cross_origin, 'no-cors', 'include',108 cookie_cross_origin,109 undefined);110 fetchTest("cross-origin + no-cors + credentials:same-origin",111 cross_origin, 'no-cors', 'same-origin',112 undefined,113 undefined);...
Using AI Code Generation
1wpt.fetchTest('12345', function(err, data) {2 console.log(data);3});4wpt.getTestStatus('12345', function(err, data) {5 console.log(data);6});7wpt.getLocations(function(err, data) {8 console.log(data);9});10wpt.getTesters(function(err, data) {11 console.log(data);12});13wpt.getVideo('12345', function(err, data) {14 console.log(data);15});16wpt.getWaterfall('12345', function(err, data) {17 console.log(data);18});19wpt.getScreenshot('12345', function(err, data) {20 console.log(data);21});22wpt.getHar('12345', function(err, data) {23 console.log(data);24});25wpt.getPagespeed('12345', function(err, data) {26 console.log(data);27});28wpt.getRequests('12345', function(err, data) {29 console.log(data);30});31wpt.getTimings('12345', function(err, data) {32 console.log(data);33});34wpt.getBreakdown('12345', function(err, data) {35 console.log(data);36});37wpt.getDoms('12345', function(err, data) {38 console.log(data);39});
Using AI Code Generation
1wpt.fetchTest('testid', function(data) {2 console.log(data);3});4wpt.fetchTest('testid', function(data) {5 console.log(data);6});7wpt.fetchTest('testid', function(data) {8 console.log(data);9});10wpt.fetchTest('testid', function(data) {11 console.log(data);12});13wpt.fetchTest('testid', function(data) {14 console.log(data);15});16wpt.fetchTest('testid', function(data) {17 console.log(data);18});19wpt.fetchTest('testid', function(data) {20 console.log(data);21});22wpt.fetchTest('testid', function(data) {23 console.log(data);24});25wpt.fetchTest('testid', function(data) {26 console.log(data);27});28wpt.fetchTest('testid', function(data) {29 console.log(data);30});31wpt.fetchTest('testid', function(data) {32 console.log(data);33});34wpt.fetchTest('testid', function(data) {35 console.log(data);36});37wpt.fetchTest('testid', function(data) {38 console.log(data);39});40wpt.fetchTest('testid', function(data) {41 console.log(data);42});
Using AI Code Generation
1const wpt = require('wpt');2wpt.fetchTest('testId', function(err, test) {3});4const wpt = require('wpt');5wpt.fetchTest('testId', function(err, test) {6});7const wpt = require('wpt');8wpt.fetchTest('testId', function(err, test) {9});10const wpt = require('wpt');11wpt.fetchTest('testId', function(err, test) {12});13const wpt = require('wpt');14wpt.fetchTest('testId', function(err, test) {15});16const wpt = require('wpt');17wpt.fetchTest('testId', function(err, test) {18});19const wpt = require('wpt');20wpt.fetchTest('testId', function(err, test) {21});22const wpt = require('wpt');23wpt.fetchTest('testId', function(err, test) {24});25const wpt = require('wpt');26wpt.fetchTest('testId', function(err, test) {27});28const wpt = require('wpt');29wpt.fetchTest('testId', function(err, test) {30});31const wpt = require('w
Using AI Code Generation
1var wpt = require('./wpt.js');2 console.log(data);3});4var request = require('request');5var wpt = {6 fetchTest: function(url,callback) {7 request(wptUrl, function (error, response, body) {8 if (!error && response.statusCode == 200) {9 }10 });11 }12}13module.exports = wpt;
Using AI Code Generation
1var wpt = require('wpt-api');2var fs = require('fs');3var apiKey = process.env.WPT_API_KEY;4var wpt = new wpt(apiKey);5var script = fs.readFileSync('script.js', 'utf8');6wpt.runTest(testUrl, {7}, function (err, data) {8 if(err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14{15 "data": {16 "data": {
Using AI Code Generation
1var wpt = require('wpt');2wpt.fetchTest('1234', {pretty: 1}, function(err, data) {3});4var wpt = require('wpt');5wpt.fetchTest('1234', {pretty: 1}, function(err, data) {6});7var wpt = require('wpt');8wpt.fetchTest('1234', {pretty: 1}, function(err, data) {9});10var wpt = require('wpt');11wpt.fetchTest('1234', {pretty: 1}, function(err, data) {12});13var wpt = require('wpt');14wpt.fetchTest('1234', {pretty: 1}, function(err, data) {15});16var wpt = require('wpt');17wpt.fetchTest('1234', {pretty: 1}, function(err, data) {18});19var wpt = require('wpt');20wpt.fetchTest('1234', {pretty: 1}, function(err, data) {21});22var wpt = require('wpt');23wpt.fetchTest('1234', {pretty: 1}, function(err, data) {24});25var wpt = require('wpt');26wpt.fetchTest('1234', {pretty: 1}, function(err, data) {27});28var wpt = require('wpt');29wpt.fetchTest('1234', {pretty: 1}, function(err, data) {
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPagetest('www.webpagetest.org');3wpt.fetchTest('170105_5A_4f2c4e7f8d1c1b7d4b1e1d7d8e9c9f9e', function(err, data) {4 if(err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wpt = new WebPagetest('www.webpagetest.org');12wpt.getLocations(function(err, data) {13 if(err) {14 console.log('Error: ' + err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wpt = new WebPagetest('www.webpagetest.org');21wpt.getTesters(function(err, data) {22 if(err) {23 console.log('Error: ' + err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wpt = new WebPagetest('www.webpagetest.org');30wpt.getLocations(function(err, data) {31 if(err) {32 console.log('Error: ' + err);33 } else {34 console.log(data);35 }36});37var wpt = require('wpt');38var wpt = new WebPagetest('www.webpagetest.org');39wpt.getTesters(function(err, data) {40 if(err) {41 console.log('Error: ' + err);42 } else {43 console.log(data);44 }45});46var wpt = require('wpt');47var wpt = new WebPagetest('www.webpagetest.org');48wpt.getLocations(function(err, data) {49 if(err) {50 console.log('Error: ' + err);51 } else {52 console.log(data);
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!!