diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-07-13 05:56:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-13 00:56:45 -0400 |
commit | 63edeb1c36bfcea278c248e8be92f7dbb75f7671 (patch) | |
tree | dbf47efb616fbe896e2c7b559f186af8150ab5e7 /cli/tests | |
parent | 4aeac64ecd82d5953bdb92916248f5f7352be4f4 (diff) |
fix(cli/js/web/url): Implement IPv4 hostname parsing (#6707)
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/url_test.ts | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/cli/tests/unit/url_test.ts b/cli/tests/unit/url_test.ts index 3e7ac6214..37d92089b 100644 --- a/cli/tests/unit/url_test.ts +++ b/cli/tests/unit/url_test.ts @@ -27,23 +27,65 @@ unitTest(function urlParsing(): void { ); }); -unitTest(function urlHostParsing(): void { +unitTest(function urlAuthenticationParsing(): void { + const specialUrl = new URL("http://foo:bar@baz"); + assertEquals(specialUrl.username, "foo"); + assertEquals(specialUrl.password, "bar"); + assertEquals(specialUrl.hostname, "baz"); + assertThrows(() => new URL("file://foo:bar@baz"), TypeError, "Invalid URL."); + const nonSpecialUrl = new URL("abcd://foo:bar@baz"); + assertEquals(nonSpecialUrl.username, "foo"); + assertEquals(nonSpecialUrl.password, "bar"); + assertEquals(nonSpecialUrl.hostname, "baz"); +}); + +unitTest(function urlHostnameParsing(): void { // IPv6. - assertEquals(new URL("https://foo:bar@[::1]:8000").hostname, "[::1]"); + assertEquals(new URL("http://[::1]").hostname, "[::1]"); + assertEquals(new URL("file://[::1]").hostname, "[::1]"); + assertEquals(new URL("abcd://[::1]").hostname, "[::1]"); // Forbidden host code point. - assertThrows(() => new URL("https:// a"), TypeError, "Invalid URL."); - assertThrows(() => new URL("abcde:// a"), TypeError, "Invalid URL."); - assertThrows(() => new URL("https://%"), TypeError, "Invalid URL."); - assertEquals(new URL("abcde://%").hostname, "%"); + assertThrows(() => new URL("http:// a"), TypeError, "Invalid URL."); + assertThrows(() => new URL("file:// a"), TypeError, "Invalid URL."); + assertThrows(() => new URL("abcd:// a"), TypeError, "Invalid URL."); + assertThrows(() => new URL("http://%"), TypeError, "Invalid URL."); + assertThrows(() => new URL("file://%"), TypeError, "Invalid URL."); + assertEquals(new URL("abcd://%").hostname, "%"); // Percent-decode. - assertEquals(new URL("https://%21").hostname, "!"); - assertEquals(new URL("abcde://%21").hostname, "%21"); + assertEquals(new URL("http://%21").hostname, "!"); + assertEquals(new URL("file://%21").hostname, "!"); + assertEquals(new URL("abcd://%21").hostname, "%21"); + + // IPv4 parsing. + assertEquals(new URL("http://260").hostname, "0.0.1.4"); + assertEquals(new URL("file://260").hostname, "0.0.1.4"); + assertEquals(new URL("abcd://260").hostname, "260"); + assertEquals(new URL("http://255.0.0.0").hostname, "255.0.0.0"); + assertThrows(() => new URL("http://256.0.0.0"), TypeError, "Invalid URL."); + assertEquals(new URL("http://0.255.0.0").hostname, "0.255.0.0"); + assertThrows(() => new URL("http://0.256.0.0"), TypeError, "Invalid URL."); + assertEquals(new URL("http://0.0.255.0").hostname, "0.0.255.0"); + assertThrows(() => new URL("http://0.0.256.0"), TypeError, "Invalid URL."); + assertEquals(new URL("http://0.0.0.255").hostname, "0.0.0.255"); + assertThrows(() => new URL("http://0.0.0.256"), TypeError, "Invalid URL."); + assertEquals(new URL("http://0.0.65535").hostname, "0.0.255.255"); + assertThrows(() => new URL("http://0.0.65536"), TypeError, "Invalid URL."); + assertEquals(new URL("http://0.16777215").hostname, "0.255.255.255"); + assertThrows(() => new URL("http://0.16777216"), TypeError, "Invalid URL."); + assertEquals(new URL("http://4294967295").hostname, "255.255.255.255"); + assertThrows(() => new URL("http://4294967296"), TypeError, "Invalid URL."); +}); - // TODO(nayeemrmn): IPv4 parsing. - // assertEquals(new URL("https://260").hostname, "0.0.1.4"); - assertEquals(new URL("abcde://260").hostname, "260"); +unitTest(function urlPortParsing(): void { + const specialUrl = new URL("http://foo:8000"); + assertEquals(specialUrl.hostname, "foo"); + assertEquals(specialUrl.port, "8000"); + assertThrows(() => new URL("file://foo:8000"), TypeError, "Invalid URL."); + const nonSpecialUrl = new URL("abcd://foo:8000"); + assertEquals(nonSpecialUrl.hostname, "foo"); + assertEquals(nonSpecialUrl.port, "8000"); }); unitTest(function urlModifications(): void { @@ -200,12 +242,18 @@ unitTest(function urlUncHostname() { }); unitTest(function urlHostnameUpperCase() { - assertEquals(new URL("https://EXAMPLE.COM").href, "https://example.com/"); - assertEquals(new URL("abcde://EXAMPLE.COM").href, "abcde://EXAMPLE.COM/"); + assertEquals(new URL("http://EXAMPLE.COM").href, "http://example.com/"); + assertEquals(new URL("abcd://EXAMPLE.COM").href, "abcd://EXAMPLE.COM"); +}); + +unitTest(function urlEmptyPath() { + assertEquals(new URL("http://foo").pathname, "/"); + assertEquals(new URL("file://foo").pathname, "/"); + assertEquals(new URL("abcd://foo").pathname, ""); }); unitTest(function urlTrim() { - assertEquals(new URL(" https://example.com ").href, "https://example.com/"); + assertEquals(new URL(" http://example.com ").href, "http://example.com/"); }); unitTest(function urlEncoding() { |