Best JavaScript code snippet using wpt
test-RegExpRoute.mjs
Source:test-RegExpRoute.mjs
1/*2 Copyright 2018 Google LLC3 Use of this source code is governed by an MIT-style4 license that can be found in the LICENSE file or at5 https://opensource.org/licenses/MIT.6*/7import {RegExpRoute} from 'workbox-routing/RegExpRoute.mjs';8describe(`RegExpRoute`, function() {9 const SAME_ORIGIN_URL = new URL('https://example.com');10 const CROSS_ORIGIN_URL = new URL('https://cross-origin-example.com');11 const PATH = '/test/path';12 const HANDLER = {handle: () => {}};13 const sandbox = sinon.createSandbox();14 beforeEach(function() {15 sandbox.restore();16 sandbox.stub(self, 'location').value(SAME_ORIGIN_URL);17 });18 after(function() {19 sandbox.restore();20 });21 for (const badRegExp of [undefined, null, 123, '123', {}]) {22 it(`should throw when called with a regExp parameter of ${JSON.stringify(badRegExp)} in dev`, async function() {23 if (process.env.NODE_ENV === 'production') this.skip();24 await expectError(25 () => new RegExpRoute(),26 'incorrect-class',27 (error) => {28 expect(error.details).to.have.property('moduleName').that.equals('workbox-routing');29 expect(error.details).to.have.property('className').that.equals('RegExpRoute');30 expect(error.details).to.have.property('funcName').that.equals('constructor');31 expect(error.details).to.have.property('paramName').that.equals('pattern');32 },33 );34 });35 }36 it(`should not throw when called with valid parameters`, function() {37 expect(() => new RegExpRoute(new RegExp('/test/'), HANDLER)).not.to.throw();38 });39 it(`should properly match URLs`, function() {40 const matchingURL = new URL(PATH, SAME_ORIGIN_URL);41 const nonMatchingURL = new URL('/does/not/match', SAME_ORIGIN_URL);42 const crossOriginURL = new URL(PATH, CROSS_ORIGIN_URL);43 const regExp = new RegExp(PATH);44 const route = new RegExpRoute(regExp, HANDLER);45 expect(route.match({url: matchingURL})).to.be.ok;46 expect(route.match({url: nonMatchingURL})).not.to.be.ok;47 // This route will not match because while the RegExp matches, the match48 // doesn't occur at the start of the cross-origin URL.49 expect(route.match({url: crossOriginURL})).not.to.be.ok;50 });51 it(`should properly match cross-origin URLs with wildcards`, function() {52 const matchingURL = new URL('https://fonts.googleapis.com/icon?family=Material+Icons');53 const matchingURL2 = new URL('https://code.getmdl.io/1.2.1/material.indigo-pink.min.css');54 const route = new RegExpRoute(/.*\.(?:googleapis|getmdl)\.(?:com|io)\/.*/, HANDLER);55 expect(route.match({url: matchingURL})).to.be.ok;56 expect(route.match({url: matchingURL2})).to.be.ok;57 });58 it(`should properly match cross-origin URLs without wildcards`, function() {59 const matchingURL = new URL(PATH, CROSS_ORIGIN_URL);60 const nonMatchingURL = new URL('/does/not/match', CROSS_ORIGIN_URL);61 const crossOriginRegExp = new RegExp(matchingURL.href);62 const route = new RegExpRoute(crossOriginRegExp, HANDLER);63 expect(route.match({url: matchingURL})).to.be.ok;64 expect(route.match({url: nonMatchingURL})).not.to.be.ok;65 });66 it(`should properly match URLs with capture groups`, function() {67 const value1 = 'value1';68 const value2 = 'value2';69 const captureGroupRegExp = new RegExp('/(\\w+)/dummy/(\\w+)');70 const captureGroupMatchingURL = new URL(`/${value1}/dummy/${value2}`, SAME_ORIGIN_URL);71 const captureGroupNonMatchingURL = new URL(`/${value1}/${value2}`, SAME_ORIGIN_URL);72 const route = new RegExpRoute(captureGroupRegExp, HANDLER);73 const match = route.match({url: captureGroupMatchingURL});74 expect(match.length).to.equal(2);75 expect(match[0]).to.equal(value1);76 expect(match[1]).to.equal(value2);77 expect(route.match({url: captureGroupNonMatchingURL})).not.to.be.ok;78 });...
Using AI Code Generation
1url.pathname = url.pathname + "/same-origin-url";2url2.pathname = url2.pathname + "/same-origin-url";3url3.pathname = url3.pathname + "/same-origin-url";4url3.searchParams.set("redirect", "true");5url4.pathname = url4.pathname + "/same-origin-url";6url4.searchParams.set("redirect", "true");7url4.searchParams.set("redirect_status", "307");8url5.pathname = url5.pathname + "/same-origin-url";9url5.searchParams.set("redirect", "true");10url5.searchParams.set("redirect_status", "308");11url6.pathname = url6.pathname + "/same-origin-url";12url6.searchParams.set("redirect", "true");13url6.searchParams.set("redirect_status", "307");14url6.searchParams.set("redirect_count", "20");
Using AI Code Generation
1var url = wpt.same_origin_url("test.html");2var xhr = new XMLHttpRequest();3xhr.open("GET", url, false);4xhr.send();5if (xhr.status == 200) {6 var response = xhr.responseText;7} else {8}
Using AI Code Generation
1function same_origin_url(url) {2 return fetch(url).then(r => r.text());3}4function cross_origin_url(url) {5 return fetch(url).then(r => r.text());6}7function fetch_url(url) {8 return fetch(url).then(r => r.text());9}10function fetch_cross_origin_url(url) {11 return fetch(url).then(r => r.text());12}13function fetch_cross_origin_url(url) {14 return fetch(url).then(r => r.text());15}16function fetch_cross_origin_url(url) {17 return fetch(url).then(r => r.text());18}19function fetch_cross_origin_url(url) {20 return fetch(url).then(r => r.text());21}22function fetch_cross_origin_url(url) {23 return fetch(url).then(r => r.text());24}25function fetch_cross_origin_url(url) {26 return fetch(url).then(r => r.text());27}
Using AI Code Generation
1var url = same_origin_url();2var xhr = new XMLHttpRequest();3xhr.open("GET", url + "/resources/feature-policy-allow-scripts.php");4xhr.send();5if (xhr.status == 200) {6 test_result = true;7}8return test_result;9return false;10return "NOT SUPPORTED";
Using AI Code Generation
1async function testSameOriginFetch(){2 let origin = await wptagent.same_origin_url();3 let response = await fetch(origin);4 if(response.status == 200){5 console.log("Same Origin Fetch Passed");6 }7 else{8 console.log("Same Origin Fetch Failed");9 }10}11testSameOriginFetch();12This test works as expected when run in Chrome 83, but fails when run in Chrome 84. The same_origin_url() call returns the correct URL, but the fetch() call fails with a CORS error:
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!!