forked from felixhayashi/jsurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
134 lines (125 loc) · 5.8 KB
/
test.html
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
<!doctype html>
<html>
<head>
<script src="url.min.js"></script>
</head>
<body>
<script>
function assertEqual( a, b, name) {
if (a == b) {
document.write( '<i style="color:green">' + name + " — Passed</i> ( '" + a + "' == '" + b + "')<br>");
}
else {
document.write( '<b style="color:red">' + name + "— Failed</b>( '" + a + "' != '" + b + "')<br>");
}
};
// full URL
document.write( "<h4>Testing full URL</h4>");
var str = 'wss://user:pass@example.com:9999/some/path.html?foo=bar#anchor';
document.write('<p>Source URL = \'' + str + '\'</p>');
var u = new Url( str);
assertEqual( u.protocol, "wss", "Parse protocol");
assertEqual( u.user, "user", "Parse user");
assertEqual( u.pass, "pass", "Parse password");
assertEqual( u.host, "example.com", "Parse host");
assertEqual( u.port, "9999", "Parse port");
assertEqual( u.path, "/some/path.html", "Parse path");
assertEqual( u.query, "foo=bar", "Parse query");
assertEqual( u.query.foo, "bar", "Parse query param");
assertEqual( u.hash, "anchor", "Parse anchor");
assertEqual( str, u.toString(), "Stringify URL");
// relative-absolute URL
document.write("<h4>Testing relative-absolute URL</h4>");
var str = '/some/path.html?foo=bar#anchor';
document.write('<p>Source URL = \'' + str + '\'</p>');
var u = new Url( str), du = new Url( document.location.href);
assertEqual( u.protocol, du.protocol, "Parse protocol");
assertEqual( u.user, du.user, "Parse user");
assertEqual( u.pass, du.pass, "Parse password");
assertEqual( u.host, du.host, "Parse host");
assertEqual( u.port, du.port, "Parse port");
assertEqual( u.path, "/some/path.html", "Parse path");
assertEqual( u.query, "foo=bar", "Parse query");
assertEqual( u.query.foo, "bar", "Parse query param");
assertEqual( u.hash, "anchor", "Parse anchor");
assertEqual(
(du.protocol && (du.protocol + '://')) +
(du.user && (du.user + (du.pass && (':' + du.pass)) + '@')) +
(du.host && du.host) +
(du.port && (':' + du.port)) + str,
u.toString(), "Stringify URL"
);
// relative URL
document.write( "<h4>Testing relative URL</h4>");
var str = 'some/path.html?foo=bar#anchor';
document.write('<p>Source URL = \'' + str + '\'</p>');
var base = document.location.href.match(/(.*\/)/)[0];
var u = new Url( str), du = new Url( document.location.href);
assertEqual( u.protocol, du.protocol, "Parse protocol");
assertEqual( u.user, du.user, "Parse user");
assertEqual( u.pass, du.pass, "Parse password");
assertEqual( u.host, du.host, "Parse host");
assertEqual( u.port, du.port, "Parse port");
assertEqual( u.path, base.replace( /.*?\/\/.*?\//, '/') + "some/path.html", "Parse path");
assertEqual( u.query, "foo=bar", "Parse query");
assertEqual( u.query.foo, "bar", "Parse query param");
assertEqual( u.hash, "anchor", "Parse anchor");
assertEqual(
base + str,
u.toString(), "Stringify URL"
);
// relative URL with upwards
var dirlen = document.location.href.match(/(.*\/)/)[0].replace( /.*?\/\/.*?\//, '/').split( '/').length - 2;
document.write( "<h4>Testing relative URL with upwards in path</h4>");
var str = '../some/path.html?foo=bar#anchor';
document.write('<p>Source URL = \'' + str + '\'</p>');
var base = document.location.href.match(/(.*\/)/)[0].replace( /[^/]+?\/$/, '');
var u = new Url( str), du = new Url( document.location.href);
assertEqual( u.protocol, du.protocol, "Parse protocol");
assertEqual( u.user, du.user, "Parse user");
assertEqual( u.pass, du.pass, "Parse password");
assertEqual( u.host, du.host, "Parse host");
assertEqual( u.port, du.port, "Parse port");
assertEqual( u.path, base.replace( /.*?\/\/(.*?\/)?/, '/') + "some/path.html", "Parse path");
assertEqual( u.query, "foo=bar", "Parse query");
assertEqual( u.query.foo, "bar", "Parse query param");
assertEqual( u.hash, "anchor", "Parse anchor");
assertEqual(
base + (!dirlen ? document.location.hostname + '/' : '') + 'some/path.html?foo=bar#anchor',
u.toString(), "Stringify URL"
);
// relative URL with upwards out of base path
var dirlen = document.location.href.match(/(.*\/)/)[0].replace( /.*?\/\/.*?\//, '/').split( '/').length + 1;
document.write( "<h4>Testing relative URL with upwards out of base path</h4>");
var str = (new Array( dirlen).join( "../")) + 'some/path.html?foo=bar#anchor';
document.write('<p>Source URL = \'' + str + '\'</p>');
var base = document.location.href.match(/(.*\/)/)[0].replace( /([^/]+?\/){0,20}$/, '').replace( /\/\/\//, '//');
var u = new Url( str), du = new Url( document.location.href);
assertEqual( u.protocol, du.protocol, "Parse protocol");
assertEqual( u.user, du.user, "Parse user");
assertEqual( u.pass, du.pass, "Parse password");
assertEqual( u.host, du.host, "Parse host");
assertEqual( u.port, du.port, "Parse port");
assertEqual( u.path, base.replace( /.*?\/\/(.*?\/)?/, '/') + "some/path.html", "Parse path");
assertEqual( u.query, "foo=bar", "Parse query");
assertEqual( u.query.foo, "bar", "Parse query param");
assertEqual( u.hash, "anchor", "Parse anchor");
assertEqual(
base + document.location.host + '/' + 'some/path.html?foo=bar#anchor',
u.toString(), "Stringify URL"
);
// clear query
var url = new Url( 'http://example.com/?a=b=c=d=e=f=g=h#foo');
document.write( "<h4>Testing query cleanup function</h4>");
url.query.clear();
assertEqual( 'http://example.com/#foo', url.toString(), "query.clear()");
// encode-decode
var
url1 = new Url('http://localhost/?a=%3F').toString()
url2 = new Url('http://localhost/?a=%3f').toString()
;
document.write( "<h4>Testing case sensitivity in encode-decode</h4>");
assertEqual( url1.toLowerCase(), url2.toLowerCase(), "constructor()");
</script>
</body>
</html>