Skip to content

Commit 124f715

Browse files
anonrigtargos
authored andcommittedOct 2, 2024
test: move more url tests to node:test
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>
1 parent d2ec961 commit 124f715

9 files changed

+533
-528
lines changed
 
+11-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
'use strict';
22

3-
const common = require('../common');
4-
if (!common.hasIntl)
5-
common.skip('missing Intl');
3+
const { hasIntl } = require('../common');
64

7-
const strictEqual = require('assert').strictEqual;
8-
const url = require('url');
9-
10-
const domainToASCII = url.domainToASCII;
11-
const domainToUnicode = url.domainToUnicode;
5+
const { strictEqual } = require('node:assert');
6+
const { domainToASCII, domainToUnicode } = require('node:url');
7+
const { test } = require('node:test');
128

139
const domainWithASCII = [
1410
['ıíd', 'xn--d-iga7r'],
@@ -21,11 +17,11 @@ const domainWithASCII = [
2117
['भारत.org', 'xn--h2brj9c.org'],
2218
];
2319

24-
domainWithASCII.forEach((pair) => {
25-
const domain = pair[0];
26-
const ascii = pair[1];
27-
const domainConvertedToASCII = domainToASCII(domain);
28-
strictEqual(domainConvertedToASCII, ascii);
29-
const asciiConvertedToUnicode = domainToUnicode(ascii);
30-
strictEqual(asciiConvertedToUnicode, domain);
20+
test('domainToASCII and domainToUnicode', { skip: !hasIntl }, () => {
21+
for (const [domain, ascii] of domainWithASCII) {
22+
const domainConvertedToASCII = domainToASCII(domain);
23+
strictEqual(domainConvertedToASCII, ascii);
24+
const asciiConvertedToUnicode = domainToUnicode(ascii);
25+
strictEqual(asciiConvertedToUnicode, domain);
26+
}
3127
});

‎test/parallel/test-url-fileurltopath.js

+45-36
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict';
22
const { isWindows } = require('../common');
3-
const assert = require('assert');
4-
const url = require('url');
53

6-
function testInvalidArgs(...args) {
7-
for (const arg of args) {
4+
const { test } = require('node:test');
5+
const assert = require('node:assert');
6+
const url = require('node:url');
7+
8+
test('invalid arguments', () => {
9+
for (const arg of [null, undefined, 1, {}, true]) {
810
assert.throws(() => url.fileURLToPath(arg), {
911
code: 'ERR_INVALID_ARG_TYPE'
1012
});
1113
}
12-
}
13-
14-
// Input must be string or URL
15-
testInvalidArgs(null, undefined, 1, {}, true);
14+
});
1615

17-
// Input must be a file URL
18-
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
19-
code: 'ERR_INVALID_URL_SCHEME'
16+
test('input must be a file URL', () => {
17+
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
18+
code: 'ERR_INVALID_URL_SCHEME'
19+
});
2020
});
2121

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

2525
if (isWindows) {
@@ -29,9 +29,9 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
2929
code: 'ERR_INVALID_FILE_URL_HOST'
3030
});
3131
}
32-
}
32+
});
3333

34-
{
34+
test('fileURLToPath with invalid path', () => {
3535
if (isWindows) {
3636
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
3737
code: 'ERR_INVALID_FILE_URL_PATH'
@@ -47,7 +47,7 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
4747
code: 'ERR_INVALID_FILE_URL_PATH'
4848
});
4949
}
50-
}
50+
});
5151

