• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Refs: https://github.com/nodejs/node/issues/5832
4require('../common');
5const url = require('url');
6const assert = require('assert');
7const fixtures = require('../common/fixtures');
8const tests = require(
9  fixtures.path('wpt', 'url', 'resources', 'urltestdata.json')
10);
11
12let failed = 0;
13let attempted = 0;
14
15tests.forEach((test) => {
16  attempted++;
17  // Skip comments
18  if (typeof test === 'string') return;
19  let parsed;
20
21  try {
22    // Attempt to parse
23    parsed = url.parse(url.resolve(test.base, test.input));
24    if (test.failure) {
25      // If the test was supposed to fail and we didn't get an
26      // error, treat it as a failure.
27      failed++;
28    } else {
29      // Test was not supposed to fail, so we're good so far. Now
30      // check the results of the parse.
31      let username, password;
32      try {
33        assert.strictEqual(test.href, parsed.href);
34        assert.strictEqual(test.protocol, parsed.protocol);
35        username = parsed.auth ? parsed.auth.split(':', 2)[0] : '';
36        password = parsed.auth ? parsed.auth.split(':', 2)[1] : '';
37        assert.strictEqual(test.username, username);
38        assert.strictEqual(test.password, password);
39        assert.strictEqual(test.host, parsed.host);
40        assert.strictEqual(test.hostname, parsed.hostname);
41        assert.strictEqual(+test.port, +parsed.port);
42        assert.strictEqual(test.pathname, parsed.pathname || '/');
43        assert.strictEqual(test.search, parsed.search || '');
44        assert.strictEqual(test.hash, parsed.hash || '');
45      } catch {
46        // For now, we're just interested in the number of failures.
47        failed++;
48      }
49    }
50  } catch {
51    // If Parse failed and it wasn't supposed to, treat it as a failure.
52    if (!test.failure)
53      failed++;
54  }
55});
56
57assert.ok(failed === 0, `${failed} failed tests (out of ${attempted})`);
58