node/test/parallel/test-whatwg-url-parsing.js
James M Snell 4f2e372716 test: add common.noop, default for common.mustCall()
Export a new common.noop no-operation function for general use.
Allow using common.mustCall() without a fn argument to simplify
test cases.

Replace various non-op functions throughout tests with common.noop

PR-URL: https://github.com/nodejs/node/pull/12027
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
2017-03-26 12:47:15 -07:00

59 lines
1.9 KiB
JavaScript

'use strict';
const common = require('../common');
const URL = require('url').URL;
const path = require('path');
const assert = require('assert');
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
return;
}
// Tests below are not from WPT.
const tests = require(path.join(common.fixturesDir, 'url-tests'));
const failureTests = tests.filter((test) => test.failure).concat([
{ input: '' },
{ input: 'test' },
{ input: undefined },
{ input: 0 },
{ input: true },
{ input: false },
{ input: null },
{ input: new Date() },
{ input: new RegExp() },
{ input: common.noop }
]);
for (const test of failureTests) {
assert.throws(
() => new URL(test.input, test.base),
(error) => {
// The input could be processed, so we don't do strict matching here
const match = (error + '').match(/^TypeError: Invalid URL: (.*)$/);
if (!match) {
return false;
}
return error.input === match[1];
});
}
const additional_tests = require(
path.join(common.fixturesDir, 'url-tests-additional.js'));
for (const test of additional_tests) {
const url = new URL(test.url);
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);
}