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