Best JavaScript code snippet using wpt
sanitizeUriSpec.js
Source:sanitizeUriSpec.js
1'use strict';2describe('sanitizeUri', function() {3 var sanitizeHref, sanitizeImg, sanitizeUriProvider, testUrl;4 beforeEach(function() {5 module(function(_$$sanitizeUriProvider_) {6 sanitizeUriProvider = _$$sanitizeUriProvider_;7 });8 inject(function($$sanitizeUri) {9 sanitizeHref = function(uri) {10 return $$sanitizeUri(uri, false);11 };12 sanitizeImg = function(uri) {13 return $$sanitizeUri(uri, true);14 };15 });16 });17 function isEvilInCurrentBrowser(uri) {18 var a = document.createElement('a');19 a.setAttribute('href', uri);20 return a.href.substring(0, 4) !== 'http';21 }22 describe('img[src] sanitization', function() {23 it('should sanitize javascript: urls', function() {24 /* jshint scripturl:true */25 testUrl = "javascript:doEvilStuff()";26 expect(sanitizeImg(testUrl)).toBe('unsafe:javascript:doEvilStuff()');27 });28 it('should sanitize javascript: urls with comments', function() {29 /* jshint scripturl:true */30 testUrl = "javascript:alert(1)//data:image/";31 expect(sanitizeImg(testUrl)).toBe('unsafe:javascript:alert(1)//data:image/');32 });33 it('should sanitize non-image data: urls', function() {34 testUrl = "data:application/javascript;charset=US-ASCII,alert('evil!');";35 expect(sanitizeImg(testUrl)).toBe("unsafe:data:application/javascript;charset=US-ASCII,alert('evil!');");36 testUrl = "data:,foo";37 expect(sanitizeImg(testUrl)).toBe("unsafe:data:,foo");38 });39 it('should not sanitize data: URIs for images', function() {40 // image data uri41 // ref: http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever42 testUrl = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";43 expect(sanitizeImg(testUrl)).toBe('data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');44 });45 it('should sanitize mailto: urls', function() {46 testUrl = "mailto:foo@bar.com";47 expect(sanitizeImg(testUrl)).toBe('unsafe:mailto:foo@bar.com');48 });49 it('should sanitize obfuscated javascript: urls', function() {50 /* jshint scripturl:true */51 // case-sensitive52 testUrl = "JaVaScRiPt:doEvilStuff()";53 expect(sanitizeImg(testUrl)).toBe('unsafe:javascript:doEvilStuff()');54 // tab in protocol55 testUrl = "java\u0009script:doEvilStuff()";56 if (isEvilInCurrentBrowser(testUrl)) {57 expect(sanitizeImg(testUrl)).toEqual('unsafe:javascript:doEvilStuff()');58 }59 // space before60 testUrl = " javascript:doEvilStuff()";61 expect(sanitizeImg(testUrl)).toBe('unsafe:javascript:doEvilStuff()');62 // ws chars before63 testUrl = " \u000e javascript:doEvilStuff()";64 if (isEvilInCurrentBrowser(testUrl)) {65 expect(sanitizeImg(testUrl)).toEqual('unsafe:javascript:doEvilStuff()');66 }67 // post-fixed with proper url68 testUrl = "javascript:doEvilStuff(); http://make.me/look/good";69 expect(sanitizeImg(testUrl)).toBeOneOf(70 'unsafe:javascript:doEvilStuff(); http://make.me/look/good',71 'unsafe:javascript:doEvilStuff();%20http://make.me/look/good'72 );73 });74 it('should sanitize ng-src bindings as well', function() {75 /* jshint scripturl:true */76 testUrl = "javascript:doEvilStuff()";77 expect(sanitizeImg(testUrl)).toBe('unsafe:javascript:doEvilStuff()');78 });79 it('should not sanitize valid urls', function() {80 testUrl = "foo/bar";81 expect(sanitizeImg(testUrl)).toBe('foo/bar');82 testUrl = "/foo/bar";83 expect(sanitizeImg(testUrl)).toBe('/foo/bar');84 testUrl = "../foo/bar";85 expect(sanitizeImg(testUrl)).toBe('../foo/bar');86 testUrl = "#foo";87 expect(sanitizeImg(testUrl)).toBe('#foo');88 testUrl = "http://foo.com/bar";89 expect(sanitizeImg(testUrl)).toBe('http://foo.com/bar');90 testUrl = " http://foo.com/bar";91 expect(sanitizeImg(testUrl)).toBe(' http://foo.com/bar');92 testUrl = "https://foo.com/bar";93 expect(sanitizeImg(testUrl)).toBe('https://foo.com/bar');94 testUrl = "ftp://foo.com/bar";95 expect(sanitizeImg(testUrl)).toBe('ftp://foo.com/bar');96 testUrl = "file:///foo/bar.html";97 expect(sanitizeImg(testUrl)).toBe('file:///foo/bar.html');98 });99 it('should allow reconfiguration of the src whitelist', function() {100 /* jshint scripturl:true */101 var returnVal;102 expect(sanitizeUriProvider.imgSrcSanitizationWhitelist() instanceof RegExp).toBe(true);103 returnVal = sanitizeUriProvider.imgSrcSanitizationWhitelist(/javascript:/);104 expect(returnVal).toBe(sanitizeUriProvider);105 testUrl = "javascript:doEvilStuff()";106 expect(sanitizeImg(testUrl)).toBe('javascript:doEvilStuff()');107 testUrl = "http://recon/figured";108 expect(sanitizeImg(testUrl)).toBe('unsafe:http://recon/figured');109 });110 });111 describe('a[href] sanitization', function() {112 it('should sanitize javascript: urls', inject(function() {113 /* jshint scripturl:true */114 testUrl = "javascript:doEvilStuff()";115 expect(sanitizeHref(testUrl)).toBe('unsafe:javascript:doEvilStuff()');116 }));117 it('should sanitize data: urls', inject(function() {118 testUrl = "data:evilPayload";119 expect(sanitizeHref(testUrl)).toBe('unsafe:data:evilPayload');120 }));121 it('should sanitize obfuscated javascript: urls', inject(function() {122 /* jshint scripturl:true */123 // case-sensitive124 testUrl = "JaVaScRiPt:doEvilStuff()";125 expect(sanitizeHref(testUrl)).toBe('unsafe:javascript:doEvilStuff()');126 // tab in protocol127 testUrl = "java\u0009script:doEvilStuff()";128 if (isEvilInCurrentBrowser(testUrl)) {129 expect(sanitizeHref(testUrl)).toEqual('unsafe:javascript:doEvilStuff()');130 }131 // space before132 testUrl = " javascript:doEvilStuff()";133 expect(sanitizeHref(testUrl)).toBe('unsafe:javascript:doEvilStuff()');134 // ws chars before135 testUrl = " \u000e javascript:doEvilStuff()";136 if (isEvilInCurrentBrowser(testUrl)) {137 expect(sanitizeHref(testUrl)).toEqual('unsafe:javascript:doEvilStuff()');138 }139 // post-fixed with proper url140 testUrl = "javascript:doEvilStuff(); http://make.me/look/good";141 expect(sanitizeHref(testUrl)).toBeOneOf(142 'unsafe:javascript:doEvilStuff(); http://make.me/look/good',143 'unsafe:javascript:doEvilStuff();%20http://make.me/look/good'144 );145 }));146 it('should sanitize ngHref bindings as well', inject(function() {147 /* jshint scripturl:true */148 testUrl = "javascript:doEvilStuff()";149 expect(sanitizeHref(testUrl)).toBe('unsafe:javascript:doEvilStuff()');150 }));151 it('should not sanitize valid urls', inject(function() {152 testUrl = "foo/bar";153 expect(sanitizeHref(testUrl)).toBe('foo/bar');154 testUrl = "/foo/bar";155 expect(sanitizeHref(testUrl)).toBe('/foo/bar');156 testUrl = "../foo/bar";157 expect(sanitizeHref(testUrl)).toBe('../foo/bar');158 testUrl = "#foo";159 expect(sanitizeHref(testUrl)).toBe('#foo');160 testUrl = "http://foo/bar";161 expect(sanitizeHref(testUrl)).toBe('http://foo/bar');162 testUrl = " http://foo/bar";163 expect(sanitizeHref(testUrl)).toBe(' http://foo/bar');164 testUrl = "https://foo/bar";165 expect(sanitizeHref(testUrl)).toBe('https://foo/bar');166 testUrl = "ftp://foo/bar";167 expect(sanitizeHref(testUrl)).toBe('ftp://foo/bar');168 testUrl = "mailto:foo@bar.com";169 expect(sanitizeHref(testUrl)).toBe('mailto:foo@bar.com');170 testUrl = "file:///foo/bar.html";171 expect(sanitizeHref(testUrl)).toBe('file:///foo/bar.html');172 }));173 it('should allow reconfiguration of the href whitelist', function() {174 /* jshint scripturl:true */175 var returnVal;176 expect(sanitizeUriProvider.aHrefSanitizationWhitelist() instanceof RegExp).toBe(true);177 returnVal = sanitizeUriProvider.aHrefSanitizationWhitelist(/javascript:/);178 expect(returnVal).toBe(sanitizeUriProvider);179 testUrl = "javascript:doEvilStuff()";180 expect(sanitizeHref(testUrl)).toBe('javascript:doEvilStuff()');181 testUrl = "http://recon/figured";182 expect(sanitizeHref(testUrl)).toBe('unsafe:http://recon/figured');183 });184 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var testURL = wpt.testURL;3var options = {4};5testURL(url, options, function(err, data) {6 if (err) {7 console.log(err);8 } else {9 console.log(data);10 }11});12var wpt = require('webpagetest');13var testURL = wpt.testURL;14var options = {15};16testURL(url, options, function(err, data) {17 if (err) {18 console.log(err);19 } else {20 console.log(data);21 }22});23var wpt = require('webpagetest');24var testURL = wpt.testURL;25var options = {26};27testURL(url, options, function(err, data) {28 if (err) {29 console.log(err);30 } else {31 console.log(data);32 }33});34var wpt = require('webpagetest');35var testURL = wpt.testURL;36var options = {
Using AI Code Generation
1function testURL(url, expected, msg) {2 var urlObj = new URL(url);3 var actual = urlObj.href;4 assert_equals(actual, expected, msg);5}6function testURL(url, expected, msg) {7 var urlObj = new URL(url);8 var actual = urlObj.href;9 assert_equals(actual, expected, msg);10}11function testURL(url, expected, msg) {12 var urlObj = new URL(url);13 var actual = urlObj.href;14 assert_equals(actual, expected, msg);15}16function testURL(url, expected, msg) {17 var urlObj = new URL(url);18 var actual = urlObj.href;19 assert_equals(actual, expected, msg);20}21function testURL(url, expected, msg) {22 var urlObj = new URL(url);23 var actual = urlObj.href;24 assert_equals(actual, expected, msg);25}26function testURL(url, expected, msg) {27 var urlObj = new URL(url);28 var actual = urlObj.href;29 assert_equals(actual, expected, msg);30}31function testURL(url, expected, msg) {32 var urlObj = new URL(url);33 var actual = urlObj.href;34 assert_equals(actual, expected, msg);35}36function testURL(url, expected, msg) {37 var urlObj = new URL(url);38 var actual = urlObj.href;39 assert_equals(actual, expected, msg);40}41function testURL(url, expected, msg) {42 var urlObj = new URL(url);43 var actual = urlObj.href;44 assert_equals(actual, expected, msg);45}46function testURL(url, expected, msg) {47 var urlObj = new URL(url);48 var actual = urlObj.href;49 assert_equals(actual, expected, msg);50}51function testURL(url, expected, msg) {52 var urlObj = new URL(url
Using AI Code Generation
1testURL.then(function(result) {2 console.log(result);3});4 console.log(result);5});6 console.log(result);7});8 console.log(result);9});10 console.log(result);11});12 console.log(result);13});14 console.log(result);15});16 console.log(result
Using AI Code Generation
1const testURL = require('./testURL');2const expectedStatusCode = 200;3testURL(url, expectedStatusCode);4const puppeteer = require('puppeteer');5module.exports = async function testURL(url, expectedStatusCode) {6 const browser = await puppeteer.launch();7 const page = await browser.newPage();8 await page.goto(url);9 const statusCode = await page.evaluate(() => {10 return window.performance.getEntries()[0].responseEnd;11 });12 expect(statusCode).toBe(expectedStatusCode);13 await browser.close();14};
Using AI Code Generation
1function testURL(url) {2 window.external.notify("testURL:"+url);3}4function testURL(url) {5 window.external.notify("testURL:"+url);6}7function testURL(url) {8 window.external.notify("testURL:"+url);9}10function testURL(url) {11 window.external.notify("testURL:"+url);12}13function testURL(url) {14 window.external.notify("testURL:"+url);15}16function testURL(url) {17 window.external.notify("testURL:"+url);18}19function testURL(url) {20 window.external.notify("testURL:"+url);21}22function testURL(url) {23 window.external.notify("testURL:"+url);24}25function testURL(url) {26 window.external.notify("testURL:"+url);27}28function testURL(url) {29 window.external.notify("testURL:"+url);30}31function testURL(url) {32 window.external.notify("testURL:"+url);33}
Using AI Code Generation
1function testURL(url, expected) {2 var result = document.createElement("div");3 var actual = url;4 if (expected) {5 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;6 } else {7 result.innerHTML = "Actual: " + actual;8 }9 document.body.appendChild(result);10}11function testURL(url, expected) {12 var result = document.createElement("div");13 var actual = url;14 if (expected) {15 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;16 } else {17 result.innerHTML = "Actual: " + actual;18 }19 document.body.appendChild(result);20}21function testURL(url, expected) {22 var result = document.createElement("div");23 var actual = url;24 if (expected) {25 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;26 } else {27 result.innerHTML = "Actual: " + actual;28 }29 document.body.appendChild(result);30}31function testURL(url, expected) {32 var result = document.createElement("div");33 var actual = url;34 if (expected) {35 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;36 } else {37 result.innerHTML = "Actual: " + actual;38 }39 document.body.appendChild(result);40}41function testURL(url, expected) {42 var result = document.createElement("div");43 var actual = url;44 if (expected) {45 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;46 } else {47 result.innerHTML = "Actual: " + actual;48 }49 document.body.appendChild(result);50}51function testURL(url, expected) {52 var result = document.createElement("div");53 var actual = url;54 if (expected) {55 result.innerHTML = "Expecting: " + expected + " Actual: " + actual;56 } else {57 result.innerHTML = "Actual: " + actual;58 }59 document.body.appendChild(result);60}
Using AI Code Generation
1function handler(metadata, response) {2 response.setHeader("Content-Type", "text/plain");3 response.write("Hello world");4}5function handler2(metadata, response) {6 response.setHeader("Content-Type", "text/plain");7 response.write("Hello world2");8}9function handler3(metadata, response) {10 response.setHeader("Content-Type", "text/plain");11 response.write("Hello world3");12}13function handler4(metadata, response) {14 response.setHeader("Content-Type", "text/plain");15 response.write("Hello world4");16}17function handler5(metadata, response) {18 response.setHeader("Content-Type", "text/plain");19 response.write("Hello world5");20}21function handler6(metadata, response) {22 response.setHeader("Content-Type", "text/plain");23 response.write("Hello world6");24}25function handler7(metadata, response) {26 response.setHeader("Content-Type", "text/plain");27 response.write("Hello world7");28}29function handler8(metadata, response) {30 response.setHeader("Content-Type", "text/plain");31 response.write("Hello world8");32}33function handler9(metadata, response) {34 response.setHeader("Content-Type", "text/plain");35 response.write("Hello world9");36}37function handler10(metadata, response) {38 response.setHeader("Content-Type", "text/plain");39 response.write("Hello world10");40}41function handler11(metadata, response) {42 response.setHeader("Content-Type", "text/plain");43 response.write("Hello world11");44}
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!!