Best JavaScript code snippet using wpt
service-worker-fetch.https.window.js
Source:service-worker-fetch.https.window.js
1// META: script=/common/utils.js2// META: script=resources/support.sub.js3//4// Spec: https://wicg.github.io/private-network-access/#integration-fetch5//6// These tests check that fetches from within `ServiceWorker` scripts are7// subject to Private Network Access checks, just like fetches from within8// documents.9// Results that may be expected in tests.10const TestResult = {11 SUCCESS: { ok: true, body: "success" },12 FAILURE: { error: "TypeError" },13};14async function makeTest(t, { source, target, expected }) {15 const bridgeUrl = resolveUrl(16 "resources/service-worker-bridge.html",17 sourceResolveOptions({ server: source.server }));18 const scriptUrl =19 resolveUrl("resources/service-worker.js", sourceResolveOptions(source));20 const realTargetUrl = preflightUrl(target);21 // Fetch a URL within the service worker's scope, but tell it which URL to22 // really fetch.23 const targetUrl = new URL("service-worker-proxy", scriptUrl);24 targetUrl.searchParams.append("proxied-url", realTargetUrl.href);25 const iframe = await appendIframe(t, document, bridgeUrl);26 const request = (message) => {27 const reply = futureMessage();28 iframe.contentWindow.postMessage(message, "*");29 return reply;30 };31 {32 const { error, loaded } = await request({33 action: "register",34 url: scriptUrl.href,35 });36 assert_equals(error, undefined, "register error");37 assert_true(loaded, "response loaded");38 }39 try {40 const { controlled, numControllerChanges } = await request({41 action: "wait",42 numControllerChanges: 1,43 });44 assert_equals(numControllerChanges, 1, "controller change");45 assert_true(controlled, "bridge script is controlled");46 const { error, ok, body } = await request({47 action: "fetch",48 url: targetUrl.href,49 });50 assert_equals(error, expected.error, "fetch error");51 assert_equals(ok, expected.ok, "response ok");52 assert_equals(body, expected.body, "response body");53 } finally {54 // Always unregister the service worker.55 const { error, unregistered } = await request({56 action: "unregister",57 scope: new URL("./", scriptUrl).href,58 });59 assert_equals(error, undefined, "unregister error");60 assert_true(unregistered, "unregistered");61 }62}63promise_test(t => makeTest(t, {64 source: { server: Server.HTTPS_LOCAL },65 target: { server: Server.HTTPS_LOCAL },66 expected: TestResult.SUCCESS,67}), "local to local: success.");68promise_test(t => makeTest(t, {69 source: { server: Server.HTTPS_PRIVATE },70 target: {71 server: Server.HTTPS_LOCAL,72 behavior: { response: ResponseBehavior.allowCrossOrigin() },73 },74 expected: TestResult.FAILURE,75}), "private to local: failed preflight.");76promise_test(t => makeTest(t, {77 source: { server: Server.HTTPS_PRIVATE },78 target: {79 server: Server.HTTPS_LOCAL,80 behavior: {81 preflight: PreflightBehavior.success(token()),82 response: ResponseBehavior.allowCrossOrigin(),83 },84 },85 expected: TestResult.SUCCESS,86}), "private to local: success.");87promise_test(t => makeTest(t, {88 source: { server: Server.HTTPS_PRIVATE },89 target: { server: Server.HTTPS_PRIVATE },90 expected: TestResult.SUCCESS,91}), "private to private: success.");92promise_test(t => makeTest(t, {93 source: { server: Server.HTTPS_PUBLIC },94 target: {95 server: Server.HTTPS_LOCAL,96 behavior: { response: ResponseBehavior.allowCrossOrigin() },97 },98 expected: TestResult.FAILURE,99}), "public to local: failed preflight.");100promise_test(t => makeTest(t, {101 source: { server: Server.HTTPS_PUBLIC },102 target: {103 server: Server.HTTPS_LOCAL,104 behavior: {105 preflight: PreflightBehavior.success(token()),106 response: ResponseBehavior.allowCrossOrigin(),107 },108 },109 expected: TestResult.SUCCESS,110}), "public to local: success.");111promise_test(t => makeTest(t, {112 source: { server: Server.HTTPS_PUBLIC },113 target: {114 server: Server.HTTPS_PRIVATE,115 behavior: { response: ResponseBehavior.allowCrossOrigin() },116 },117 expected: TestResult.FAILURE,118}), "public to private: failed preflight.");119promise_test(t => makeTest(t, {120 source: { server: Server.HTTPS_PUBLIC },121 target: {122 server: Server.HTTPS_PRIVATE,123 behavior: {124 preflight: PreflightBehavior.success(token()),125 response: ResponseBehavior.allowCrossOrigin(),126 },127 },128 expected: TestResult.SUCCESS,129}), "public to private: success.");130promise_test(t => makeTest(t, {131 source: { server: Server.HTTPS_PUBLIC },132 target: { server: Server.HTTPS_PUBLIC },133 expected: TestResult.SUCCESS,134}), "public to public: success.");135promise_test(t => makeTest(t, {136 source: {137 server: Server.HTTPS_LOCAL,138 treatAsPublic: true,139 },140 target: { server: Server.HTTPS_LOCAL },141 expected: TestResult.FAILURE,142}), "treat-as-public to local: failed preflight.");143promise_test(t => makeTest(t, {144 source: {145 server: Server.HTTPS_LOCAL,146 treatAsPublic: true,147 },148 target: {149 server: Server.HTTPS_LOCAL,150 behavior: { preflight: PreflightBehavior.success(token()) },151 },152 expected: TestResult.SUCCESS,153}), "treat-as-public to local: success.");154promise_test(t => makeTest(t, {155 source: {156 server: Server.HTTPS_LOCAL,157 treatAsPublic: true,158 },159 target: {160 server: Server.HTTPS_PRIVATE,161 behavior: { response: ResponseBehavior.allowCrossOrigin() },162 },163 expected: TestResult.FAILURE,164}), "treat-as-public to private: failed preflight.");165promise_test(t => makeTest(t, {166 source: {167 server: Server.HTTPS_LOCAL,168 treatAsPublic: true,169 },170 target: {171 server: Server.HTTPS_PRIVATE,172 behavior: {173 preflight: PreflightBehavior.success(token()),174 response: ResponseBehavior.allowCrossOrigin(),175 },176 },177 expected: TestResult.SUCCESS,178}), "treat-as-public to private: success.");179promise_test(t => makeTest(t, {180 source: {181 server: Server.HTTPS_LOCAL,182 treatAsPublic: true,183 },184 target: {185 server: Server.HTTPS_PUBLIC,186 behavior: { response: ResponseBehavior.allowCrossOrigin() },187 },188 expected: TestResult.SUCCESS,...
page-searcher.ts
Source:page-searcher.ts
1export interface PostContentItem {2 href: string;3 score: number;4 title: string;5}6export class PageSearcher {7 constructor() {}8 getPostsOnForum(finder: CheerioStatic): string[] {9 let result = [];10 let itemList = finder('table.olt');11 itemList.find('tr').each(function (item) {12 let tr = finder(this);13 let a_href = tr.find('.title').children('a').attr('href');14 // let a_title = tr.find('.title').children('a').attr('title');15 if (a_href) {16 result.push(a_href);17 }18 });19 return result;20 }21 getContentFromPost(finder: CheerioStatic, keywords: string[]): PostContentItem {22 let result = {23 href: '',24 score: 0,25 title: ''26 };27 // ä»ç®æ 页é¢æ£æé¨åå¹é
å
³é®è¯28 let node = finder('div#link-report');29 let contentNode = node.find('div.topic-content');30 let content = contentNode.text();31 let score = 0,32 total = 0,33 temp = 0;34 for (let i = 0; i < keywords.length; i++) {35 total += (i + 1);36 if (content.indexOf(keywords[i]) !== -1) {37 temp += (i + 1);38 }39 score = Math.floor(((temp / total) * 100));40 }41 // ä»ç®æ 页é¢ä¸å¯»æ¾é¡µé¢URLä¿¡æ¯42 let infoNode = finder('div#sep.tabs');43 let targetURL = infoNode.children().first().attr('href');44 let realTargetURL = targetURL.split('#');45 // ä»ç®æ 页é¢ä¸å¯»æ¾æç« æ é¢46 let titleNode = finder('div#content');47 let title = titleNode.children().first().text();48 result = Object.assign(result, {49 href: realTargetURL[0],50 score: score,51 title: title.trim()52 });53 return result;54 }...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.realTargetUrl('www.webpagetest.org', function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});
Using AI Code Generation
1var wptWrapper = require('./wptWrapper.js');2 if (err) {3 console.log(err);4 } else {5 console.log(realUrl);6 }7});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test ID: %s', data.data.testId);5 console.log('Target URL: %s', data.data.summary);6 console.log('Target URL: %s', data.data.summary);7 console.log('Real Target URL: %s', data.data.summary);8 console.log('Location: %s', data.data.location);9 console.log('From: %s', data.data.from);10 console.log('Connectivity: %s', data.data.connectivity);11 console.log('Runs: %s', data.data.runs);12});
Using AI Code Generation
1var wptHook = require('wptHook');2 console.log('realTargetUrl: ' + url);3});4### wptHook.realTargetUrl(url, callback)5### wptHook.getTestResults(location, testId, callback)6callback(err, results)7### wptHook.getTestResultsForUrl(location, url, callback)8callback(err, results)9### wptHook.getTestResultsForUrlWithParams(location, url, params, callback)10callback(err, results)
Using AI Code Generation
1var wptHook = require('wptHook');2var targetUrl = wptHook.realTargetUrl();3console.log(targetUrl);4#### wptHook.realTargetUrl()5#### wptHook.realTargetUrl()6#### wptHook.testId()7#### wptHook.testPath()8#### wptHook.testUrl()9#### wptHook.testUrlHost()10#### wptHook.testUrlPath()11#### wptHook.testUrlPort()
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!!