url_test.js 7.1 KB

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