-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
Expand file tree
/
Copy pathtest-urlpattern-types.js
More file actions
142 lines (119 loc) Β· 4.4 KB
/
test-urlpattern-types.js
File metadata and controls
142 lines (119 loc) Β· 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
require('../common');
const { URLPattern } = require('url');
const assert = require('assert');
// Verifies that calling URLPattern with no new keyword throws.
assert.throws(() => URLPattern(), {
code: 'ERR_CONSTRUCT_CALL_REQUIRED',
});
// Verifies that type checks are performed on the arguments.
assert.throws(() => new URLPattern(1), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => new URLPattern({}, 1), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => new URLPattern({}, '', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});
// Per WebIDL, ignoreCase is coerced to boolean (not type-checked).
{
const p = new URLPattern({}, { ignoreCase: '' });
assert.strictEqual(p.protocol, '*');
}
{
const p = new URLPattern({}, { ignoreCase: undefined });
assert.strictEqual(p.protocol, '*');
}
{
const p = new URLPattern({}, {});
assert.strictEqual(p.protocol, '*');
}
const pattern = new URLPattern();
assert.throws(() => pattern.exec(1), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => pattern.exec('', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => pattern.test(1), {
code: 'ERR_INVALID_ARG_TYPE',
});
assert.throws(() => pattern.test('', 1), {
code: 'ERR_INVALID_ARG_TYPE',
});
// Per WebIDL, undefined/null for a URLPatternInput (union including dictionary)
// uses the default value (empty URLPatternInit {}).
// Constructor: undefined input should be treated as empty init.
{
const p = new URLPattern(undefined);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.hostname, '*');
}
// Constructor: null input should be treated as empty init (union, dict branch).
{
const p = new URLPattern(null);
assert.strictEqual(p.protocol, '*');
assert.strictEqual(p.hostname, '*');
}
// Constructor: 2-arg with undefined/null uses overload 2 (options defaults).
{
const p1 = new URLPattern(undefined, undefined);
assert.strictEqual(p1.protocol, '*');
const p2 = new URLPattern(null, null);
assert.strictEqual(p2.protocol, '*');
const p3 = new URLPattern({}, null);
assert.strictEqual(p3.protocol, '*');
const p4 = new URLPattern('https://example.com', null);
assert.strictEqual(p4.hostname, 'example.com');
const p5 = new URLPattern('https://example.com', undefined);
assert.strictEqual(p5.hostname, 'example.com');
}
// Constructor: valid input with undefined/null options.
{
const p = new URLPattern({ pathname: '/foo' }, undefined);
assert.strictEqual(p.pathname, '/foo');
}
// Constructor: 3-arg with null/undefined baseURL is stringified per WebIDL,
// rejected as invalid URL by the parser.
assert.throws(() => new URLPattern('https://example.com', null, null));
assert.throws(() => new URLPattern('https://example.com', undefined, undefined));
// Constructor: 3-arg with valid baseURL and null options uses defaults.
{
const p = new URLPattern('https://example.com', 'https://example.com', null);
assert.strictEqual(p.hostname, 'example.com');
const p2 = new URLPattern('https://example.com', 'https://example.com', undefined);
assert.strictEqual(p2.hostname, 'example.com');
}
// exec() and test(): undefined input should be treated as empty init.
{
const p = new URLPattern();
assert.strictEqual(p.test(undefined), true);
assert.strictEqual(p.test(undefined, undefined), true);
assert.notStrictEqual(p.exec(undefined), null);
assert.notStrictEqual(p.exec(undefined, undefined), null);
}
// exec() and test(): null input should be treated as empty init.
{
const p = new URLPattern();
assert.strictEqual(p.test(null), true);
assert.notStrictEqual(p.exec(null), null);
}
// exec() and test(): null for baseURL is stringified to "null" per WebIDL.
// With string input, "null" is not a valid base URL so match fails silently.
// With dict input, providing baseURL with a dict throws per spec.
{
const p = new URLPattern();
// String input + null baseURL: no throw, match returns null (false).
assert.strictEqual(p.test('https://example.com', null), false);
assert.strictEqual(p.exec('https://example.com', null), null);
// Dict input + null baseURL: throws (baseURL not allowed with dict input).
assert.throws(() => p.test(null, null));
assert.throws(() => p.exec(null, null));
}
// exec() and test(): valid input with undefined baseURL.
{
const p = new URLPattern({ protocol: 'https' });
assert.strictEqual(p.test('https://example.com', undefined), true);
assert.notStrictEqual(p.exec('https://example.com', undefined), null);
}