test_url.html (15424B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Test URL API</title> 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <body> 10 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=887364">Mozilla Bug 887364</a> 11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=991471">Mozilla Bug 991471</a> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=996055">Mozilla Bug 996055</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 <iframe name="x" id="x"></iframe> 16 <iframe name="y" id="y"></iframe> 17 </div> 18 <pre id="test"> 19 </pre> 20 <script> 21 22 /** Test for Bug 887364 */ 23 ok("URL" in window, "window.URL exists"); 24 25 var tests = [ 26 { url: "http://www.abc.com", 27 base: undefined, 28 error: false, 29 href: "http://www.abc.com/", 30 origin: "http://www.abc.com", 31 protocol: "http:", 32 username: "", 33 password: "", 34 host: "www.abc.com", 35 hostname: "www.abc.com", 36 port: "", 37 pathname: "/", 38 search: "", 39 hash: "", 40 }, 41 { url: "ftp://auser:apw@www.abc.com", 42 base: undefined, 43 error: false, 44 href: "ftp://auser:apw@www.abc.com/", 45 origin: "ftp://www.abc.com", 46 protocol: "ftp:", 47 username: "auser", 48 password: "apw", 49 host: "www.abc.com", 50 hostname: "www.abc.com", 51 port: "", 52 pathname: "/", 53 search: "", 54 hash: "", 55 }, 56 { url: "http://www.abc.com:90/apath/", 57 base: undefined, 58 error: false, 59 href: "http://www.abc.com:90/apath/", 60 origin: "http://www.abc.com:90", 61 protocol: "http:", 62 username: "", 63 password: "", 64 host: "www.abc.com:90", 65 hostname: "www.abc.com", 66 port: "90", 67 pathname: "/apath/", 68 search: "", 69 hash: "", 70 }, 71 { url: "http://www.abc.com/apath/afile.txt#ahash", 72 base: undefined, 73 error: false, 74 href: "http://www.abc.com/apath/afile.txt#ahash", 75 origin: "http://www.abc.com", 76 protocol: "http:", 77 username: "", 78 password: "", 79 host: "www.abc.com", 80 hostname: "www.abc.com", 81 port: "", 82 pathname: "/apath/afile.txt", 83 search: "", 84 hash: "#ahash", 85 }, 86 { url: "http://example.com/?test#hash", 87 base: undefined, 88 error: false, 89 href: "http://example.com/?test#hash", 90 origin: "http://example.com", 91 protocol: "http:", 92 username: "", 93 password: "", 94 host: "example.com", 95 hostname: "example.com", 96 port: "", 97 pathname: "/", 98 search: "?test", 99 hash: "#hash", 100 }, 101 { url: "http://example.com/?test", 102 base: undefined, 103 error: false, 104 href: "http://example.com/?test", 105 origin: "http://example.com", 106 protocol: "http:", 107 username: "", 108 password: "", 109 host: "example.com", 110 hostname: "example.com", 111 port: "", 112 pathname: "/", 113 search: "?test", 114 hash: "", 115 }, 116 { url: "http://example.com/carrot#question%3f", 117 base: undefined, 118 error: false, 119 hash: "#question%3f", 120 }, 121 { url: "https://example.com:4443?", 122 base: undefined, 123 error: false, 124 protocol: "https:", 125 port: "4443", 126 pathname: "/", 127 hash: "", 128 search: "", 129 }, 130 { url: "http://www.abc.com/apath/afile.txt#ahash?asearch", 131 base: undefined, 132 error: false, 133 href: "http://www.abc.com/apath/afile.txt#ahash?asearch", 134 protocol: "http:", 135 pathname: "/apath/afile.txt", 136 hash: "#ahash?asearch", 137 search: "", 138 }, 139 { url: "http://www.abc.com/apath/afile.txt?asearch#ahash", 140 base: undefined, 141 error: false, 142 href: "http://www.abc.com/apath/afile.txt?asearch#ahash", 143 protocol: "http:", 144 pathname: "/apath/afile.txt", 145 hash: "#ahash", 146 search: "?asearch", 147 }, 148 { url: "http://abc.com/apath/afile.txt?#ahash", 149 base: undefined, 150 error: false, 151 pathname: "/apath/afile.txt", 152 hash: "#ahash", 153 search: "", 154 }, 155 { url: "http://auser:apassword@www.abc.com:90/apath/afile.txt?asearch#ahash", 156 base: undefined, 157 error: false, 158 protocol: "http:", 159 username: "auser", 160 password: "apassword", 161 host: "www.abc.com:90", 162 hostname: "www.abc.com", 163 port: "90", 164 pathname: "/apath/afile.txt", 165 hash: "#ahash", 166 search: "?asearch", 167 origin: "http://www.abc.com:90", 168 }, 169 170 { url: "/foo#bar", 171 base: "www.test.org", 172 error: true, 173 }, 174 { url: "/foo#bar", 175 base: null, 176 error: true, 177 }, 178 { url: "/foo#bar", 179 base: 42, 180 error: true, 181 }, 182 { url: "ftp://ftp.something.net", 183 base: undefined, 184 error: false, 185 protocol: "ftp:", 186 }, 187 { url: "file:///tmp/file", 188 base: undefined, 189 error: false, 190 protocol: "file:", 191 }, 192 { url: "gopher://gopher.something.net", 193 base: undefined, 194 error: false, 195 protocol: "gopher:", 196 expectedChangedProtocol: "https:", 197 }, 198 { url: "ws://ws.something.net", 199 base: undefined, 200 error: false, 201 protocol: "ws:", 202 }, 203 { url: "wss://ws.something.net", 204 base: undefined, 205 error: false, 206 protocol: "wss:", 207 }, 208 { url: "foo://foo.something.net", 209 base: undefined, 210 error: false, 211 protocol: "foo:", 212 expectedChangedProtocol: "https:", 213 }, 214 215 { url: "about:blank", 216 base: undefined, 217 error: false, 218 protocol: "about:", 219 pathname: "blank", 220 skip_setters: false, 221 }, 222 223 { url: "foo:bar?what#yeah", 224 base: undefined, 225 error: false, 226 protocol: "foo:", 227 pathname: "bar", 228 search: "?what", 229 hash: "#yeah", 230 skip_setters: false, 231 }, 232 233 { url: "http://sub2.xn--lt-uia.mochi.test:8888/foo", 234 base: undefined, 235 error: false, 236 href: "http://sub2.xn--lt-uia.mochi.test:8888/foo", 237 origin: "http://sub2.xn--lt-uia.mochi.test:8888", 238 protocol: "http:", 239 username: "", 240 password: "", 241 host: "sub2.xn--lt-uia.mochi.test:8888", 242 hostname: "sub2.xn--lt-uia.mochi.test", 243 port: "8888", 244 pathname: "/foo", 245 search: "", 246 hash: "", 247 }, 248 { url: "http://sub2.ält.mochi.test:8888/foo", 249 base: undefined, 250 error: false, 251 href: "http://sub2.xn--lt-uia.mochi.test:8888/foo", 252 origin: "http://sub2.xn--lt-uia.mochi.test:8888", 253 protocol: "http:", 254 username: "", 255 password: "", 256 host: "sub2.xn--lt-uia.mochi.test:8888", 257 hostname: "sub2.xn--lt-uia.mochi.test", 258 port: "8888", 259 pathname: "/foo", 260 search: "", 261 hash: "", 262 }, 263 ]; 264 265 while (tests.length) { 266 var test = tests.shift(); 267 268 var error = false; 269 var url; 270 try { 271 if (test.base) { 272 url = new URL(test.url, test.base); 273 } else { 274 url = new URL(test.url); 275 } 276 } catch (e) { 277 error = true; 278 } 279 280 is(test.error, error, "Error creating URL"); 281 if (test.error) { 282 continue; 283 } 284 285 if ("href" in test) is(url.href, test.href, "href"); 286 if ("origin" in test) is(url.origin, test.origin, "origin"); 287 if ("protocol" in test) is(url.protocol, test.protocol, "protocol"); 288 if ("username" in test) is(url.username, test.username, "username"); 289 if ("password" in test) is(url.password, test.password, "password"); 290 if ("host" in test) is(url.host, test.host, "host"); 291 if ("hostname" in test) is(url.hostname, test.hostname, "hostname"); 292 if ("port" in test) is(url.port, test.port, "port"); 293 if ("pathname" in test) is(url.pathname, test.pathname, "pathname"); 294 if ("search" in test) is(url.search, test.search, "search"); 295 if ("hash" in test) is(url.hash, test.hash, "hash"); 296 297 { 298 let uri = SpecialPowers.wrap(url).URI; 299 is(url.href, uri.spec, "Conversion to nsIURI"); 300 is(SpecialPowers.wrap(URL).fromURI(uri).href, uri.spec, "Conversion from nsIURI"); 301 } 302 303 if ("skip_setters" in test && test.skip_setters === false) { 304 info("Skip setter methods for URL: " + test); 305 continue; 306 } 307 308 url = new URL("https://www.example.net/what#foo?bar"); 309 ok(url, "Url exists!"); 310 311 if ("href" in test) url.href = test.href; 312 if ("protocol" in test) url.protocol = test.protocol; 313 if ("username" in test && test.username) url.username = test.username; 314 if ("password" in test && test.password) url.password = test.password; 315 if ("host" in test) url.host = test.host; 316 if ("hostname" in test) url.hostname = test.hostname; 317 if ("port" in test) url.port = test.port; 318 if ("pathname" in test) url.pathname = test.pathname; 319 if ("search" in test) url.search = test.search; 320 if ("hash" in test) url.hash = test.hash; 321 322 if ("href" in test) is(url.href, test.href, "href"); 323 if ("origin" in test) is(url.origin, test.origin, "origin"); 324 if ("expectedChangedProtocol" in test) is(url.protocol, test.expectedChangedProtocol, "protocol"); 325 else if ("protocol" in test) is(url.protocol, test.protocol, "protocol"); 326 if ("username" in test) is(url.username, test.username, "username"); 327 if ("password" in test) is(url.password, test.password, "password"); 328 if ("host" in test) is(url.host, test.host, "host"); 329 if ("hostname" in test) is(test.hostname, url.hostname, "hostname"); 330 if ("port" in test) is(test.port, url.port, "port"); 331 if ("pathname" in test) is(test.pathname, url.pathname, "pathname"); 332 if ("search" in test) is(test.search, url.search, "search"); 333 if ("hash" in test) is(test.hash, url.hash, "hash"); 334 335 if ("href" in test) is(test.href, url + "", "stringify works"); 336 } 337 338 </script> 339 340 <script> 341 /** Test for Bug 991471 */ 342 var url = new URL("http://localhost/"); 343 url.hostname = ""; 344 url.username = "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"; 345 url.hostname = "www.mozilla.org"; 346 url.username = ""; 347 url.hostname = "www.mozilla.org"; 348 is(url.href, "http://www.mozilla.org/", "No parsing error with empty host"); 349 </script> 350 351 <script> 352 /** Test for Bug 996055 */ 353 var url = new URL("http://localhost/"); 354 url.hostname = ""; 355 is(url.href, "http://localhost/", "Empty hostname is ignored"); 356 </script> 357 358 <script> 359 /** Test for Bug 960014 */ 360 var url = new URL("http://localhost/"); 361 url.hostname = "[2001::1]"; 362 is(url.hostname, "[2001::1]", "IPv6 hostname"); 363 is(url.href, "http://[2001::1]/"); 364 365 url.hostname = "[::192.9.5.5]"; 366 is(url.hostname, "[::c009:505]", "IPv6 hostname"); 367 is(url.href, "http://[::c009:505]/"); 368 369 url = new URL("http://localhost/"); 370 url.hostname = "[::]"; 371 is(url.hostname, "[::]", "IPv6 hostname"); 372 373 url = new URL("http://localhost/"); 374 url.host = "[2001::1]:30"; 375 is(url.hostname, "[2001::1]", "IPv6 hostname"); 376 is(url.port, "30", "Port"); 377 is(url.host, "[2001::1]:30", "IPv6 host"); 378 379 url = new URL("http://localhost/"); 380 // This should silently fail since it's missing the brackets 381 url.hostname = "2001::1"; 382 is(url.hostname, "localhost", "Setting bad hostname fails"); 383 </script> 384 385 <script> 386 var blob = new Blob(["a"]); 387 var url = URL.createObjectURL(blob); 388 389 var u = new URL(url); 390 is(u.origin, location.origin, "The URL generated from a blob URI has an origin"); 391 </script> 392 393 <script> 394 var blob = new Blob(["a"]); 395 var url = URL.createObjectURL(blob); 396 397 var a = document.createElement("A"); 398 a.href = url; 399 is(a.origin, location.origin, "The 'a' element has the correct origin"); 400 </script> 401 402 <script> 403 var blob = new Blob(["a"]); 404 var url = URL.createObjectURL(blob); 405 URL.revokeObjectURL(url); 406 URL.revokeObjectURL(url); 407 ok(true, "Calling revokeObjectURL twice should be ok"); 408 </script> 409 410 <script> 411 URL.revokeObjectURL("blob:something"); 412 ok(true, "This should not throw."); 413 </script> 414 415 <script> 416 var base = new URL("http:\\\\test.com\\path/to\\file?query\\backslash#hash\\"); 417 is(base.href, "http://test.com/path/to/file?query\\backslash#hash\\"); 418 419 var url = new URL("..\\", base); 420 is(url.href, "http://test.com/path/"); 421 422 url = new URL("\\test", base); 423 is(url.href, "http://test.com/test"); 424 425 url = new URL("\\test\\", base); 426 is(url.href, "http://test.com/test/"); 427 428 url = new URL("http://example.org/test", base); 429 is(url.href, "http://example.org/test"); 430 431 url = new URL("ftp://tmp/test", base); 432 is(url.href, "ftp://tmp/test"); 433 434 url = new URL("ftp:\\\\tmp\\test", base); 435 is(url.href, "ftp://tmp/test"); 436 </script> 437 438 <script> 439 /** Test for Bug 1275746 */ 440 SimpleTest.doesThrow(() => { new URL("http:"); }, "http: is not a valid URL"); 441 SimpleTest.doesThrow(() => { new URL("http:///"); }, "http: is not a valid URL"); 442 443 var url = new URL("file:"); 444 is(url.href, "file:///", "Parsing file: should work."); 445 446 url = new URL("file:///"); 447 is(url.href, "file:///", "Parsing file:/// should work."); 448 </script> 449 450 <script> 451 async function testStrippingAndEncoding() { 452 await SpecialPowers.pushPrefEnv({ set: [ 453 ["network.url.useDefaultURI", true], 454 ]}); 455 456 var url = new URL("scheme:path/to/file?query#hash"); 457 is(url.href, "scheme:path/to/file?query#hash"); 458 is(url.pathname, "path/to/file"); 459 is(url.search, "?query"); 460 is(url.hash, "#hash"); 461 462 // pathname cannot be overwritten. 463 url.pathname = "new/path?newquery#newhash"; 464 is(url.href, "scheme:path/to/file?query#hash"); 465 466 // don't escape '#' until we implement a spec-compliant parser. 467 url.search = "?newquery#newhash"; 468 is(url.href, "scheme:path/to/file?newquery%23newhash#hash"); 469 470 // nulls get encoded, whitespace gets stripped 471 url = new URL("scheme:pa\0\nth/to/fi\0\nle?qu\0\nery#ha\0\nsh"); 472 is(url.href, "scheme:pa%00th/to/fi%00le?qu%00ery#ha%00sh"); 473 474 url.search = "new\0\nquery"; 475 is(url.href, "scheme:pa%00th/to/fi%00le?new%00query#ha%00sh"); 476 url.hash = "new\0\nhash"; 477 is(url.href, "scheme:pa%00th/to/fi%00le?new%00query#new%00hash"); 478 479 url = new URL("scheme:path#hash"); 480 is(url.href, "scheme:path#hash"); 481 url.search = "query"; 482 is(url.href, "scheme:path?query#hash"); 483 url.hash = ""; 484 is(url.href, "scheme:path?query"); 485 url.hash = "newhash"; 486 is(url.href, "scheme:path?query#newhash"); 487 url.search = ""; 488 is(url.href, "scheme:path#newhash"); 489 490 // we don't implement a spec-compliant parser yet. 491 // make sure we are bug compatible with existing implementations. 492 url = new URL("data:text/html,<a href=\"http://example.org/?q\">Link</a>"); 493 is(url.href, "data:text/html,<a href=\"http://example.org/?q\">Link</a>"); 494 495 SimpleTest.finish(); 496 }; 497 SimpleTest.waitForExplicitFinish(); 498 testStrippingAndEncoding(); 499 </script> 500 501 <script> 502 var u = new URL("http://www.example.org"); 503 is(u.toJSON(), "http://www.example.org/", "URL.toJSON()"); 504 is(JSON.stringify(u), "\"http://www.example.org/\"", "JSON.stringify(u) works"); 505 </script> 506 507 <script> 508 // bug 1648493 509 var url = new URL("http://example.com/"); 510 url.protocol = 'file' 511 url.protocol = 'resource' 512 url.password = 'ê' 513 url.username = 'ç' 514 url.protocol = 't' 515 </script> 516 </body> 517 </html>