2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-11-12 13:45:48 -05:00
|
|
|
import { test, assert, assertEquals, assertThrows } from "./test_util.ts";
|
2018-12-17 20:07:47 -05:00
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlParsing(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL(
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.hash, "#qat");
|
|
|
|
assertEquals(url.host, "baz.qat:8000");
|
|
|
|
assertEquals(url.hostname, "baz.qat");
|
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
url.href,
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.origin, "https://baz.qat:8000");
|
|
|
|
assertEquals(url.password, "bar");
|
|
|
|
assertEquals(url.pathname, "/qux/quux");
|
|
|
|
assertEquals(url.port, "8000");
|
|
|
|
assertEquals(url.protocol, "https:");
|
|
|
|
assertEquals(url.search, "?foo=bar&baz=12");
|
|
|
|
assertEquals(url.searchParams.getAll("foo"), ["bar"]);
|
|
|
|
assertEquals(url.searchParams.getAll("baz"), ["12"]);
|
|
|
|
assertEquals(url.username, "foo");
|
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
String(url),
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
JSON.stringify({ key: url }),
|
|
|
|
`{"key":"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlModifications(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL(
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
|
|
|
url.hash = "";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
|
|
|
url.href,
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12"
|
|
|
|
);
|
2018-12-17 20:07:47 -05:00
|
|
|
url.host = "qat.baz:8080";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
|
|
|
url.href,
|
|
|
|
"https://foo:bar@qat.baz:8080/qux/quux?foo=bar&baz=12"
|
|
|
|
);
|
2018-12-17 20:07:47 -05:00
|
|
|
url.hostname = "foo.bar";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
|
|
|
url.href,
|
|
|
|
"https://foo:bar@foo.bar:8080/qux/quux?foo=bar&baz=12"
|
|
|
|
);
|
2018-12-17 20:07:47 -05:00
|
|
|
url.password = "qux";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
|
|
|
url.href,
|
|
|
|
"https://foo:qux@foo.bar:8080/qux/quux?foo=bar&baz=12"
|
|
|
|
);
|
2018-12-17 20:07:47 -05:00
|
|
|
url.pathname = "/foo/bar%qat";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
url.href,
|
|
|
|
"https://foo:qux@foo.bar:8080/foo/bar%qat?foo=bar&baz=12"
|
|
|
|
);
|
|
|
|
url.port = "";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "https://foo:qux@foo.bar/foo/bar%qat?foo=bar&baz=12");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.protocol = "http:";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "http://foo:qux@foo.bar/foo/bar%qat?foo=bar&baz=12");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.search = "?foo=bar&foo=baz";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "http://foo:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz");
|
|
|
|
assertEquals(url.searchParams.getAll("foo"), ["bar", "baz"]);
|
2018-12-17 20:07:47 -05:00
|
|
|
url.username = "foo@bar";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
url.href,
|
|
|
|
"http://foo%40bar:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz"
|
|
|
|
);
|
|
|
|
url.searchParams.set("bar", "qat");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
url.href,
|
|
|
|
"http://foo%40bar:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz&bar=qat"
|
|
|
|
);
|
|
|
|
url.searchParams.delete("foo");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "http://foo%40bar:qux@foo.bar/foo/bar%qat?bar=qat");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.searchParams.append("foo", "bar");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-17 20:07:47 -05:00
|
|
|
url.href,
|
|
|
|
"http://foo%40bar:qux@foo.bar/foo/bar%qat?bar=qat&foo=bar"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlModifyHref(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL("http://example.com/");
|
|
|
|
url.href = "https://foo:bar@example.com:8080/baz/qat#qux";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.protocol, "https:");
|
|
|
|
assertEquals(url.username, "foo");
|
|
|
|
assertEquals(url.password, "bar");
|
|
|
|
assertEquals(url.host, "example.com:8080");
|
|
|
|
assertEquals(url.hostname, "example.com");
|
|
|
|
assertEquals(url.pathname, "/baz/qat");
|
|
|
|
assertEquals(url.hash, "#qux");
|
2018-12-17 20:07:47 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlModifyPathname(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL("http://foo.bar/baz%qat/qux%quux");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.pathname, "/baz%qat/qux%quux");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.pathname = url.pathname;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.pathname, "/baz%qat/qux%quux");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.pathname = "baz#qat qux";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.pathname, "/baz%23qat%20qux");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.pathname = url.pathname;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.pathname, "/baz%23qat%20qux");
|
2018-12-17 20:07:47 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlModifyHash(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL("http://foo.bar");
|
|
|
|
url.hash = "%foo bar/qat%qux#bar";
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.hash, "#%foo%20bar/qat%qux#bar");
|
2018-12-17 20:07:47 -05:00
|
|
|
url.hash = url.hash;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.hash, "#%foo%20bar/qat%qux#bar");
|
2018-12-17 20:07:47 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlSearchParamsReuse(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL(
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
|
|
|
const sp = url.searchParams;
|
|
|
|
url.host = "baz.qat";
|
|
|
|
assert(sp === url.searchParams, "Search params should be reused.");
|
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlBaseURL(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const base = new URL(
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
|
|
|
const url = new URL("/foo/bar?baz=foo#qux", base);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
|
2018-12-17 20:07:47 -05:00
|
|
|
});
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
test(function urlBaseString(): void {
|
2018-12-17 20:07:47 -05:00
|
|
|
const url = new URL(
|
|
|
|
"/foo/bar?baz=foo#qux",
|
|
|
|
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
|
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
|
2018-12-17 20:07:47 -05:00
|
|
|
});
|
2019-04-29 19:45:20 -04:00
|
|
|
|
2019-09-05 20:01:27 -04:00
|
|
|
test(function urlRelativeWithBase(): void {
|
|
|
|
assertEquals(new URL("", "file:///a/a/a").href, "file:///a/a/a");
|
|
|
|
assertEquals(new URL(".", "file:///a/a/a").href, "file:///a/a/");
|
|
|
|
assertEquals(new URL("..", "file:///a/a/a").href, "file:///a/");
|
|
|
|
assertEquals(new URL("b", "file:///a/a/a").href, "file:///a/a/b");
|
|
|
|
assertEquals(new URL("b", "file:///a/a/a/").href, "file:///a/a/a/b");
|
|
|
|
assertEquals(new URL("b/", "file:///a/a/a").href, "file:///a/a/b/");
|
|
|
|
assertEquals(new URL("../b", "file:///a/a/a").href, "file:///a/b");
|
|
|
|
});
|
|
|
|
|
2019-09-11 16:20:54 -04:00
|
|
|
test(function emptyBasePath(): void {
|
|
|
|
assertEquals(new URL("", "http://example.com").href, "http://example.com/");
|
|
|
|
});
|
|
|
|
|
2019-04-29 19:45:20 -04:00
|
|
|
test(function deletingAllParamsRemovesQuestionMarkFromURL(): void {
|
|
|
|
const url = new URL("http://example.com/?param1¶m2");
|
|
|
|
url.searchParams.delete("param1");
|
|
|
|
url.searchParams.delete("param2");
|
|
|
|
assertEquals(url.href, "http://example.com/");
|
|
|
|
assertEquals(url.search, "");
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function removingNonExistentParamRemovesQuestionMarkFromURL(): void {
|
|
|
|
const url = new URL("http://example.com/?");
|
|
|
|
assertEquals(url.href, "http://example.com/?");
|
|
|
|
url.searchParams.delete("param1");
|
|
|
|
assertEquals(url.href, "http://example.com/");
|
|
|
|
assertEquals(url.search, "");
|
|
|
|
});
|
2019-06-10 23:55:38 -04:00
|
|
|
|
|
|
|
test(function sortingNonExistentParamRemovesQuestionMarkFromURL(): void {
|
|
|
|
const url = new URL("http://example.com/?");
|
|
|
|
assertEquals(url.href, "http://example.com/?");
|
|
|
|
url.searchParams.sort();
|
|
|
|
assertEquals(url.href, "http://example.com/");
|
|
|
|
assertEquals(url.search, "");
|
|
|
|
});
|
2019-10-31 14:55:54 -04:00
|
|
|
|
|
|
|
test(function customInspectFunction(): void {
|
|
|
|
const url = new URL("http://example.com/?");
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(url),
|
|
|
|
'URL { href: "http://example.com/?", origin: "http://example.com", protocol: "http:", username: "", password: "", host: "example.com", hostname: "example.com", port: "", pathname: "/", hash: "", search: "?" }'
|
|
|
|
);
|
|
|
|
});
|
2019-11-12 13:45:48 -05:00
|
|
|
|
|
|
|
test(function protocolNotHttpOrFile() {
|
|
|
|
const url = new URL("about:blank");
|
|
|
|
assertEquals(url.href, "about:blank");
|
|
|
|
assertEquals(url.protocol, "about:");
|
|
|
|
assertEquals(url.origin, "null");
|
|
|
|
});
|
|
|
|
|
|
|
|
test(function createBadUrl(): void {
|
|
|
|
assertThrows(() => {
|
|
|
|
new URL("0.0.0.0:8080");
|
|
|
|
});
|
|
|
|
});
|