How to use sourceResolveOptions method in wpt

Best JavaScript code snippet using wpt

support.sub.js

Source:support.sub.js Github

copy

Full Screen

...112//113// `server` identifies the server from which to load the document.114// `treatAsPublic`, if set to true, specifies that the source document should115// be artificially placed in the `public` address space using CSP.116function sourceResolveOptions({ server, treatAsPublic }) {117 const options = {...server};118 if (treatAsPublic) {119 options.headers = { "Content-Security-Policy": "treat-as-public-address" };120 }121 return options;122}123// Computes options to pass to `resolveUrl()` for `resources/preflight.py`.124//125// `server` identifies the server from which to load the resource.126// `behavior` specifies the behavior of the target server. It may contain:127// - `preflight`: The result of calling one of `PreflightBehavior`'s methods.128// - `response`: The result of calling one of `ResponseBehavior`'s methods.129function targetResolveOptions({ server, behavior }) {130 const options = {...server};131 if (behavior) {132 const { preflight, response } = behavior;133 options.searchParams = {134 ...preflight,135 ...response,136 };137 }138 return options;139}140// Methods generate behavior specifications for how `resources/preflight.py`141// should behave upon receiving a preflight request.142const PreflightBehavior = {143 // The preflight response should fail with a non-2xx code.144 failure: () => ({}),145 // The preflight response should be missing CORS headers.146 // `uuid` should be a UUID that uniquely identifies the preflight request.147 noCorsHeader: (uuid) => ({148 "preflight-uuid": uuid,149 }),150 // The preflight response should be missing PNA headers.151 // `uuid` should be a UUID that uniquely identifies the preflight request.152 noPnaHeader: (uuid) => ({153 "preflight-uuid": uuid,154 "preflight-headers": "cors",155 }),156 // The preflight response should succeed.157 // `uuid` should be a UUID that uniquely identifies the preflight request.158 success: (uuid) => ({159 "preflight-uuid": uuid,160 "preflight-headers": "cors+pna",161 }),162};163// Methods generate behavior specifications for how `resources/preflight.py`164// should behave upon receiving a regular (non-preflight) request.165const ResponseBehavior = {166 // The response should succeed without CORS headers.167 default: () => ({}),168 // The response should succeed with CORS headers.169 allowCrossOrigin: () => ({ "final-headers": "cors" }),170};171const FetchTestResult = {172 SUCCESS: {173 ok: true,174 body: "success",175 },176 OPAQUE: {177 ok: false,178 type: "opaque",179 body: "",180 },181 FAILURE: {182 error: "TypeError: Failed to fetch",183 },184};185// Runs a fetch test. Tries to fetch a given subresource from a given document.186//187// Main argument shape:188//189// {190// // Optional. Passed to `sourceResolveOptions()`.191// source,192//193// // Optional. Passed to `targetResolveOptions()`.194// target,195//196// // Optional. Passed to `fetch()`.197// fetchOptions,198//199// // Required. One of the values in `FetchTestResult`.200// expected,201// }202//203async function fetchTest(t, { source, target, fetchOptions, expected }) {204 const sourceUrl =205 resolveUrl("resources/fetcher.html", sourceResolveOptions(source));206 const targetUrl =207 resolveUrl("resources/preflight.py", targetResolveOptions(target));208 const iframe = await appendIframe(t, document, sourceUrl);209 const reply = futureMessage();210 const message = {211 url: targetUrl.href,212 options: fetchOptions,213 };214 iframe.contentWindow.postMessage(message, "*");215 const { error, ok, type, body } = await reply;216 assert_equals(error, expected.error, "error");217 assert_equals(ok, expected.ok, "response ok");218 assert_equals(body, expected.body, "response body");219 if (expected.type !== undefined) {220 assert_equals(type, expected.type, "response type");221 }222}223const XhrTestResult = {224 SUCCESS: {225 loaded: true,226 status: 200,227 body: "success",228 },229 FAILURE: {230 loaded: false,231 status: 0,232 },233};234// Runs an XHR test. Tries to fetch a given subresource from a given document.235//236// Main argument shape:237//238// {239// // Optional. Passed to `sourceResolveOptions()`.240// source,241//242// // Optional. Passed to `targetResolveOptions()`.243// target,244//245// // Optional. Method to use when sending the request. Defaults to "GET".246// method,247//248// // Required. One of the values in `XhrTestResult`.249// expected,250// }251//252async function xhrTest(t, { source, target, method, expected }) {253 const sourceUrl =254 resolveUrl("resources/xhr-sender.html", sourceResolveOptions(source));255 const targetUrl =256 resolveUrl("resources/preflight.py", targetResolveOptions(target));257 const iframe = await appendIframe(t, document, sourceUrl);258 const reply = futureMessage();259 const message = {260 url: targetUrl.href,261 method: method,262 };263 iframe.contentWindow.postMessage(message, "*");264 const { loaded, status, body } = await reply;265 assert_equals(loaded, expected.loaded, "response loaded");266 assert_equals(status, expected.status, "response status");267 assert_equals(body, expected.body, "response body");268}269const WebsocketTestResult = {270 SUCCESS: "open",271 // The code is a best guess. It is not yet entirely specified, so it may need272 // to be changed in the future based on implementation experience.273 FAILURE: "close: code 1006",274};275// Runs a websocket test. Attempts to open a websocket from `source` (in an276// iframe) to `target`, then checks that the result is as `expected`.277//278// Argument shape:279//280// {281// // Required. Passed to `sourceResolveOptions()`.282// source,283//284// // Required.285// target: {286// // Required. Target server.287// server,288// }289//290// // Required. Should be one of the values in `WebsocketTestResult`.291// expected,292// }293//294async function websocketTest(t, { source, target, expected }) {295 const sourceUrl =296 resolveUrl("resources/socket-opener.html", sourceResolveOptions(source));297 const targetUrl = resolveUrl("/echo", target.server);298 const iframe = await appendIframe(t, document, sourceUrl);299 const reply = futureMessage();300 iframe.contentWindow.postMessage(targetUrl.href, "*");301 assert_equals(await reply, expected);302}303const WorkerScriptTestResult = {304 SUCCESS: { loaded: true },305 FAILURE: { error: "unknown error" },306};307async function workerScriptTest(t, { source, target, expected }) {308 const sourceUrl =309 resolveUrl("resources/worker-fetcher.html", sourceResolveOptions(source));310 const targetUrl =311 resolveUrl("resources/preflight.py", targetResolveOptions(target));312 targetUrl.searchParams.append("body", "postMessage({ loaded: true })")313 targetUrl.searchParams.append("mime-type", "application/javascript")314 const iframe = await appendIframe(t, document, sourceUrl);315 const reply = futureMessage();316 iframe.contentWindow.postMessage({ url: targetUrl.href }, "*");317 const { error, loaded } = await reply;318 assert_equals(error, expected.error, "worker error");319 assert_equals(loaded, expected.loaded, "response loaded");320}321async function sharedWorkerScriptTest(t, { source, target, expected }) {322 const sourceUrl = resolveUrl("resources/shared-worker-fetcher.html",323 sourceResolveOptions(source));324 const targetUrl =325 resolveUrl("resources/preflight.py", targetResolveOptions(target));326 targetUrl.searchParams.append(327 "body", "onconnect = (e) => e.ports[0].postMessage({ loaded: true })")328 const iframe = await appendIframe(t, document, sourceUrl);329 const reply = futureMessage();330 iframe.contentWindow.postMessage({ url: targetUrl.href }, "*");331 const { error, loaded } = await reply;332 assert_equals(error, expected.error, "worker error");333 assert_equals(loaded, expected.loaded, "response loaded");334}335// Results that may be expected in tests.336const WorkerFetchTestResult = {337 SUCCESS: { status: 200, body: "success" },338 FAILURE: { error: "TypeError" },339};340async function workerFetchTest(t, { source, target, expected }) {341 const targetUrl =342 resolveUrl("resources/preflight.py", targetResolveOptions(target));343 const sourceUrl =344 resolveUrl("resources/fetcher.js", sourceResolveOptions(source));345 sourceUrl.searchParams.append("url", targetUrl.href);346 const fetcherUrl = new URL("worker-fetcher.html", sourceUrl);347 const reply = futureMessage();348 const iframe = await appendIframe(t, document, fetcherUrl);349 iframe.contentWindow.postMessage({ url: sourceUrl.href }, "*");350 const { error, status, message } = await reply;351 assert_equals(error, expected.error, "fetch error");352 assert_equals(status, expected.status, "response status");353 assert_equals(message, expected.message, "response body");354}355async function sharedWorkerFetchTest(t, { source, target, expected }) {356 const targetUrl =357 resolveUrl("resources/preflight.py", targetResolveOptions(target));358 const sourceUrl =359 resolveUrl("resources/shared-fetcher.js", sourceResolveOptions(source));360 sourceUrl.searchParams.append("url", targetUrl.href);361 const fetcherUrl = new URL("shared-worker-fetcher.html", sourceUrl);362 const reply = futureMessage();363 const iframe = await appendIframe(t, document, fetcherUrl);364 iframe.contentWindow.postMessage({ url: sourceUrl.href }, "*");365 const { error, status, message } = await reply;366 assert_equals(error, expected.error, "fetch error");367 assert_equals(status, expected.status, "response status");368 assert_equals(message, expected.message, "response body");...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getLocations(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10[ { Location: 'Dulles:Chrome',

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var options = {3};4wpt.runTest(options, function(err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});11{ statusCode: 200,12 { statusCode: 200,13 { testId: '170111_0B_1',14{ statusCode: 200,15 { statusCode: 200,16 { testId: '170111_0B_1',

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = WebPageTest('www.webpagetest.org', 'API Key');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9{10 "data": {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful