Skip to content

Commit

Permalink
test: move more url tests to node:test
Browse files Browse the repository at this point in the history
PR-URL: #54636
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
anonrig authored and targos committed Oct 2, 2024
1 parent d2ec961 commit 124f715
Show file tree
Hide file tree
Showing 9 changed files with 533 additions and 528 deletions.
26 changes: 11 additions & 15 deletions test/parallel/test-url-domain-ascii-unicode.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict';

const common = require('../common');
if (!common.hasIntl)
common.skip('missing Intl');
const { hasIntl } = require('../common');

const strictEqual = require('assert').strictEqual;
const url = require('url');

const domainToASCII = url.domainToASCII;
const domainToUnicode = url.domainToUnicode;
const { strictEqual } = require('node:assert');
const { domainToASCII, domainToUnicode } = require('node:url');
const { test } = require('node:test');

const domainWithASCII = [
['ıíd', 'xn--d-iga7r'],
Expand All @@ -21,11 +17,11 @@ const domainWithASCII = [
['भारत.org', 'xn--h2brj9c.org'],
];

domainWithASCII.forEach((pair) => {
const domain = pair[0];
const ascii = pair[1];
const domainConvertedToASCII = domainToASCII(domain);
strictEqual(domainConvertedToASCII, ascii);
const asciiConvertedToUnicode = domainToUnicode(ascii);
strictEqual(asciiConvertedToUnicode, domain);
test('domainToASCII and domainToUnicode', { skip: !hasIntl }, () => {
for (const [domain, ascii] of domainWithASCII) {
const domainConvertedToASCII = domainToASCII(domain);
strictEqual(domainConvertedToASCII, ascii);
const asciiConvertedToUnicode = domainToUnicode(ascii);
strictEqual(asciiConvertedToUnicode, domain);
}
});
81 changes: 45 additions & 36 deletions test/parallel/test-url-fileurltopath.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';
const { isWindows } = require('../common');
const assert = require('assert');
const url = require('url');

function testInvalidArgs(...args) {
for (const arg of args) {
const { test } = require('node:test');
const assert = require('node:assert');
const url = require('node:url');

test('invalid arguments', () => {
for (const arg of [null, undefined, 1, {}, true]) {
assert.throws(() => url.fileURLToPath(arg), {
code: 'ERR_INVALID_ARG_TYPE'
});
}
}

// Input must be string or URL
testInvalidArgs(null, undefined, 1, {}, true);
});

// Input must be a file URL
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
code: 'ERR_INVALID_URL_SCHEME'
test('input must be a file URL', () => {
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
code: 'ERR_INVALID_URL_SCHEME'
});
});

{
test('fileURLToPath with host', () => {
const withHost = new URL('file://host/a');

if (isWindows) {
Expand All @@ -29,9 +29,9 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
code: 'ERR_INVALID_FILE_URL_HOST'
});
}
}
});

{
test('fileURLToPath with invalid path', () => {
if (isWindows) {
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
code: 'ERR_INVALID_FILE_URL_PATH'
Expand All @@ -47,7 +47,7 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
code: 'ERR_INVALID_FILE_URL_PATH'
});
}
}
});

const windowsTestCases = [
// Lowercase ascii alpha
Expand Down Expand Up @@ -95,6 +95,7 @@ const windowsTestCases = [
// UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
{ path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' },
];

const posixTestCases = [
// Lowercase ascii alpha
{ path: '/foo', fileURL: 'file:///foo' },
Expand Down Expand Up @@ -140,29 +141,37 @@ const posixTestCases = [
{ path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' },
];

for (const { path, fileURL } of windowsTestCases) {
const fromString = url.fileURLToPath(fileURL, { windows: true });
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
assert.strictEqual(fromURL, path);
}
test('fileURLToPath with windows path', { skip: !isWindows }, () => {

for (const { path, fileURL } of windowsTestCases) {
const fromString = url.fileURLToPath(fileURL, { windows: true });
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
assert.strictEqual(fromURL, path);
}
});

for (const { path, fileURL } of posixTestCases) {
const fromString = url.fileURLToPath(fileURL, { windows: false });
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
assert.strictEqual(fromURL, path);
}
test('fileURLToPath with posix path', { skip: isWindows }, () => {
for (const { path, fileURL } of posixTestCases) {
const fromString = url.fileURLToPath(fileURL, { windows: false });
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
assert.strictEqual(fromURL, path);
}
});

const defaultTestCases = isWindows ? windowsTestCases : posixTestCases;

// Test when `options` is null
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
test('options is null', () => {
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
});

for (const { path, fileURL } of defaultTestCases) {
const fromString = url.fileURLToPath(fileURL);
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL));
assert.strictEqual(fromURL, path);
}
test('defaultTestCases', () => {
for (const { path, fileURL } of defaultTestCases) {
const fromString = url.fileURLToPath(fileURL);
assert.strictEqual(fromString, path);
const fromURL = url.fileURLToPath(new URL(fileURL));
assert.strictEqual(fromURL, path);
}
});
51 changes: 27 additions & 24 deletions test/parallel/test-url-format-invalid-input.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const url = require('url');

const throwsObjsAndReportTypes = [
undefined,
null,
true,
false,
0,
function() {},
Symbol('foo'),
];
require('../common');

for (const urlObject of throwsObjsAndReportTypes) {
assert.throws(() => {
url.format(urlObject);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "urlObject" argument must be one of type object or string.' +
common.invalidArgTypeHelper(urlObject)
});
}
assert.strictEqual(url.format(''), '');
assert.strictEqual(url.format({}), '');
const assert = require('node:assert');
const url = require('node:url');
const { test } = require('node:test');

test('format invalid input', () => {
const throwsObjsAndReportTypes = [
undefined,
null,
true,
false,
0,
function() {},
Symbol('foo'),
];

for (const urlObject of throwsObjsAndReportTypes) {
assert.throws(() => {
url.format(urlObject);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}
assert.strictEqual(url.format(''), '');
assert.strictEqual(url.format({}), '');
});
Loading

0 comments on commit 124f715

Please sign in to comment.