1'use strict'; 2 3// Tests below are not from WPT. 4 5const common = require('../common'); 6if (!common.hasIntl) { 7 // A handful of the tests fail when ICU is not included. 8 common.skip('missing Intl'); 9} 10 11const assert = require('assert'); 12const fixtures = require('../common/fixtures'); 13 14const tests = require( 15 fixtures.path('wpt', 'url', 'resources', 'urltestdata.json') 16); 17 18const originalFailures = tests.filter((test) => test.failure); 19 20const typeFailures = [ 21 { input: '' }, 22 { input: 'test' }, 23 { input: undefined }, 24 { input: 0 }, 25 { input: true }, 26 { input: false }, 27 { input: null }, 28 { input: new Date() }, 29 { input: new RegExp() }, 30 { input: 'test', base: null }, 31 { input: 'http://nodejs.org', base: null }, 32 { input: () => {} }, 33]; 34 35// See https://github.com/w3c/web-platform-tests/pull/10955 36// > If `failure` is true, parsing `about:blank` against `base` 37// > must give failure. This tests that the logic for converting 38// > base URLs into strings properly fails the whole parsing 39// > algorithm if the base URL cannot be parsed. 40const aboutBlankFailures = originalFailures 41 .map((test) => ({ 42 input: 'about:blank', 43 base: test.input, 44 failure: true 45 })); 46 47const failureTests = originalFailures 48 .concat(typeFailures) 49 .concat(aboutBlankFailures); 50 51const expectedError = { code: 'ERR_INVALID_URL', name: 'TypeError' }; 52 53for (const test of failureTests) { 54 assert.throws( 55 () => new URL(test.input, test.base), 56 (error) => { 57 assert.throws(() => { throw error; }, expectedError); 58 59 // The input could be processed, so we don't do strict matching here 60 let match; 61 assert(match = (`${error}`).match(/Invalid URL: (.*)$/)); 62 assert.strictEqual(error.input, match[1]); 63 return true; 64 }); 65} 66 67const additional_tests = 68 require(fixtures.path('url-tests-additional.js')); 69 70for (const test of additional_tests) { 71 const url = new URL(test.url); 72 if (test.href) assert.strictEqual(url.href, test.href); 73 if (test.origin) assert.strictEqual(url.origin, test.origin); 74 if (test.protocol) assert.strictEqual(url.protocol, test.protocol); 75 if (test.username) assert.strictEqual(url.username, test.username); 76 if (test.password) assert.strictEqual(url.password, test.password); 77 if (test.hostname) assert.strictEqual(url.hostname, test.hostname); 78 if (test.host) assert.strictEqual(url.host, test.host); 79 if (test.port !== undefined) assert.strictEqual(url.port, test.port); 80 if (test.pathname) assert.strictEqual(url.pathname, test.pathname); 81 if (test.search) assert.strictEqual(url.search, test.search); 82 if (test.hash) assert.strictEqual(url.hash, test.hash); 83} 84