Best JavaScript code snippet using wpt
test.js
Source:test.js
1import test from 'ava';2import normalizeUrl from './index.js';3test('main', t => {4 t.is(normalizeUrl('sindresorhus.com'), 'http://sindresorhus.com');5 t.is(normalizeUrl('sindresorhus.com '), 'http://sindresorhus.com');6 t.is(normalizeUrl('sindresorhus.com.'), 'http://sindresorhus.com');7 t.is(normalizeUrl('SindreSorhus.com'), 'http://sindresorhus.com');8 t.is(normalizeUrl('sindresorhus.com', {defaultProtocol: 'https:'}), 'https://sindresorhus.com');9 t.is(normalizeUrl('HTTP://sindresorhus.com'), 'http://sindresorhus.com');10 t.is(normalizeUrl('//sindresorhus.com'), 'http://sindresorhus.com');11 t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');12 t.is(normalizeUrl('http://sindresorhus.com:80'), 'http://sindresorhus.com');13 t.is(normalizeUrl('https://sindresorhus.com:443'), 'https://sindresorhus.com');14 t.is(normalizeUrl('ftp://sindresorhus.com:21'), 'ftp://sindresorhus.com');15 t.is(normalizeUrl('http://www.sindresorhus.com'), 'http://sindresorhus.com');16 t.is(normalizeUrl('www.com'), 'http://www.com');17 t.is(normalizeUrl('http://www.www.sindresorhus.com'), 'http://www.www.sindresorhus.com');18 t.is(normalizeUrl('www.sindresorhus.com'), 'http://sindresorhus.com');19 t.is(normalizeUrl('http://sindresorhus.com/foo/'), 'http://sindresorhus.com/foo');20 t.is(normalizeUrl('sindresorhus.com/?foo=bar baz'), 'http://sindresorhus.com/?foo=bar+baz');21 t.is(normalizeUrl('https://foo.com/https://bar.com'), 'https://foo.com/https://bar.com');22 t.is(normalizeUrl('https://foo.com/https://bar.com/foo//bar'), 'https://foo.com/https://bar.com/foo/bar');23 t.is(normalizeUrl('https://foo.com/http://bar.com'), 'https://foo.com/http://bar.com');24 t.is(normalizeUrl('https://foo.com/http://bar.com/foo//bar'), 'https://foo.com/http://bar.com/foo/bar');25 t.is(normalizeUrl('http://sindresorhus.com/%7Efoo/'), 'http://sindresorhus.com/~foo', 'decode URI octets');26 t.is(normalizeUrl('https://foo.com/%FAIL%/07/94/ca/55.jpg'), 'https://foo.com/%FAIL%/07/94/ca/55.jpg');27 t.is(normalizeUrl('http://sindresorhus.com/?'), 'http://sindresorhus.com');28 t.is(normalizeUrl('êxample.com'), 'http://xn--xample-hva.com');29 t.is(normalizeUrl('http://sindresorhus.com/?b=bar&a=foo'), 'http://sindresorhus.com/?a=foo&b=bar');30 t.is(normalizeUrl('http://sindresorhus.com/?foo=bar*|<>:"'), 'http://sindresorhus.com/?foo=bar*|%3C%3E:%22');31 t.is(normalizeUrl('http://sindresorhus.com:5000'), 'http://sindresorhus.com:5000');32 t.is(normalizeUrl('//sindresorhus.com/', {normalizeProtocol: false}), '//sindresorhus.com');33 t.is(normalizeUrl('//sindresorhus.com:80/', {normalizeProtocol: false}), '//sindresorhus.com');34 t.is(normalizeUrl('http://sindresorhus.com/foo#bar'), 'http://sindresorhus.com/foo#bar');35 t.is(normalizeUrl('http://sindresorhus.com/foo#bar', {stripHash: true}), 'http://sindresorhus.com/foo');36 t.is(normalizeUrl('http://sindresorhus.com/foo#bar:~:text=hello%20world', {stripHash: true}), 'http://sindresorhus.com/foo');37 t.is(normalizeUrl('http://sindresorhus.com/foo/bar/../baz'), 'http://sindresorhus.com/foo/baz');38 t.is(normalizeUrl('http://sindresorhus.com/foo/bar/./baz'), 'http://sindresorhus.com/foo/bar/baz');39 t.is(normalizeUrl('sindre://www.sorhus.com'), 'sindre://sorhus.com');40 t.is(normalizeUrl('sindre://www.sorhus.com/'), 'sindre://sorhus.com');41 t.is(normalizeUrl('sindre://www.sorhus.com/foo/bar'), 'sindre://sorhus.com/foo/bar');42 t.is(normalizeUrl('https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png'), 'https://i.vimeocdn.com/filter/overlay?src0=https://i.vimeocdn.com/video/598160082_1280x720.jpg&src1=https://f.vimeocdn.com/images_v6/share/play_icon_overlay.png');43});44test('stripAuthentication option', t => {45 t.is(normalizeUrl('http://user:password@www.sindresorhus.com'), 'http://sindresorhus.com');46 t.is(normalizeUrl('https://user:password@www.sindresorhus.com'), 'https://sindresorhus.com');47 t.is(normalizeUrl('https://user:password@www.sindresorhus.com/@user'), 'https://sindresorhus.com/@user');48 t.is(normalizeUrl('user:password@sindresorhus.com'), 'http://sindresorhus.com');49 t.is(normalizeUrl('http://user:password@www.êxample.com'), 'http://xn--xample-hva.com');50 t.is(normalizeUrl('sindre://user:password@www.sorhus.com'), 'sindre://sorhus.com');51 const options = {stripAuthentication: false};52 t.is(normalizeUrl('http://user:password@www.sindresorhus.com', options), 'http://user:password@sindresorhus.com');53 t.is(normalizeUrl('https://user:password@www.sindresorhus.com', options), 'https://user:password@sindresorhus.com');54 t.is(normalizeUrl('https://user:password@www.sindresorhus.com/@user', options), 'https://user:password@sindresorhus.com/@user');55 t.is(normalizeUrl('user:password@sindresorhus.com', options), 'http://user:password@sindresorhus.com');56 t.is(normalizeUrl('http://user:password@www.êxample.com', options), 'http://user:password@xn--xample-hva.com');57 t.is(normalizeUrl('sindre://user:password@www.sorhus.com', options), 'sindre://user:password@sorhus.com');58});59test('stripProtocol option', t => {60 const options = {stripProtocol: true};61 t.is(normalizeUrl('http://www.sindresorhus.com', options), 'sindresorhus.com');62 t.is(normalizeUrl('http://sindresorhus.com', options), 'sindresorhus.com');63 t.is(normalizeUrl('https://www.sindresorhus.com', options), 'sindresorhus.com');64 t.is(normalizeUrl('//www.sindresorhus.com', options), 'sindresorhus.com');65 t.is(normalizeUrl('sindre://user:password@www.sorhus.com', options), 'sindre://sorhus.com');66 t.is(normalizeUrl('sindre://www.sorhus.com', options), 'sindre://sorhus.com');67});68test('stripTextFragment option', t => {69 t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');70 t.is(normalizeUrl('http://sindresorhus.com/about#'), 'http://sindresorhus.com/about');71 t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello'), 'http://sindresorhus.com/about');72 t.is(normalizeUrl('http://sindresorhus.com/about#main'), 'http://sindresorhus.com/about#main');73 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello'), 'http://sindresorhus.com/about#main');74 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world'), 'http://sindresorhus.com/about#main');75 const options = {stripTextFragment: false};76 t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');77 t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello', options), 'http://sindresorhus.com/about#:~:text=hello');78 t.is(normalizeUrl('http://sindresorhus.com/about#main', options), 'http://sindresorhus.com/about#main');79 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello', options), 'http://sindresorhus.com/about#main:~:text=hello');80 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world', options), 'http://sindresorhus.com/about#main:~:text=hello%20world');81 const options2 = {stripHash: true, stripTextFragment: false};82 t.is(normalizeUrl('http://sindresorhus.com', options2), 'http://sindresorhus.com');83 t.is(normalizeUrl('http://sindresorhus.com/about#:~:text=hello', options2), 'http://sindresorhus.com/about');84 t.is(normalizeUrl('http://sindresorhus.com/about#main', options2), 'http://sindresorhus.com/about');85 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello', options2), 'http://sindresorhus.com/about');86 t.is(normalizeUrl('http://sindresorhus.com/about#main:~:text=hello%20world', options2), 'http://sindresorhus.com/about');87});88test('stripWWW option', t => {89 const options = {stripWWW: false};90 t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');91 t.is(normalizeUrl('www.sindresorhus.com', options), 'http://www.sindresorhus.com');92 t.is(normalizeUrl('http://www.êxample.com', options), 'http://www.xn--xample-hva.com');93 t.is(normalizeUrl('sindre://www.sorhus.com', options), 'sindre://www.sorhus.com');94 const options2 = {stripWWW: true};95 t.is(normalizeUrl('http://www.vue.amsterdam', options2), 'http://vue.amsterdam');96 t.is(normalizeUrl('http://www.sorhus.xx--bck1b9a5dre4c', options2), 'http://sorhus.xx--bck1b9a5dre4c');97 const tooLongTLDURL = 'http://www.sorhus.' + ''.padEnd(64, 'a');98 t.is(normalizeUrl(tooLongTLDURL, options2), tooLongTLDURL);99});100test('removeQueryParameters option', t => {101 const options = {102 stripWWW: false,103 removeQueryParameters: [/^utm_\w+/i, 'ref'],104 };105 t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test'), 'http://sindresorhus.com/?foo=bar');106 t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');107 t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com/?foo=bar');108 t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar');109});110test('removeQueryParameters boolean `true` option', t => {111 const options = {112 stripWWW: false,113 removeQueryParameters: true,114 };115 t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');116 t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com');117 t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com');118});119test('removeQueryParameters boolean `false` option', t => {120 const options = {121 stripWWW: false,122 removeQueryParameters: false,123 };124 t.is(normalizeUrl('http://www.sindresorhus.com', options), 'http://www.sindresorhus.com');125 t.is(normalizeUrl('www.sindresorhus.com?foo=bar', options), 'http://www.sindresorhus.com/?foo=bar');126 t.is(normalizeUrl('www.sindresorhus.com?foo=bar&utm_medium=test&ref=test_ref', options), 'http://www.sindresorhus.com/?foo=bar&ref=test_ref&utm_medium=test');127});128test('forceHttp option', t => {129 const options = {forceHttp: true};130 t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');131 t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');132 t.is(normalizeUrl('https://www.sindresorhus.com', options), 'http://sindresorhus.com');133 t.is(normalizeUrl('//sindresorhus.com', options), 'http://sindresorhus.com');134});135test('forceHttp option with forceHttps', t => {136 t.throws(() => {137 normalizeUrl('https://www.sindresorhus.com', {forceHttp: true, forceHttps: true});138 }, {139 message: 'The `forceHttp` and `forceHttps` options cannot be used together',140 });141});142test('forceHttps option', t => {143 const options = {forceHttps: true};144 t.is(normalizeUrl('https://sindresorhus.com'), 'https://sindresorhus.com');145 t.is(normalizeUrl('http://sindresorhus.com', options), 'https://sindresorhus.com');146 t.is(normalizeUrl('https://www.sindresorhus.com', options), 'https://sindresorhus.com');147 t.is(normalizeUrl('//sindresorhus.com', options), 'https://sindresorhus.com');148});149test('removeTrailingSlash option', t => {150 const options = {removeTrailingSlash: false};151 t.is(normalizeUrl('http://sindresorhus.com'), 'http://sindresorhus.com');152 t.is(normalizeUrl('http://sindresorhus.com/'), 'http://sindresorhus.com');153 t.is(normalizeUrl('http://sindresorhus.com', options), 'http://sindresorhus.com');154 t.is(normalizeUrl('http://sindresorhus.com/', options), 'http://sindresorhus.com');155 t.is(normalizeUrl('http://sindresorhus.com/redirect'), 'http://sindresorhus.com/redirect');156 t.is(normalizeUrl('http://sindresorhus.com/redirect/'), 'http://sindresorhus.com/redirect');157 t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');158 t.is(normalizeUrl('http://sindresorhus.com/redirect/', options), 'http://sindresorhus.com/redirect/');159 t.is(normalizeUrl('http://sindresorhus.com/#/'), 'http://sindresorhus.com/#/');160 t.is(normalizeUrl('http://sindresorhus.com/#/', options), 'http://sindresorhus.com/#/');161 t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true'), 'http://sindresorhus.com/?unicorns=true');162 t.is(normalizeUrl('http://sindresorhus.com/?unicorns=true', options), 'http://sindresorhus.com/?unicorns=true');163});164test('removeSingleSlash option', t => {165 const options = {removeSingleSlash: false};166 t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');167 t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');168 t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');169 t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect');170 t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');171 t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');172});173test('removeSingleSlash option combined with removeTrailingSlash option', t => {174 const options = {removeTrailingSlash: false, removeSingleSlash: false};175 t.is(normalizeUrl('https://sindresorhus.com', options), 'https://sindresorhus.com');176 t.is(normalizeUrl('https://sindresorhus.com/', options), 'https://sindresorhus.com/');177 t.is(normalizeUrl('https://sindresorhus.com/redirect', options), 'https://sindresorhus.com/redirect');178 t.is(normalizeUrl('https://sindresorhus.com/redirect/', options), 'https://sindresorhus.com/redirect/');179 t.is(normalizeUrl('https://sindresorhus.com/#/', options), 'https://sindresorhus.com/#/');180 t.is(normalizeUrl('https://sindresorhus.com/?unicorns=true', options), 'https://sindresorhus.com/?unicorns=true');181});182test('removeDirectoryIndex option', t => {183 const options1 = {removeDirectoryIndex: ['index.html', 'index.php']};184 t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');185 t.is(normalizeUrl('http://sindresorhus.com/index.html', options1), 'http://sindresorhus.com');186 t.is(normalizeUrl('http://sindresorhus.com/index.htm', options1), 'http://sindresorhus.com/index.htm');187 t.is(normalizeUrl('http://sindresorhus.com/index.php', options1), 'http://sindresorhus.com');188 t.is(normalizeUrl('http://sindresorhus.com/path/index.html'), 'http://sindresorhus.com/path/index.html');189 t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options1), 'http://sindresorhus.com/path');190 t.is(normalizeUrl('http://sindresorhus.com/path/index.htm', options1), 'http://sindresorhus.com/path/index.htm');191 t.is(normalizeUrl('http://sindresorhus.com/path/index.php', options1), 'http://sindresorhus.com/path');192 t.is(normalizeUrl('http://sindresorhus.com/foo/bar/index.html', options1), 'http://sindresorhus.com/foo/bar');193 const options2 = {removeDirectoryIndex: [/^index\.[a-z]+$/, 'remove.html']};194 t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');195 t.is(normalizeUrl('http://sindresorhus.com/index.html', options2), 'http://sindresorhus.com');196 t.is(normalizeUrl('http://sindresorhus.com/index/index.html', options2), 'http://sindresorhus.com/index');197 t.is(normalizeUrl('http://sindresorhus.com/remove.html', options2), 'http://sindresorhus.com');198 t.is(normalizeUrl('http://sindresorhus.com/default.htm', options2), 'http://sindresorhus.com/default.htm');199 t.is(normalizeUrl('http://sindresorhus.com/index.php', options2), 'http://sindresorhus.com');200 const options3 = {removeDirectoryIndex: true};201 t.is(normalizeUrl('http://sindresorhus.com/index.html'), 'http://sindresorhus.com/index.html');202 t.is(normalizeUrl('http://sindresorhus.com/index.html', options3), 'http://sindresorhus.com');203 t.is(normalizeUrl('http://sindresorhus.com/index.htm', options3), 'http://sindresorhus.com');204 t.is(normalizeUrl('http://sindresorhus.com/index.php', options3), 'http://sindresorhus.com');205});206test('removeTrailingSlash and removeDirectoryIndex options)', t => {207 const options1 = {208 removeTrailingSlash: true,209 removeDirectoryIndex: true,210 };211 t.is(normalizeUrl('http://sindresorhus.com/path/', options1), 'http://sindresorhus.com/path');212 t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options1), 'http://sindresorhus.com/path');213 t.is(normalizeUrl('http://sindresorhus.com/#/path/', options1), 'http://sindresorhus.com/#/path/');214 t.is(normalizeUrl('http://sindresorhus.com/foo/#/bar/', options1), 'http://sindresorhus.com/foo#/bar/');215 const options2 = {216 removeTrailingSlash: false,217 removeDirectoryIndex: true,218 };219 t.is(normalizeUrl('http://sindresorhus.com/path/', options2), 'http://sindresorhus.com/path/');220 t.is(normalizeUrl('http://sindresorhus.com/path/index.html', options2), 'http://sindresorhus.com/path/');221 t.is(normalizeUrl('http://sindresorhus.com/#/path/', options2), 'http://sindresorhus.com/#/path/');222});223test('sortQueryParameters option', t => {224 const options1 = {225 sortQueryParameters: true,226 };227 t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');228 t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');229 t.is(normalizeUrl('http://sindresorhus.com/?a=Z&d=W&b=Y&c=X', options1), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');230 t.is(normalizeUrl('http://sindresorhus.com/', options1), 'http://sindresorhus.com');231 const options2 = {232 sortQueryParameters: false,233 };234 t.is(normalizeUrl('http://sindresorhus.com/?a=Z&b=Y&c=X&d=W', options2), 'http://sindresorhus.com/?a=Z&b=Y&c=X&d=W');235 t.is(normalizeUrl('http://sindresorhus.com/?b=Y&c=X&a=Z&d=W', options2), 'http://sindresorhus.com/?b=Y&c=X&a=Z&d=W');236 t.is(normalizeUrl('http://sindresorhus.com/?a=Z&d=W&b=Y&c=X', options2), 'http://sindresorhus.com/?a=Z&d=W&b=Y&c=X');237 t.is(normalizeUrl('http://sindresorhus.com/', options2), 'http://sindresorhus.com');238 t.is(normalizeUrl('http://sindresorhus.com/?a=/path', options1), normalizeUrl('http://sindresorhus.com/?a=/path', options2));239});240test('invalid urls', t => {241 t.throws(() => {242 normalizeUrl('http://');243 }, {244 message: /^Invalid URL/,245 });246 t.throws(() => {247 normalizeUrl('/');248 }, {249 message: /^Invalid URL/,250 });251 t.throws(() => {252 normalizeUrl('/relative/path/');253 }, {254 message: /^Invalid URL/,255 });256});257test('remove duplicate pathname slashes', t => {258 t.is(normalizeUrl('http://sindresorhus.com////foo/bar'), 'http://sindresorhus.com/foo/bar');259 t.is(normalizeUrl('http://sindresorhus.com////foo////bar'), 'http://sindresorhus.com/foo/bar');260 t.is(normalizeUrl('//sindresorhus.com//foo', {normalizeProtocol: false}), '//sindresorhus.com/foo');261 t.is(normalizeUrl('http://sindresorhus.com:5000///foo'), 'http://sindresorhus.com:5000/foo');262 t.is(normalizeUrl('http://sindresorhus.com///foo'), 'http://sindresorhus.com/foo');263 t.is(normalizeUrl('http://sindresorhus.com:5000//foo'), 'http://sindresorhus.com:5000/foo');264 t.is(normalizeUrl('http://sindresorhus.com//foo'), 'http://sindresorhus.com/foo');265 t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com'), 'http://sindresorhus.com/s3://sindresorhus.com');266 t.is(normalizeUrl('http://sindresorhus.com/s3://sindresorhus.com//foo'), 'http://sindresorhus.com/s3://sindresorhus.com/foo');267 t.is(normalizeUrl('http://sindresorhus.com//foo/s3://sindresorhus.com'), 'http://sindresorhus.com/foo/s3://sindresorhus.com');268 t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com'), 'http://sindresorhus.com/git://sindresorhus.com');269 t.is(normalizeUrl('http://sindresorhus.com/git://sindresorhus.com//foo'), 'http://sindresorhus.com/git://sindresorhus.com/foo');270 t.is(normalizeUrl('http://sindresorhus.com//foo/git://sindresorhus.com//foo'), 'http://sindresorhus.com/foo/git://sindresorhus.com/foo');271 t.is(normalizeUrl('http://sindresorhus.com/a://sindresorhus.com//foo'), 'http://sindresorhus.com/a:/sindresorhus.com/foo');272 t.is(normalizeUrl('http://sindresorhus.com/alongprotocolwithin50charlimitxxxxxxxxxxxxxxxxxxxx://sindresorhus.com//foo'), 'http://sindresorhus.com/alongprotocolwithin50charlimitxxxxxxxxxxxxxxxxxxxx://sindresorhus.com/foo');273 t.is(normalizeUrl('http://sindresorhus.com/alongprotocolexceeds50charlimitxxxxxxxxxxxxxxxxxxxxx://sindresorhus.com//foo'), 'http://sindresorhus.com/alongprotocolexceeds50charlimitxxxxxxxxxxxxxxxxxxxxx:/sindresorhus.com/foo');274 t.is(normalizeUrl('http://sindresorhus.com/a2-.+://sindresorhus.com'), 'http://sindresorhus.com/a2-.+://sindresorhus.com');275 t.is(normalizeUrl('http://sindresorhus.com/a2-.+_://sindresorhus.com'), 'http://sindresorhus.com/a2-.+_:/sindresorhus.com');276 t.is(normalizeUrl('http://sindresorhus.com/2abc://sindresorhus.com'), 'http://sindresorhus.com/2abc:/sindresorhus.com');277});278test('data URL', t => {279 // Invalid URL.280 t.throws(() => {281 normalizeUrl('data:');282 }, {283 message: 'Invalid URL: data:',284 });285 // Strip default MIME type286 t.is(normalizeUrl('data:text/plain,foo'), 'data:,foo');287 // Strip default charset288 t.is(normalizeUrl('data:;charset=us-ascii,foo'), 'data:,foo');289 // Normalize away trailing semicolon.290 t.is(normalizeUrl('data:;charset=UTF-8;,foo'), 'data:;charset=utf-8,foo');291 // Empty MIME type.292 t.is(normalizeUrl('data:,'), 'data:,');293 // Empty MIME type with charset.294 t.is(normalizeUrl('data:;charset=utf-8,foo'), 'data:;charset=utf-8,foo');295 // Lowercase the MIME type.296 t.is(normalizeUrl('data:TEXT/HTML,foo'), 'data:text/html,foo');297 // Strip empty hash.298 t.is(normalizeUrl('data:,foo# '), 'data:,foo');299 // Key only mediaType attribute.300 t.is(normalizeUrl('data:;foo=;bar,'), 'data:;foo;bar,');301 // Lowercase the charset.302 t.is(normalizeUrl('data:;charset=UTF-8,foo'), 'data:;charset=utf-8,foo');303 // Remove spaces after the comma when it's base64.304 t.is(normalizeUrl('data:;base64, Zm9v #foo #bar'), 'data:;base64,Zm9v#foo #bar');305 // Keep spaces when it's not base64.306 t.is(normalizeUrl('data:, foo #bar'), 'data:, foo #bar');307 // Options.308 const options = {309 defaultProtocol: 'http:',310 normalizeProtocol: true,311 forceHttp: true,312 stripHash: true,313 stripWWW: true,314 stripProtocol: true,315 removeQueryParameters: [/^utm_\w+/i, 'ref'],316 sortQueryParameters: true,317 removeTrailingSlash: true,318 removeDirectoryIndex: true,319 };320 t.is(normalizeUrl('data:,sindresorhus.com/', options), 'data:,sindresorhus.com/');321 t.is(normalizeUrl('data:,sindresorhus.com/index.html', options), 'data:,sindresorhus.com/index.html');322 t.is(normalizeUrl('data:,sindresorhus.com?foo=bar&a=a&utm_medium=test', options), 'data:,sindresorhus.com?foo=bar&a=a&utm_medium=test');323 t.is(normalizeUrl('data:,foo#bar', options), 'data:,foo');324 t.is(normalizeUrl('data:,www.sindresorhus.com', options), 'data:,www.sindresorhus.com');325});326test('prevents homograph attack', t => {327 // The input string uses Unicode to make it look like a valid `ebay.com` URL.328 t.is(normalizeUrl('https://ebаy.com'), 'https://xn--eby-7cd.com');329});330test('view-source URL', t => {331 t.throws(() => {332 normalizeUrl('view-source:https://www.sindresorhus.com');333 }, {334 message: '`view-source:` is not supported as it is a non-standard protocol',335 });336});337test('does not have exponential performance for data URLs', t => {338 for (let index = 0; index < 1000; index += 50) {339 const url = 'data:' + Array.from({length: index}).fill(',#').join('') + '\ra';340 const start = Date.now();341 try {342 normalizeUrl(url);343 } catch {}344 const difference = Date.now() - start;345 t.true(difference < 100, `Execution time: ${difference}`);346 }...
normalize-url.test.js
Source:normalize-url.test.js
1import normalizeUrl from '../src/normalize-url';2describe('serialize-url', () => {3 describe('common use cases', () => {4 it('handle empty url', () => {5 expect(normalizeUrl().toString()).toBe('http://localhost/');6 expect(normalizeUrl(null).toString()).toBe('http://localhost/');7 expect(normalizeUrl('').toString()).toBe('http://localhost/');8 });9 it('any url', () => {10 const urlString = 'http://my-host.com/abc?a=b#foo';11 expect(normalizeUrl(urlString).toString()).toBe(urlString);12 });13 });14 describe('request object', () => {15 it('protocol', () => {16 const params = { protocol: 'http', host: 'my-host' };17 expect(normalizeUrl(params).toString()).toBe('http://my-host/');18 });19 it('protocol + slashes', () => {20 const params = { protocol: 'http', host: 'my-host', slashes: true };21 expect(normalizeUrl(params).toString()).toBe('http://my-host/');22 });23 it('auth', () => {24 const params = { auth: 'user:pass', host: 'myhost' };25 expect(normalizeUrl(params).toString()).toBe('http://user:pass@myhost/');26 });27 it('hostname + port', () => {28 const params = { hostname: 'myhost', port: 81 };29 expect(normalizeUrl(params).toString()).toBe('http://myhost:81/');30 });31 it('hostname + port :80', () => {32 const params = { hostname: 'myhost', port: 80 };33 expect(normalizeUrl(params).toString()).toBe('http://myhost/');34 });35 it('hostname', () => {36 const params = { hostname: 'myhost' };37 expect(normalizeUrl(params).toString()).toBe('http://myhost/');38 });39 it('host', () => {40 const params = { host: 'my-host', hostname: 'myhost', port: 81 };41 expect(normalizeUrl(params).toString()).toBe('http://my-host/');42 });43 it('host + slashes', () => {44 const params = { host: 'my-host', slashes: true };45 expect(normalizeUrl(params).toString()).toBe('http://my-host/');46 });47 it('pathname', () => {48 const params = { pathname: 'abc' };49 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc');50 });51 it('pathname + slashes', () => {52 const params = { pathname: 'abc', slashes: true };53 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc');54 });55 it('search a=b', () => {56 const params = { pathname: 'abc', search: 'a=b' };57 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc?a=b');58 });59 it('search ?a=b', () => {60 const params = { pathname: 'abc', search: '?a=b' };61 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc?a=b');62 });63 it('query {a: b}', () => {64 const params = { pathname: 'abc', query: { a: 'b' } };65 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc?a=b');66 });67 it('hash', () => {68 const params = { pathname: 'abc', hash: 'foo' };69 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc#foo');70 });71 it('hash #', () => {72 const params = { pathname: 'abc', hash: '#foo' };73 expect(normalizeUrl(params).toString()).toBe('http://localhost/abc#foo');74 });75 });76 describe('whatwg url', () => {77 it('should match', () => {78 const urlString = 'http://my-host.com/abc?a=b#foo';79 expect(normalizeUrl(new URL(urlString)).toString()).toBe(urlString);80 });81 });...
4.dependent.normalizeUrl.js
Source:4.dependent.normalizeUrl.js
1"use strict";2var normalizeUrl = require("../lib/normalizeUrl");3var parseUrl = require("../lib/parseUrl");4var UrlType = require("../lib/UrlType");5var expect = require("chai").expect;6var urllib = require("url");7function urlTest1(obj)8{9 expect(obj.protocol).to.equal("http:");10 expect(obj.slashes).to.be.true;11 expect(obj.auth).to.equal("user:pass");12 expect(obj.host).to.equal("fakeurl.com");13 expect(obj.port).to.be.null;14 expect(obj.hostname).to.equal("fakeurl.com");15 expect(obj.hash).to.equal("#hash");16 expect(obj.search).to.equal("?query=1&query=2&var2=");17 expect(obj.query).to.equal("query=1&query=2&var2=");18 expect(obj.pathname).to.equal("/dir/filename.html");19 expect(obj.path).to.equal("/dir/filename.html?query=1&query=2&var2=");20 expect(obj.href).to.equal("http://user:pass@fakeurl.com/dir/filename.html?query=1&query=2&var2=#hash");21 expect(obj.extra.protocolTruncated).to.equal("http");22 expect(obj.extra.portIsDefault).to.be.true;23 expect(obj.extra.directory).to.deep.equal([ "dir" ]);24 expect(obj.extra.directoryLeadingSlash).to.be.true;25 expect(obj.extra.filename).to.equal("filename.html");26 expect(obj.extra.filenameIsIndex).to.be.false;27 expect(obj.extra.query).to.deep.equal({ query:["1","2"], var2:"" });28 expect(obj.extra.type).to.equal(UrlType.ABSOLUTE);29}30function urlTest2(obj)31{32 expect(obj.protocol).to.equal("http:");33 expect(obj.slashes).to.be.true;34 expect(obj.auth).to.equal("user:pass");35 expect(obj.host).to.equal("fakeurl.com");36 expect(obj.port).to.be.null;37 expect(obj.hostname).to.equal("fakeurl.com");38 expect(obj.hash).to.equal("#hash");39 expect(obj.search).to.be.null;40 expect(obj.query).to.be.null;41 expect(obj.pathname).to.equal("/dir/filename.html");42 expect(obj.path).to.equal("/dir/filename.html");43 expect(obj.href).to.equal("http://user:pass@fakeurl.com/dir/filename.html#hash");44 expect(obj.extra.protocolTruncated).to.equal("http");45 expect(obj.extra.portIsDefault).to.be.true;46 expect(obj.extra.directory).to.deep.equal([ "dir" ]);47 expect(obj.extra.directoryLeadingSlash).to.be.true;48 expect(obj.extra.filename).to.equal("filename.html");49 expect(obj.extra.filenameIsIndex).to.be.false;50 expect(obj.extra.query).to.deep.equal({});51 expect(obj.extra.type).to.equal(UrlType.ABSOLUTE);52}53describe("normalizeUrl", function()54{55 it("should work", function(done)56 {57 var urlString;58 59 urlString = "http://user:pass@fakeurl.com:80/dir/to/../filename.html?query=1&query=2&var2=#hash";60 urlTest1( normalizeUrl( urlString ) );61 urlTest1( normalizeUrl( urllib.parse(urlString) ) );62 urlTest1( normalizeUrl( parseUrl(urlString) ) );63 64 urlString = "http://user:pass@fakeurl.com:80/dir/filename.html?query=1&query=2&var2=#hash";65 urlTest1( normalizeUrl( urlString ) );66 urlTest1( normalizeUrl( urllib.parse(urlString) ) );67 urlTest1( normalizeUrl( parseUrl(urlString) ) );68 69 urlString = "http://user:pass@fakeurl.com/dir/filename.html?#hash";70 urlTest2( normalizeUrl( urlString ) );71 urlTest2( normalizeUrl( urllib.parse(urlString) ) );72 urlTest2( normalizeUrl( parseUrl(urlString) ) );73 74 done();75 });...
urls.js
Source:urls.js
1const {assert} = require('./assert');2const {normalizeUrl} = require('../../../../src/lib/urls');3describe('urls', function () {4 describe('normalizeUrl', function () {5 it('should ignore empty searches', function () {6 assert(normalizeUrl('/foo?') === '/foo/', 'search should be ignored');7 });8 it('should ignore searches if they have an index.html', function () {9 assert(10 normalizeUrl('/foo?bar/index.html') === '/foo/?bar/index.html',11 'search should be ignored if it has index.html',12 );13 });14 it('should add a trailing slash if one is missing', function () {15 assert(normalizeUrl('/foo') === '/foo/', 'requires trailing slash');16 });17 it('should remove index.html', function () {18 assert(19 normalizeUrl('/zing/test/index.html') === '/zing/test/',20 'removes index.html',21 );22 });23 it('should ignore non-index.html pages', function () {24 assert(25 normalizeUrl('/test/hello.html') === '/test/hello.html',26 'ignores non-index.html HTML pages',27 );28 });29 it('should remove extra slashes', function () {30 assert(normalizeUrl('///foo') === '/foo/', 'removes extra slashes');31 assert(32 normalizeUrl('/foo///bar//') === '/foo/bar/',33 'removes extra slashes',34 );35 });36 it('should do nothing to valid URLs', function () {37 assert(38 normalizeUrl('/foo/?query=123&other') === '/foo/?query=123&other',39 'retains query string',40 );41 assert(normalizeUrl('/foo/') === '/foo/', 'does nothing');42 assert(43 normalizeUrl('/foo/page.html') === '/foo/page.html',44 'does nothing to non-index.html',45 );46 assert(normalizeUrl('/') === '/', 'does nothing');47 assert(48 normalizeUrl('/foo/bar/hello/') === '/foo/bar/hello/',49 'does nothing to long path',50 );51 });52 });...
wk-plugin.js
Source:wk-plugin.js
...4 return;5 }6 // Initialize Ionic7 window.Ionic = window.Ionic || {};8 function normalizeURL(url) {9 if (!url) {10 return url;11 }12 if (!url.startsWith("file://")) {13 return url;14 }15 url = url.substr(7); // len("file://") == 716 if (url.length == 0 || url[0] !== '/') { // ensure the new URL starts with /17 url = '/' + url;18 }19 return 'http://localhost:8080' + url;20 }21 if (typeof window.wkRewriteURL === 'undefined') {22 window.wkRewriteURL = function (url) {23 console.warn('wkRewriteURL is deprecated, use normalizeURL instead');24 return normalizeURL(url);25 }26 }27 window.Ionic.normalizeURL = normalizeURL;28 var xhrPrototype = window.XMLHttpRequest.prototype;29 var originalOpen = xhrPrototype.open;30 xhrPrototype.open = function _wk_open(method, url) {31 arguments[1] = normalizeURL(url);32 originalOpen.apply(this, arguments);33 }34 console.debug("XHR polyfill injected!");35 var stopScrollHandler = window.webkit.messageHandlers.stopScroll;36 if (!stopScrollHandler) {37 console.error('Can not find stopScroll handler');38 return;39 }40 var stopScrollFunc = null;41 var stopScroll = {42 stop: function stop(callback) {43 if (!stopScrollFunc) {44 stopScrollFunc = callback;45 stopScrollHandler.postMessage('');...
helper-url-test.js
Source:helper-url-test.js
...3import { normalizeURL, cleanURL } from 'ember-websockets/helpers';4module('Unit | Helpers | Helper URL Functions', function(hooks) {5 setupTest(hooks);6 test('that normalizeURL works correctly', function(assert) {7 assert.equal(normalizeURL('ws://localhost:8000'), 'ws://localhost:8000/');8 assert.equal(normalizeURL('ws://localhost:8000/'), 'ws://localhost:8000/');9 assert.equal(normalizeURL('ws://example.com'), 'ws://example.com/');10 assert.equal(normalizeURL('ws://example.com/foo'), 'ws://example.com/foo');11 assert.equal(normalizeURL('ws://example.com/foo/'), 'ws://example.com/foo/');12 });13 test('that normalizeURL works correctly if url contains query params', function(assert) {14 assert.equal(normalizeURL('ws://example.com/?param=value'), 'ws://example.com/?param=value');15 assert.equal(normalizeURL('ws://example.com?param=value'), 'ws://example.com/?param=value');16 assert.equal(normalizeURL('ws://example.com:8000/?param=value'), 'ws://example.com:8000/?param=value');17 assert.equal(normalizeURL('ws://example.com:8000?param=value'), 'ws://example.com:8000/?param=value');18 assert.equal(normalizeURL('ws://example.com:8000/foo?param=value'), 'ws://example.com:8000/foo?param=value');19 assert.equal(normalizeURL('ws://example.com:8000/foo/?param=value'), 'ws://example.com:8000/foo/?param=value');20 });21 test('that cleanURL works correctly', function(assert) {22 assert.equal(cleanURL('ws://example.com'), 'ws://examplecom');23 assert.equal(cleanURL('ws://example.com?param=foo.bar'), 'ws://examplecom?param=foobar');24 });...
url-spec.js
Source:url-spec.js
1'use strict';2var normalizeUrl= require('../index.js');3describe('normalizeUrl', function(){4 it('returns false if not given a string', function(){5 var url1 = normalizeUrl([1,2,3]);6 var url2 = normalizeUrl({url: 'http://google.com/'});7 var url3 = normalizeUrl(372785);8 expect(url1).toBe(false);9 expect(url2).toBe(false);10 expect(url3).toBe(false);11 });12 it('will normalize the url', function(){13 var url1 = normalizeUrl('http://google.com/');14 var url2 = normalizeUrl('http://google.com');15 var url3 = normalizeUrl('https://google.com/');16 var url4 = normalizeUrl('google.com');17 var url5 = normalizeUrl('www.google.com');18 var url6 = normalizeUrl('google.com/some/path/index.js');19 expect(url1).toEqual('http://google.com/');20 expect(url2).toEqual('http://google.com/');21 expect(url3).toEqual('https://google.com/');22 expect(url4).toEqual('http://google.com/');23 expect(url5).toEqual('http://www.google.com/');24 expect(url6).toEqual('http://google.com/some/path/index.js');25 });...
normalizeUrl.js
Source:normalizeUrl.js
1'use strict'2const test = require('ava')3const uuid = require('uuid').v44const normalizeUrl = require('../../src/utils/normalizeUrl')5test('remove directory index', (t) => {6 const url = 'https://example.com/index.html'7 const result = normalizeUrl(url)8 t.is(result, 'https://example.com')9})10test('remove ref query parameter', (t) => {11 const url = `https://example.com/?ref=${ uuid() }`12 const result = normalizeUrl(url)13 t.is(result, 'https://example.com')14})15test('remove fbclid query parameter', (t) => {16 const url = `https://example.com/?fbclid=${ uuid() }`17 const result = normalizeUrl(url)18 t.is(result, 'https://example.com')19})20test('remove source query parameter', (t) => {21 const url = `https://example.com/?source=${ uuid() }`22 const result = normalizeUrl(url)23 t.is(result, 'https://example.com')...
Using AI Code Generation
1var wpt = require('wpt');2wpt.normalizeURL(url, function(err, data) {3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10wpt.getLocations(function(err, data) {11 if (err) {12 console.log('Error: ' + err);13 } else {14 console.log(data);15 }16});17var wpt = require('wpt');18wpt.getTesters(function(err, data) {19 if (err) {20 console.log('Error: ' + err);21 } else {22 console.log(data);23 }24});25var wpt = require('wpt');26var testId = "130820_6C_5c5";27wpt.getTest(testId, function(err, data) {28 if (err) {29 console.log('Error: ' + err);30 } else {31 console.log(data);32 }33});34var wpt = require('wpt');35var testId = "130820_6C_5c5";36wpt.getTestStatus(testId, function(err, data) {37 if (err) {38 console.log('Error: ' + err);39 } else {40 console.log(data);41 }42});43var wpt = require('wpt');44var testId = "130820_6C_5c5";45wpt.getTestResults(testId, function(err, data) {46 if (err) {47 console.log('Error: ' + err);48 } else {49 console.log(data);50 }51});
Using AI Code Generation
1var wpt = require('wpt-api');2var wpt = new wpt('API_KEY');3wpt.normalizeURL(url, function(err, data) {4 console.log('Normalized URL: ' + data);5});6MIT © [WebPageTest](
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!!