5252
const windowsTestCases = [
5353
// Lowercase ascii alpha
@@ -95,6 +95,7 @@ const windowsTestCases = [
9595
// UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
9696
{ path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' },
9797
];
98+
9899
const posixTestCases = [
99100
// Lowercase ascii alpha
100101
{ path: '/foo', fileURL: 'file:///foo' },
@@ -140,29 +141,37 @@ const posixTestCases = [
140141
{ path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' },
141142
];
142143

143-
for (const { path, fileURL } of windowsTestCases) {
144-
const fromString = url.fileURLToPath(fileURL, { windows: true });
145-
assert.strictEqual(fromString, path);
146-
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
147-
assert.strictEqual(fromURL, path);
148-
}
144+
test('fileURLToPath with windows path', { skip: !isWindows }, () => {
145+
146+
for (const { path, fileURL } of windowsTestCases) {
147+
const fromString = url.fileURLToPath(fileURL, { windows: true });
148+
assert.strictEqual(fromString, path);
149+
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
150+
assert.strictEqual(fromURL, path);
151+
}
152+
});
149153

150-
for (const { path, fileURL } of posixTestCases) {
151-
const fromString = url.fileURLToPath(fileURL, { windows: false });
152-
assert.strictEqual(fromString, path);
153-
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
154-
assert.strictEqual(fromURL, path);
155-
}
154+
test('fileURLToPath with posix path', { skip: isWindows }, () => {
155+
for (const { path, fileURL } of posixTestCases) {
156+
const fromString = url.fileURLToPath(fileURL, { windows: false });
157+
assert.strictEqual(fromString, path);
158+
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
159+
assert.strictEqual(fromURL, path);
160+
}
161+
});
156162

157163
const defaultTestCases = isWindows ? windowsTestCases : posixTestCases;
158164

159-
// Test when `options` is null
160-
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
161-
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
165+
test('options is null', () => {
166+
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
167+
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
168+
});
162169

163-
for (const { path, fileURL } of defaultTestCases) {
164-
const fromString = url.fileURLToPath(fileURL);
165-
assert.strictEqual(fromString, path);
166-
const fromURL = url.fileURLToPath(new URL(fileURL));
167-
assert.strictEqual(fromURL, path);
168-
}
170+
test('defaultTestCases', () => {
171+
for (const { path, fileURL } of defaultTestCases) {
172+
const fromString = url.fileURLToPath(fileURL);
173+
assert.strictEqual(fromString, path);
174+
const fromURL = url.fileURLToPath(new URL(fileURL));
175+
assert.strictEqual(fromURL, path);
176+
}
177+
});
+27-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
'use strict';
2-
const common = require('../common');
3-
const assert = require('assert');
4-
const url = require('url');
52

6-
const throwsObjsAndReportTypes = [
7-
undefined,
8-
null,
9-
true,
10-
false,
11-
0,
12-
function() {},
13-
Symbol('foo'),
14-
];
3+
require('../common');
154

16-
for (const urlObject of throwsObjsAndReportTypes) {
17-
assert.throws(() => {
18-
url.format(urlObject);
19-
}, {
20-
code: 'ERR_INVALID_ARG_TYPE',
21-
name: 'TypeError',
22-
message: 'The "urlObject" argument must be one of type object or string.' +
23-
common.invalidArgTypeHelper(urlObject)
24-
});
25-
}
26-
assert.strictEqual(url.format(''), '');
27-
assert.strictEqual(url.format({}), '');
5+
const assert = require('node:assert');
6+
const url = require('node:url');
7+
const { test } = require('node:test');
8+
9+
test('format invalid input', () => {
10+
const throwsObjsAndReportTypes = [
11+
undefined,
12+
null,
13+
true,
14+
false,
15+
0,
16+
function() {},
17+
Symbol('foo'),
18+
];
19+
20+
for (const urlObject of throwsObjsAndReportTypes) {
21+
assert.throws(() => {
22+
url.format(urlObject);
23+
}, {
24+
code: 'ERR_INVALID_ARG_TYPE',
25+
name: 'TypeError',
26+
});
27+
}
28+
assert.strictEqual(url.format(''), '');
29+
assert.strictEqual(url.format({}), '');
30+
});
+134-132
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,149 @@
11
'use strict';
22

3-
const common = require('../common');
4-
if (!common.hasIntl)
5-
common.skip('missing Intl');
3+
const { hasIntl } = require('../common');
64

7-
const assert = require('assert');
8-
const url = require('url');
5+
const assert = require('node:assert');
6+
const url = require('node:url');
7+
const { test } = require('node:test');
98

109
const myURL = new URL('http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c');
1110

12-
assert.strictEqual(
13-
url.format(myURL),
14-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
15-
);
11+
test('should format', { skip: !hasIntl }, () => {
12+
assert.strictEqual(
13+
url.format(myURL),
14+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
15+
);
1616

17-
assert.strictEqual(
18-
url.format(myURL, {}),
19-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
20-
);
17+
assert.strictEqual(
18+
url.format(myURL, {}),
19+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
20+
);
21+
});
2122

22-
{
23-
[true, 1, 'test', Infinity].forEach((value) => {
23+
test('handle invalid arguments', { skip: !hasIntl }, () => {
24+
for (const value of [true, 1, 'test', Infinity]) {
2425
assert.throws(
2526
() => url.format(myURL, value),
2627
{
2728
code: 'ERR_INVALID_ARG_TYPE',
2829
name: 'TypeError',
29-
message: 'The "options" argument must be of type object.' +
30-
common.invalidArgTypeHelper(value)
3130
}
3231
);
33-
});
34-
}
35-
36-
// Any falsy value other than undefined will be treated as false.
37-
// Any truthy value will be treated as true.
38-
39-
assert.strictEqual(
40-
url.format(myURL, { auth: false }),
41-
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
42-
);
43-
44-
assert.strictEqual(
45-
url.format(myURL, { auth: '' }),
46-
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
47-
);
48-
49-
assert.strictEqual(
50-
url.format(myURL, { auth: 0 }),
51-
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
52-
);
53-
54-
assert.strictEqual(
55-
url.format(myURL, { auth: 1 }),
56-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
57-
);
58-
59-
assert.strictEqual(
60-
url.format(myURL, { auth: {} }),
61-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
62-
);
63-
64-
assert.strictEqual(
65-
url.format(myURL, { fragment: false }),
66-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
67-
);
68-
69-
assert.strictEqual(
70-
url.format(myURL, { fragment: '' }),
71-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
72-
);
73-
74-
assert.strictEqual(
75-
url.format(myURL, { fragment: 0 }),
76-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
77-
);
78-
79-
assert.strictEqual(
80-
url.format(myURL, { fragment: 1 }),
81-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
82-
);
83-
84-
assert.strictEqual(
85-
url.format(myURL, { fragment: {} }),
86-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
87-
);
88-
89-
assert.strictEqual(
90-
url.format(myURL, { search: false }),
91-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
92-
);
93-
94-
assert.strictEqual(
95-
url.format(myURL, { search: '' }),
96-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
97-
);
98-
99-
assert.strictEqual(
100-
url.format(myURL, { search: 0 }),
101-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
102-
);
103-
104-
assert.strictEqual(
105-
url.format(myURL, { search: 1 }),
106-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
107-
);
108-
109-
assert.strictEqual(
110-
url.format(myURL, { search: {} }),
111-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
112-
);
113-
114-
assert.strictEqual(
115-
url.format(myURL, { unicode: true }),
116-
'http://user:pass@理容ナカムラ.com/a?a=b#c'
117-
);
118-
119-
assert.strictEqual(
120-
url.format(myURL, { unicode: 1 }),
121-
'http://user:pass@理容ナカムラ.com/a?a=b#c'
122-
);
123-
124-
assert.strictEqual(
125-
url.format(myURL, { unicode: {} }),
126-
'http://user:pass@理容ナカムラ.com/a?a=b#c'
127-
);
128-
129-
assert.strictEqual(
130-
url.format(myURL, { unicode: false }),
131-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
132-
);
133-
134-
assert.strictEqual(
135-
url.format(myURL, { unicode: 0 }),
136-
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
137-
);
138-
139-
assert.strictEqual(
140-
url.format(new URL('http://user:pass@xn--0zwm56d.com:8080/path'), { unicode: true }),
141-
'http://user:pass@测试.com:8080/path'
142-
);
143-
144-
assert.strictEqual(
145-
url.format(new URL('tel:123')),
146-
url.format(new URL('tel:123'), { unicode: true })
147-
);
32+
}
33+
});
34+
35+
test('any falsy value other than undefined will be treated as false', { skip: !hasIntl }, () => {
36+
assert.strictEqual(
37+
url.format(myURL, { auth: false }),
38+
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
39+
);
40+
41+
assert.strictEqual(
42+
url.format(myURL, { auth: '' }),
43+
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
44+
);
45+
46+
assert.strictEqual(
47+
url.format(myURL, { auth: 0 }),
48+
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
49+
);
50+
51+
assert.strictEqual(
52+
url.format(myURL, { auth: 1 }),
53+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
54+
);
55+
56+
assert.strictEqual(
57+
url.format(myURL, { auth: {} }),
58+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
59+
);
60+
61+
assert.strictEqual(
62+
url.format(myURL, { fragment: false }),
63+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
64+
);
65+
66+
assert.strictEqual(
67+
url.format(myURL, { fragment: '' }),
68+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
69+
);
70+
71+
assert.strictEqual(
72+
url.format(myURL, { fragment: 0 }),
73+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b'
74+
);
75+
76+
assert.strictEqual(
77+
url.format(myURL, { fragment: 1 }),
78+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
79+
);
80+
81+
assert.strictEqual(
82+
url.format(myURL, { fragment: {} }),
83+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
84+
);
85+
86+
assert.strictEqual(
87+
url.format(myURL, { search: false }),
88+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
89+
);
90+
91+
assert.strictEqual(
92+
url.format(myURL, { search: '' }),
93+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
94+
);
95+
96+
assert.strictEqual(
97+
url.format(myURL, { search: 0 }),
98+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c'
99+
);
100+
101+
assert.strictEqual(
102+
url.format(myURL, { search: 1 }),
103+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
104+
);
105+
106+
assert.strictEqual(
107+
url.format(myURL, { search: {} }),
108+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
109+
);
110+
111+
assert.strictEqual(
112+
url.format(myURL, { unicode: true }),
113+
'http://user:pass@理容ナカムラ.com/a?a=b#c'
114+
);
115+
116+
assert.strictEqual(
117+
url.format(myURL, { unicode: 1 }),
118+
'http://user:pass@理容ナカムラ.com/a?a=b#c'
119+
);
120+
121+
assert.strictEqual(
122+
url.format(myURL, { unicode: {} }),
123+
'http://user:pass@理容ナカムラ.com/a?a=b#c'
124+
);
125+
126+
assert.strictEqual(
127+
url.format(myURL, { unicode: false }),
128+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
129+
);
130+
131+
assert.strictEqual(
132+
url.format(myURL, { unicode: 0 }),
133+
'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'
134+
);
135+
});
136+
137+
test('should format with unicode: true', { skip: !hasIntl }, () => {
138+
assert.strictEqual(
139+
url.format(new URL('http://user:pass@xn--0zwm56d.com:8080/path'), { unicode: true }),
140+
'http://user:pass@测试.com:8080/path'
141+
);
142+
});
143+
144+
test('should format tel: prefix', { skip: !hasIntl }, () => {
145+
assert.strictEqual(
146+
url.format(new URL('tel:123')),
147+
url.format(new URL('tel:123'), { unicode: true })
148+
);
149+
});

‎test/parallel/test-url-format.js

+260-259
Large diffs are not rendered by default.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
6+
const { URL, parse } = require('node:url');
7+
const assert = require('node:assert');
8+
const { isURL } = require('internal/url');
9+
const { test } = require('node:test');
10+
11+
test('isURL', () => {
12+
assert.strictEqual(isURL(new URL('https://www.nodejs.org')), true);
13+
assert.strictEqual(isURL(parse('https://www.nodejs.org')), false);
14+
assert.strictEqual(isURL({
15+
href: 'https://www.nodejs.org',
16+
protocol: 'https:',
17+
path: '/',
18+
}), false);
19+
});

‎test/parallel/test-url-is-url.js

-16
This file was deleted.

‎test/parallel/test-url-null-char.js

-8
This file was deleted.

‎test/parallel/test-url-parse-format.js

+37-38
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
'use strict';
2-
const common = require('../common');
2+
const { hasIntl } = require('../common');
33

4-
if (!common.hasIntl)
5-
common.skip('missing Intl');
6-
7-
const assert = require('assert');
8-
const inspect = require('util').inspect;
9-
10-
const url = require('url');
4+
const assert = require('node:assert');
5+
const { inspect } = require('node:util');
6+
const url = require('node:url');
7+
const { test } = require('node:test');
118

129
// URLs to parse, and expected data
1310
// { url : parsed }
@@ -1025,36 +1022,38 @@ const parseTests = {
10251022
}
10261023
};
10271024

1028-
for (const u in parseTests) {
1029-
let actual = url.parse(u);
1030-
const spaced = url.parse(` \t ${u}\n\t`);
1031-
let expected = Object.assign(new url.Url(), parseTests[u]);
1032-
1033-
Object.keys(actual).forEach(function(i) {
1034-
if (expected[i] === undefined && actual[i] === null) {
1035-
expected[i] = null;
1036-
}
1037-
});
1025+
test('should parse and format', { skip: !hasIntl }, () => {
1026+
for (const u in parseTests) {
1027+
let actual = url.parse(u);
1028+
const spaced = url.parse(` \t ${u}\n\t`);
1029+
let expected = Object.assign(new url.Url(), parseTests[u]);
1030+
1031+
Object.keys(actual).forEach(function(i) {
1032+
if (expected[i] === undefined && actual[i] === null) {
1033+
expected[i] = null;
1034+
}
1035+
});
1036+
1037+
assert.deepStrictEqual(
1038+
actual,
1039+
expected,
1040+
`parsing ${u} and expected ${inspect(expected)} but got ${inspect(actual)}`
1041+
);
1042+
assert.deepStrictEqual(
1043+
spaced,
1044+
expected,
1045+
`expected ${inspect(expected)}, got ${inspect(spaced)}`
1046+
);
1047+
1048+
expected = parseTests[u].href;
1049+
actual = url.format(parseTests[u]);
1050+
1051+
assert.strictEqual(actual, expected,
1052+
`format(${u}) == ${u}\nactual:${actual}`);
1053+
}
1054+
});
10381055

1039-
assert.deepStrictEqual(
1040-
actual,
1041-
expected,
1042-
`parsing ${u} and expected ${inspect(expected)} but got ${inspect(actual)}`
1043-
);
1044-
assert.deepStrictEqual(
1045-
spaced,
1046-
expected,
1047-
`expected ${inspect(expected)}, got ${inspect(spaced)}`
1048-
);
1049-
1050-
expected = parseTests[u].href;
1051-
actual = url.format(parseTests[u]);
1052-
1053-
assert.strictEqual(actual, expected,
1054-
`format(${u}) == ${u}\nactual:${actual}`);
1055-
}
1056-
1057-
{
1056+
test('parse result should equal new url.Url()', { skip: !hasIntl }, () => {
10581057
const parsed = url.parse('http://nodejs.org/')
10591058
.resolveObject('jAvascript:alert(1);a=\x27@white-listed.com\x27');
10601059

@@ -1074,4 +1073,4 @@ for (const u in parseTests) {
10741073
});
10751074

10761075
assert.deepStrictEqual(parsed, expected);
1077-
}
1076+
});

0 commit comments

Comments
 (0)
Please sign in to comment.