Debian Codegraph @

url/testdata/url_test.js

raw
1 "use strict";
2
3 const assert = require("../../assert.js");
4
5 function testURLCtor(str, expected) {
6 assert.sameValue(new URL(str).toString(), expected);
7 }
8
9 function testURLCtorBase(ref, base, expected, message) {
10 assert.sameValue(new URL(ref, base).toString(), expected, message);
11 }
12
13 testURLCtorBase("https://example.org/", undefined, "https://example.org/");
14 testURLCtorBase("/foo", "https://example.org/", "https://example.org/foo");
15 testURLCtorBase("http://Example.com/", "https://example.org/", "http://example.com/");
16 testURLCtorBase("https://Example.com/", "https://example.org/", "https://example.com/");
17 testURLCtorBase("foo://Example.com/", "https://example.org/", "foo://Example.com/");
18 testURLCtorBase("foo:Example.com/", "https://example.org/", "foo:Example.com/");
19 testURLCtorBase("#hash", "https://example.org/", "https://example.org/#hash");
20
21 testURLCtor("HTTP://test.com", "http://test.com/");
22 testURLCtor("HTTPS://á.com", "https://xn--1ca.com/");
23 testURLCtor("HTTPS://á.com:123", "https://xn--1ca.com:123/");
24 testURLCtor("https://test.com#asdfá", "https://test.com/#asdf%C3%A1");
25 testURLCtor("HTTPS://á.com:123/á", "https://xn--1ca.com:123/%C3%A1");
26 testURLCtor("fish://á.com", "fish://%C3%A1.com");
27 testURLCtor("https://test.com/?a=1 /2", "https://test.com/?a=1%20/2");
28 testURLCtor("https://test.com/á=1?á=1&ü=2#é", "https://test.com/%C3%A1=1?%C3%A1=1&%C3%BC=2#%C3%A9");
29
30 assert.throws(() => new URL("test"), TypeError);
31 assert.throws(() => new URL("ssh://EEE:ddd"), TypeError);
32
33 {
34 let u = new URL("https://example.org/");
35 assert.sameValue(u.__proto__.constructor, URL);
36 assert.sameValue(u instanceof URL, true);
37 }
38
39 {
40 let u = new URL("https://example.org/");
41 assert.sameValue(u.searchParams, u.searchParams);
42 }
43
44 let myURL;
45
46 // Hash
47 myURL = new URL("https://example.org/foo#bar");
48 myURL.hash = "baz";
49 assert.sameValue(myURL.href, "https://example.org/foo#baz");
50
51 myURL.hash = "#baz";
52 assert.sameValue(myURL.href, "https://example.org/foo#baz");
53
54 myURL.hash = "#á=1 2";
55 assert.sameValue(myURL.href, "https://example.org/foo#%C3%A1=1%202");
56
57 myURL.hash = "#a/#b";
58 // FAILING: the second # gets escaped
59 //assert.sameValue(myURL.href, "https://example.org/foo#a/#b");
60 assert.sameValue(myURL.search, "");
61 // FAILING: the second # gets escaped
62 //assert.sameValue(myURL.hash, "#a/#b");
63
64 // Host
65 myURL = new URL("https://example.org:81/foo");
66 myURL.host = "example.com:82";
67 assert.sameValue(myURL.href, "https://example.com:82/foo");
68
69 // Hostname
70 myURL = new URL("https://example.org:81/foo");
71 myURL.hostname = "example.com:82";
72 assert.sameValue(myURL.href, "https://example.org:81/foo");
73
74 myURL.hostname = "á.com";
75 assert.sameValue(myURL.href, "https://xn--1ca.com:81/foo");
76
77 // href
78 myURL = new URL("https://example.org/foo");
79 myURL.href = "https://example.com/bar";
80 assert.sameValue(myURL.href, "https://example.com/bar");
81
82 // Password
83 myURL = new URL("https://abc:[email protected]");
84 myURL.password = "123";
85 assert.sameValue(myURL.href, "https://abc:[email protected]/");
86
87 // pathname
88 myURL = new URL("https://example.org/abc/xyz?123");
89 myURL.pathname = "/abcdef";
90 assert.sameValue(myURL.href, "https://example.org/abcdef?123");
91 assert.sameValue(myURL.toString(), "https://example.org/abcdef?123");
92
93 myURL.pathname = "";
94 assert.sameValue(myURL.href, "https://example.org/?123");
95
96 myURL.pathname = "á";
97 assert.sameValue(myURL.pathname, "/%C3%A1");
98 assert.sameValue(myURL.href, "https://example.org/%C3%A1?123");
99
100 myURL = new URL("file:///./abc");
101 assert.sameValue(myURL.pathname, "/abc");
102
103 myURL = new URL("fish://host/.");
104 assert.sameValue(myURL.pathname, "/");
105 myURL.pathname = ".";
106 assert.sameValue(myURL.pathname, "/");
107
108 myURL = new URL("fish://host/a/../b");
109 assert.sameValue(myURL.pathname, '/b');
110 myURL.pathname = 'a/../c';
111 assert.sameValue(myURL.pathname, '/c');
112
113 myURL = new URL("file://");
114 assert.sameValue(myURL.pathname, "/");
115 assert.sameValue(myURL.href, "file:///");
116
117 assert.throwsNodeError(() => {
118 new URL("http://");
119 }, TypeError, "ERR_INVALID_URL");
120
121 // myURL = new URL("fish://");
122 // assert.sameValue(myURL.pathname, "");
123 // Currently returns "fish:"
124 // assert.sameValue(myURL.href, "fish://");
125
126 // port
127
128 myURL = new URL("https://example.org:8888");
129 assert.sameValue(myURL.port, "8888");
130
131 function testSetPort(port, expected) {
132 const url = new URL("https://example.org:8888");
133 url.port = port;
134 assert.sameValue(url.port, expected);
135 }
136
137 testSetPort(0, "0");
138 testSetPort(-0, "0");
139
140 // Default ports are automatically transformed to the empty string
141 // (HTTPS protocol's default port is 443)
142 testSetPort("443", "");
143 testSetPort(443, "");
144
145 // Empty string is the same as default port
146 testSetPort("", "");
147
148 // Completely invalid port strings are ignored
149 testSetPort("abcd", "8888");
150 testSetPort("-123", "");
151 testSetPort(-123, "");
152 testSetPort(-123.45, "");
153 testSetPort(undefined, "8888");
154 testSetPort(null, "8888");
155 testSetPort(+Infinity, "8888");
156 testSetPort(-Infinity, "8888");
157 testSetPort(NaN, "8888");
158
159 // Leading numbers are treated as a port number
160 testSetPort("5678abcd", "5678");
161 testSetPort("a5678abcd", "");
162
163 // Non-integers are truncated
164 testSetPort(1234.5678, "1234");
165
166 // Out-of-range numbers which are not represented in scientific notation
167 // will be ignored.
168 testSetPort(1e10, "8888");
169 testSetPort("123456", "8888");
170 testSetPort(123456, "8888");
171 testSetPort(4.567e21, "4");
172
173 // toString() takes precedence over valueOf(), even if it returns a valid integer
174 testSetPort(
175 {
176 toString() {
177 return "2";
178 },
179 valueOf() {
180 return 1;
181 },
182 },
183 "2"
184 );
185
186 // Protocol
187 function testSetProtocol(url, protocol, expected) {
188 url.protocol = protocol;
189 assert.sameValue(url.protocol, expected);
190 }
191 testSetProtocol(new URL("https://example.org"), "ftp", "ftp:");
192 testSetProtocol(new URL("https://example.org"), "ftp:", "ftp:");
193 testSetProtocol(new URL("https://example.org"), "FTP:", "ftp:");
194 testSetProtocol(new URL("https://example.org"), "ftp: blah", "ftp:");
195 // special to non-special
196 testSetProtocol(new URL("https://example.org"), "foo", "https:");
197 // non-special to special
198 testSetProtocol(new URL("fish://example.org"), "https", "fish:");
199
200 // Search
201 myURL = new URL("https://example.org/abc?123");
202 myURL.search = "abc=xyz";
203 assert.sameValue(myURL.href, "https://example.org/abc?abc=xyz");
204
205 myURL.search = "a=1 2";
206 assert.sameValue(myURL.href, "https://example.org/abc?a=1%202");
207
208 myURL.search = "á=ú";
209 assert.sameValue(myURL.search, "?%C3%A1=%C3%BA");
210 assert.sameValue(myURL.href, "https://example.org/abc?%C3%A1=%C3%BA");
211
212 myURL.hash = "hash";
213 myURL.search = "a=#b";
214 assert.sameValue(myURL.href, "https://example.org/abc?a=%23b#hash");
215 assert.sameValue(myURL.search, "?a=%23b");
216 assert.sameValue(myURL.hash, "#hash");
217
218 // Username
219 myURL = new URL("https://abc:[email protected]/");
220 myURL.username = "123";
221 assert.sameValue(myURL.href, "https://123:[email protected]/");
222
223 // Origin, read-only
224 assert.throws(() => {
225 myURL.origin = "abc";
226 }, TypeError);
227
228 // href
229 myURL = new URL("https://example.org");
230 myURL.href = "https://example.com";
231 assert.sameValue(myURL.href, "https://example.com/");
232
233 assert.throws(() => {
234 myURL.href = "test";
235 }, TypeError);
236
237 // Search Params
238 myURL = new URL("https://example.com/");
239 myURL.searchParams.append("user", "abc");
240 assert.sameValue(myURL.toString(), "https://example.com/?user=abc");
241 myURL.searchParams.append("first", "one");
242 assert.sameValue(myURL.toString(), "https://example.com/?user=abc&first=one");
243 myURL.searchParams.delete("user");
244 assert.sameValue(myURL.toString(), "https://example.com/?first=one");
245
246 {
247 const url = require("url");
248
249 assert.sameValue(url.domainToASCII('español.com'), "xn--espaol-zwa.com");
250 assert.sameValue(url.domainToASCII('中文.com'), "xn--fiq228c.com");
251 assert.sameValue(url.domainToASCII('xn--iñvalid.com'), "");
252
253 assert.sameValue(url.domainToUnicode('xn--espaol-zwa.com'), "español.com");
254 assert.sameValue(url.domainToUnicode('xn--fiq228c.com'), "中文.com");
255 assert.sameValue(url.domainToUnicode('xn--iñvalid.com'), "");
256 }
257
258 {
259 const url = new URL("otpauth://totp");
260 url.pathname = 'domain.com Domain:[email protected]';
261 assert.sameValue(url.toString(), 'otpauth://totp/domain.com%20Domain:[email protected]');
262 }