-
Notifications
You must be signed in to change notification settings - Fork 30.2k
/
test-whatwg-url-properties.js
142 lines (126 loc) Β· 3.5 KB
/
test-whatwg-url-properties.js
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 assert = require('assert');
const { URL, URLSearchParams, format } = require('url');
[
{ name: 'toString' },
{ name: 'toJSON' },
{ name: Symbol.for('nodejs.util.inspect.custom') },
].forEach(({ name }) => {
testMethod(URL.prototype, name);
});
[
'http://www.google.com',
'https://www.domain.com:443',
'file:///Users/yagiz/Developer/node',
].forEach((url) => {
const u = new URL(url);
assert.strictEqual(JSON.stringify(u), `"${u.href}"`);
assert.strictEqual(u.toString(), u.href);
assert.strictEqual(format(u), u.href);
});
[
{ name: 'href' },
{ name: 'protocol' },
{ name: 'username' },
{ name: 'password' },
{ name: 'host' },
{ name: 'hostname' },
{ name: 'port' },
{ name: 'pathname' },
{ name: 'search' },
{ name: 'hash' },
{ name: 'origin', readonly: true },
{ name: 'searchParams', readonly: true },
].forEach(({ name, readonly = false }) => {
testAccessor(URL.prototype, name, readonly);
});
[
{ name: 'createObjectURL' },
{ name: 'revokeObjectURL' },
].forEach(({ name }) => {
testStaticAccessor(URL, name);
});
[
{ name: 'append' },
{ name: 'delete' },
{ name: 'get' },
{ name: 'getAll' },
{ name: 'has' },
{ name: 'set' },
{ name: 'sort' },
{ name: 'entries' },
{ name: 'forEach' },
{ name: 'keys' },
{ name: 'values' },
{ name: 'toString' },
{ name: Symbol.iterator, methodName: 'entries' },
{ name: Symbol.for('nodejs.util.inspect.custom') },
].forEach(({ name, methodName }) => {
testMethod(URLSearchParams.prototype, name, methodName);
});
{
const params = new URLSearchParams();
params.append('a', 'b');
params.append('a', 'c');
params.append('b', 'c');
assert.strictEqual(params.size, 3);
}
{
const u = new URL('https://abc.com/?q=old');
const s = u.searchParams;
u.href = 'http://abc.com/?q=new';
assert.strictEqual(s.get('q'), 'new');
}
function stringifyName(name) {
if (typeof name === 'symbol') {
const { description } = name;
if (description === undefined) {
return '';
}
return `[${description}]`;
}
return name;
}
function testMethod(target, name, methodName = stringifyName(name)) {
const desc = Object.getOwnPropertyDescriptor(target, name);
assert.notStrictEqual(desc, undefined);
assert.strictEqual(desc.enumerable, typeof name === 'string');
const { value } = desc;
assert.strictEqual(typeof value, 'function');
assert.strictEqual(value.name, methodName);
assert.strictEqual(
Object.hasOwn(value, 'prototype'),
false,
);
}
function testAccessor(target, name, readonly = false) {
const desc = Object.getOwnPropertyDescriptor(target, name);
assert.notStrictEqual(desc, undefined);
assert.strictEqual(desc.enumerable, typeof name === 'string');
const methodName = stringifyName(name);
const { get, set } = desc;
assert.strictEqual(typeof get, 'function');
assert.strictEqual(get.name, `get ${methodName}`);
assert.strictEqual(
Object.hasOwn(get, 'prototype'),
false,
);
if (readonly) {
assert.strictEqual(set, undefined);
} else {
assert.strictEqual(typeof set, 'function');
assert.strictEqual(set.name, `set ${methodName}`);
assert.strictEqual(
Object.hasOwn(set, 'prototype'),
false,
);
}
}
function testStaticAccessor(target, name) {
const desc = Object.getOwnPropertyDescriptor(target, name);
assert.notStrictEqual(desc, undefined);
assert.strictEqual(desc.configurable, true);
assert.strictEqual(desc.enumerable, true);
assert.strictEqual(desc.writable, true);
}