-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURLSuite.js
154 lines (137 loc) · 6.45 KB
/
URLSuite.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var URL = require("../../source/URL.js");
var expect = require("expect.js");
describe("URL", function() {
var candidates = {
valid: [
"http://foo.bar-baz.com:80/p/a/t/h?q=u&e&r=y#fragment",
"http://host:80/p/a/t/h?q=u&e&r=y#fragment",
"/p/a/t/h?q=u&e&r=y#fragment",
"https://host/p/a/t/h/?q=Earth",
"http://fr.wikipedia.org/wiki/Sp%C3%A9cial:Page_au_hasard",
"//en.wikipedia.org/wiki/URI_scheme#Generic_syntax",
"redis://undefined:password@localhost:6379/0",
"http://[email protected]/#/house",
{protocol: "http", "host": "server"},
{host: "server"},
{path: "/from/root"},
{query: "A=1"},
"file://host/path/to/remote/file",
"file:///path/to/local/file",
{protocol: "file", "path": "/path/to/local/file"}
],
proofs: {
parts: [
{protocol: "http", host: "foo.bar-baz.com", port: 80, path: "/p/a/t/h", query: {q: "u", e: undefined, r: "y"}, fragment: "fragment"},
{protocol: "http", host: "host", port: 80, path: "/p/a/t/h", query: {q: "u", e: undefined, r: "y"}, fragment: "fragment"},
{path: "/p/a/t/h", query: {q: "u", e: undefined, r: "y"}, fragment: "fragment"},
{protocol: "https", host: "host", path: "/p/a/t/h/", query: {q: "Earth"}},
{protocol: "http", host: "fr.wikipedia.org", path: "/wiki/Sp%C3%A9cial:Page_au_hasard"},
{host: "en.wikipedia.org", path: "/wiki/URI_scheme", fragment: "Generic_syntax"},
{protocol: "redis", user: "undefined", password: "password", host: "localhost", port: 6379, path: "/0"},
{protocol: "http", user: "mickey", host: "disney.com", path: "/", fragment: "/house"},
{protocol: "http", host: "server", path: "/"},
{host: "server", path: "/"},
{path: "/from/root"},
{path: "/", query: {A: 1}},
{protocol: "file", host: "host", path: "/path/to/remote/file"},
{protocol: "file", path: "/path/to/local/file"},
{protocol: "file", "path": "/path/to/local/file"}
],
representations: [
"http://foo.bar-baz.com/p/a/t/h?q=u&e&r=y#fragment",
"http://host/p/a/t/h?q=u&e&r=y#fragment",
"/p/a/t/h?q=u&e&r=y#fragment",
"https://host/p/a/t/h/?q=Earth",
"http://fr.wikipedia.org/wiki/Sp%C3%A9cial:Page_au_hasard",
"//en.wikipedia.org/wiki/URI_scheme#Generic_syntax",
"redis://undefined:password@localhost:6379/0",
"http://[email protected]/#/house",
"http://server/",
"//server/",
"/from/root",
"/?A=1",
"file://host/path/to/remote/file",
"file:///path/to/local/file",
"file:///path/to/local/file"
]
},
invalid: [
"",
"123",
"?&#",
"abc.xyz",
"://host:80/p/a/t/h?q=u&e&r=y#fragment",
"http://:port/p/a/t/h?q=u&e&r=y#fragment",
"file:/whatever",
"///what",
{protocol: "http"},
{user: "whoever"},
{password: "***"},
{user: "whoever", password: "***"},
{port: 8888}
]
};
it("should respect basic generic syntax of RFC 3986", function() {
candidates["valid"].forEach(function(candidate, index) {
var locator = undefined;
expect(function() {
locator = new URL(candidate);
}).not.to.throwError(function(error) { console.error(error.stack); });
expect(locator).to.eql(candidates.proofs.parts[index]);
expect(String(locator)).to.eql(candidates.proofs.representations[index]);
});
candidates["invalid"].forEach(function(candidate) {
expect(function() {
console.log(String(new URL(candidate)));
}).to.throwError();
});
});
it("should be the same as what's provided to the constructor when not overridden nor completed", function() {
var locator = "http://localhost:1234/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1";
expect(String(new URL(locator))).to.be(locator);
});
it("could be completed with options", function() {
var locator = "/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1";
expect(String(new URL(locator, {protocol: "http"}))).to.be("/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {host: "candy.com"}))).to.be("//candy.com/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {
protocol: "https",
host: "candy.com"
}))).to.be("https://candy.com/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {query: {user: "Mickey"}}))).to.eql("/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre&user=Mickey#items?page=1");
expect(String(new URL(locator, {query: "user=Mickey"}))).to.eql("/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre&user=Mickey#items?page=1");
});
it("could be overridden by options", function() {
var locator = "http://localhost:1234/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1";
expect(String(new URL(locator, {protocol: null}, true))).to.be("//localhost:1234/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {host: null}, true))).to.be("/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {port: null}, true))).to.be("http://localhost/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {path: null}, true))).to.eql("http://localhost:1234/?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(String(new URL(locator, {query: null}, true))).to.be("http://localhost:1234/abc/def/ghi.xyz#items?page=1");
expect(String(new URL(locator, {query: {user: "Mickey"}}, true))).to.be("http://localhost:1234/abc/def/ghi.xyz?user=Mickey#items?page=1");
expect(String(new URL(locator, {fragment: null}, true))).to.be("http://localhost:1234/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre");
expect(String(new URL(locator, {
query: null,
fragment: null
}, true))).to.be("http://localhost:1234/abc/def/ghi.xyz");
});
it("should allow query manipulation", function() {
expect(new URL("/thing").query).to.be(undefined);
var locator = new URL("file://localhost:1234/abc/def/ghi.xyz?X=1&Y&Z=3&odd=bizarre#items?page=1");
expect(locator.query).to.eql({X: "1", Y: undefined, Z: "3", odd: "bizarre"});
var query = locator.query;
delete query["Y"];
expect(locator.query).to.eql({X: "1", Z: "3", odd: "bizarre"});
query["user"] = "Mickey";
expect(locator.query).to.eql({X: "1", Z: "3", odd: "bizarre", user: "Mickey"});
});
it("should provide access to common parts", function() {
var locator = new URL("file://localhost:1234/abc/def/ghi.xyz?user=Mickey#items?page=1");
expect(locator.protocol).to.be("file");
expect(locator.host).to.be("localhost");
expect(locator.port).to.be(1234);
expect(locator.path).to.be("/abc/def/ghi.xyz");
expect(locator.query).to.eql({user: "Mickey"});
expect(locator.fragment).to.be("items?page=1");
});
});