2016-05-31 11:52:19 -07:00
|
|
|
'use strict';
|
|
|
|
|
2018-08-22 03:27:56 +08:00
|
|
|
// Tests below are not from WPT.
|
|
|
|
|
2016-05-31 11:52:19 -07:00
|
|
|
const common = require('../common');
|
2016-10-23 08:08:39 -07:00
|
|
|
if (!common.hasIntl) {
|
|
|
|
// A handful of the tests fail when ICU is not included.
|
2017-01-16 22:34:04 +01:00
|
|
|
common.skip('missing Intl');
|
2016-10-23 08:08:39 -07:00
|
|
|
}
|
|
|
|
|
2017-07-01 02:29:09 +03:00
|
|
|
const assert = require('assert');
|
2017-07-17 15:33:46 -07:00
|
|
|
const fixtures = require('../common/fixtures');
|
2017-07-01 02:29:09 +03:00
|
|
|
|
2018-10-30 12:03:41 +08:00
|
|
|
const tests = require(
|
|
|
|
fixtures.path('wpt', 'url', 'resources', 'urltestdata.json')
|
|
|
|
);
|
2018-05-17 11:35:28 +08:00
|
|
|
|
|
|
|
const originalFailures = tests.filter((test) => test.failure);
|
|
|
|
|
|
|
|
const typeFailures = [
|
2017-03-20 14:18:37 +08:00
|
|
|
{ input: '' },
|
|
|
|
{ input: 'test' },
|
|
|
|
{ input: undefined },
|
|
|
|
{ input: 0 },
|
|
|
|
{ input: true },
|
|
|
|
{ input: false },
|
|
|
|
{ input: null },
|
|
|
|
{ input: new Date() },
|
|
|
|
{ input: new RegExp() },
|
2018-03-12 15:40:00 +02:00
|
|
|
{ input: 'test', base: null },
|
|
|
|
{ input: 'http://nodejs.org', base: null },
|
2021-03-26 08:51:08 -07:00
|
|
|
{ input: () => {} },
|
2018-05-17 11:35:28 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
// See https://github.com/w3c/web-platform-tests/pull/10955
|
|
|
|
// > If `failure` is true, parsing `about:blank` against `base`
|
|
|
|
// > must give failure. This tests that the logic for converting
|
|
|
|
// > base URLs into strings properly fails the whole parsing
|
|
|
|
// > algorithm if the base URL cannot be parsed.
|
|
|
|
const aboutBlankFailures = originalFailures
|
|
|
|
.map((test) => ({
|
|
|
|
input: 'about:blank',
|
|
|
|
base: test.input,
|
|
|
|
failure: true
|
|
|
|
}));
|
|
|
|
|
|
|
|
const failureTests = originalFailures
|
|
|
|
.concat(typeFailures)
|
|
|
|
.concat(aboutBlankFailures);
|
2016-05-31 11:52:19 -07:00
|
|
|
|
2019-12-25 18:02:16 +01:00
|
|
|
const expectedError = { code: 'ERR_INVALID_URL', name: 'TypeError' };
|
2017-04-21 11:54:58 -07:00
|
|
|
|
2017-03-20 14:18:37 +08:00
|
|
|
for (const test of failureTests) {
|
|
|
|
assert.throws(
|
|
|
|
() => new URL(test.input, test.base),
|
|
|
|
(error) => {
|
2019-12-25 18:02:16 +01:00
|
|
|
assert.throws(() => { throw error; }, expectedError);
|
2023-09-26 11:40:17 -04:00
|
|
|
assert.strictEqual(`${error}`, 'TypeError: Invalid URL');
|
2021-05-09 22:43:57 +02:00
|
|
|
assert.strictEqual(error.message, 'Invalid URL');
|
2019-03-16 12:09:14 +01:00
|
|
|
return true;
|
2017-03-20 14:18:37 +08:00
|
|
|
});
|
2016-05-31 11:52:19 -07:00
|
|
|
}
|
|
|
|
|
2017-07-17 15:33:46 -07:00
|
|
|
const additional_tests =
|
|
|
|
require(fixtures.path('url-tests-additional.js'));
|
2016-05-31 11:52:19 -07:00
|
|
|
|
2016-12-31 14:20:10 +08:00
|
|
|
for (const test of additional_tests) {
|
|
|
|
const url = new URL(test.url);
|
2017-02-02 14:13:16 +08:00
|
|
|
if (test.href) assert.strictEqual(url.href, test.href);
|
|
|
|
if (test.origin) assert.strictEqual(url.origin, test.origin);
|
|
|
|
if (test.protocol) assert.strictEqual(url.protocol, test.protocol);
|
|
|
|
if (test.username) assert.strictEqual(url.username, test.username);
|
|
|
|
if (test.password) assert.strictEqual(url.password, test.password);
|
|
|
|
if (test.hostname) assert.strictEqual(url.hostname, test.hostname);
|
|
|
|
if (test.host) assert.strictEqual(url.host, test.host);
|
|
|
|
if (test.port !== undefined) assert.strictEqual(url.port, test.port);
|
|
|
|
if (test.pathname) assert.strictEqual(url.pathname, test.pathname);
|
|
|
|
if (test.search) assert.strictEqual(url.search, test.search);
|
|
|
|
if (test.hash) assert.strictEqual(url.hash, test.hash);
|
2016-12-11 18:20:49 -06:00
|
|
|
}
|
2023-04-11 11:35:46 -04:00
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
new URL();
|
|
|
|
}, {
|
|
|
|
name: 'TypeError',
|
|
|
|
code: 'ERR_MISSING_ARGS',
|
|
|
|
});
|