1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual } from "./test_util.ts";
test(function urlParsing() {
const url = new URL(
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
);
assertEqual(url.hash, "#qat");
assertEqual(url.host, "baz.qat:8000");
assertEqual(url.hostname, "baz.qat");
assertEqual(
url.href,
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
);
assertEqual(url.origin, "https://baz.qat:8000");
assertEqual(url.password, "bar");
assertEqual(url.pathname, "/qux/quux");
assertEqual(url.port, "8000");
assertEqual(url.protocol, "https:");
assertEqual(url.search, "?foo=bar&baz=12");
assertEqual(url.searchParams.getAll("foo"), ["bar"]);
assertEqual(url.searchParams.getAll("baz"), ["12"]);
assertEqual(url.username, "foo");
assertEqual(
String(url),
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
);
assertEqual(
JSON.stringify({ key: url }),
`{"key":"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"}`
);
});
test(function urlModifications() {
const url = new URL(
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
);
url.hash = "";
assertEqual(url.href, "https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12");
url.host = "qat.baz:8080";
assertEqual(url.href, "https://foo:bar@qat.baz:8080/qux/quux?foo=bar&baz=12");
url.hostname = "foo.bar";
assertEqual(url.href, "https://foo:bar@foo.bar:8080/qux/quux?foo=bar&baz=12");
url.password = "qux";
assertEqual(url.href, "https://foo:qux@foo.bar:8080/qux/quux?foo=bar&baz=12");
url.pathname = "/foo/bar%qat";
assertEqual(
url.href,
"https://foo:qux@foo.bar:8080/foo/bar%qat?foo=bar&baz=12"
);
url.port = "";
assertEqual(url.href, "https://foo:qux@foo.bar/foo/bar%qat?foo=bar&baz=12");
url.protocol = "http:";
assertEqual(url.href, "http://foo:qux@foo.bar/foo/bar%qat?foo=bar&baz=12");
url.search = "?foo=bar&foo=baz";
assertEqual(url.href, "http://foo:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz");
assertEqual(url.searchParams.getAll("foo"), ["bar", "baz"]);
url.username = "foo@bar";
assertEqual(
url.href,
"http://foo%40bar:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz"
);
url.searchParams.set("bar", "qat");
assertEqual(
url.href,
"http://foo%40bar:qux@foo.bar/foo/bar%qat?foo=bar&foo=baz&bar=qat"
);
url.searchParams.delete("foo");
assertEqual(url.href, "http://foo%40bar:qux@foo.bar/foo/bar%qat?bar=qat");
url.searchParams.append("foo", "bar");
assertEqual(
url.href,
"http://foo%40bar:qux@foo.bar/foo/bar%qat?bar=qat&foo=bar"
);
});
test(function urlModifyHref() {
const url = new URL("http://example.com/");
url.href = "https://foo:bar@example.com:8080/baz/qat#qux";
assertEqual(url.protocol, "https:");
assertEqual(url.username, "foo");
assertEqual(url.password, "bar");
assertEqual(url.host, "example.com:8080");
assertEqual(url.hostname, "example.com");
assertEqual(url.pathname, "/baz/qat");
assertEqual(url.hash, "#qux");
});
test(function urlModifyPathname() {
const url = new URL("http://foo.bar/baz%qat/qux%quux");
assertEqual(url.pathname, "/baz%qat/qux%quux");
url.pathname = url.pathname;
assertEqual(url.pathname, "/baz%qat/qux%quux");
url.pathname = "baz#qat qux";
assertEqual(url.pathname, "/baz%23qat%20qux");
url.pathname = url.pathname;
assertEqual(url.pathname, "/baz%23qat%20qux");
});
test(function urlModifyHash() {
const url = new URL("http://foo.bar");
url.hash = "%foo bar/qat%qux#bar";
assertEqual(url.hash, "#%foo%20bar/qat%qux#bar");
url.hash = url.hash;
assertEqual(url.hash, "#%foo%20bar/qat%qux#bar");
});
test(function urlSearchParamsReuse() {
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.");
});
test(function urlBaseURL() {
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);
assertEqual(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
});
test(function urlBaseString() {
const url = new URL(
"/foo/bar?baz=foo#qux",
"https://foo:bar@baz.qat:8000/qux/quux?foo=bar&baz=12#qat"
);
assertEqual(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
});
|