node/test/parallel/test-whatwg-url-parsing.js
James M Snell 7535a94c8a test: begin normalizing fixtures use
Adds a new `../common/fixtures' module to begin normalizing
`test/fixtures` use. Our test code is a bit inconsistent with
regards to use of the fixtures directory. Some code uses
`path.join()`, some code uses string concats, some other
code uses template strings, etc. In mnay cases, significant
duplication of code is seen when accessing fixture files, etc.

This updates many (but by no means all) of the tests in the
test suite to use the new consistent API. There are still
many more to update, which would make an excelent Code-n-Learn
exercise.

PR-URL: https://github.com/nodejs/node/pull/14332
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2017-08-07 18:00:57 -07:00

64 lines
2.0 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasIntl) {
// A handful of the tests fail when ICU is not included.
common.skip('missing Intl');
}
const URL = require('url').URL;
const assert = require('assert');
const fixtures = require('../common/fixtures');
// Tests below are not from WPT.
const tests = require(fixtures.path('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: () => {} }
]);
const expectedError = common.expectsError(
{ code: 'ERR_INVALID_URL', type: TypeError }, 102);
for (const test of failureTests) {
assert.throws(
() => new URL(test.input, test.base),
(error) => {
if (!expectedError(error))
return false;
// The input could be processed, so we don't do strict matching here
const match = (error + '').match(/Invalid URL: (.*)$/);
if (!match) {
return false;
}
return error.input === match[1];
});
}
const additional_tests =
require(fixtures.path('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);
}