OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library uriTest; | |
6 | |
7 import "package:expect/expect.dart"; | |
8 import 'dart:utf'; | |
9 import 'dart:uri'; | |
10 | |
11 testUri(String uri, bool isAbsolute) { | |
12 Expect.equals(isAbsolute, Uri.parse(uri).isAbsolute); | |
13 Expect.equals(isAbsolute, new Uri(uri).isAbsolute); | |
14 Expect.stringEquals(uri, Uri.parse(uri).toString()); | |
15 Expect.stringEquals(uri, new Uri(uri).toString()); | |
16 | |
17 // Test equals and hashCode members. | |
18 Expect.equals(new Uri(uri), new Uri(uri)); | |
19 Expect.equals(new Uri(uri).hashCode, new Uri(uri).hashCode); | |
20 } | |
21 | |
22 testEncodeDecode(String orig, String encoded) { | |
23 var e = encodeUri(orig); | |
24 Expect.stringEquals(encoded, e); | |
25 var d = decodeUri(encoded); | |
26 Expect.stringEquals(orig, d); | |
27 } | |
28 | |
29 testEncodeDecodeComponent(String orig, String encoded) { | |
30 var e = encodeUriComponent(orig); | |
31 Expect.stringEquals(encoded, e); | |
32 var d = decodeUriComponent(encoded); | |
33 Expect.stringEquals(orig, d); | |
34 } | |
35 | |
36 testUriPerRFCs(Uri base) { | |
37 // From RFC 3986. | |
38 Expect.stringEquals("g:h", base.resolve("g:h").toString()); | |
39 Expect.stringEquals("http://a/b/c/g", base.resolve("g").toString()); | |
40 Expect.stringEquals("http://a/b/c/g", base.resolve("./g").toString()); | |
41 Expect.stringEquals("http://a/b/c/g/", base.resolve("g/").toString()); | |
42 Expect.stringEquals("http://a/g", base.resolve("/g").toString()); | |
43 Expect.stringEquals("http://g", base.resolve("//g").toString()); | |
44 Expect.stringEquals("http://a/b/c/d;p?y", base.resolve("?y").toString()); | |
45 Expect.stringEquals("http://a/b/c/g?y", base.resolve("g?y").toString()); | |
46 Expect.stringEquals("http://a/b/c/d;p?q#s", base.resolve("#s").toString()); | |
47 Expect.stringEquals("http://a/b/c/g#s", base.resolve("g#s").toString()); | |
48 Expect.stringEquals("http://a/b/c/g?y#s", base.resolve("g?y#s").toString()); | |
49 Expect.stringEquals("http://a/b/c/;x", base.resolve(";x").toString()); | |
50 Expect.stringEquals("http://a/b/c/g;x", base.resolve("g;x").toString()); | |
51 Expect.stringEquals("http://a/b/c/g;x?y#s", | |
52 base.resolve("g;x?y#s").toString()); | |
53 Expect.stringEquals("http://a/b/c/d;p?q", base.resolve("").toString()); | |
54 Expect.stringEquals("http://a/b/c/", base.resolve(".").toString()); | |
55 Expect.stringEquals("http://a/b/c/", base.resolve("./").toString()); | |
56 Expect.stringEquals("http://a/b/", base.resolve("..").toString()); | |
57 Expect.stringEquals("http://a/b/", base.resolve("../").toString()); | |
58 Expect.stringEquals("http://a/b/g", base.resolve("../g").toString()); | |
59 Expect.stringEquals("http://a/", base.resolve("../..").toString()); | |
60 Expect.stringEquals("http://a/", base.resolve("../../").toString()); | |
61 Expect.stringEquals("http://a/g", base.resolve("../../g").toString()); | |
62 Expect.stringEquals("http://a/g", base.resolve("../../../g").toString()); | |
63 Expect.stringEquals("http://a/g", base.resolve("../../../../g").toString()); | |
64 Expect.stringEquals("http://a/g", base.resolve("/./g").toString()); | |
65 Expect.stringEquals("http://a/g", base.resolve("/../g").toString()); | |
66 Expect.stringEquals("http://a/b/c/g.", base.resolve("g.").toString()); | |
67 Expect.stringEquals("http://a/b/c/.g", base.resolve(".g").toString()); | |
68 Expect.stringEquals("http://a/b/c/g..", base.resolve("g..").toString()); | |
69 Expect.stringEquals("http://a/b/c/..g", base.resolve("..g").toString()); | |
70 Expect.stringEquals("http://a/b/g", base.resolve("./../g").toString()); | |
71 Expect.stringEquals("http://a/b/c/g/", base.resolve("./g/.").toString()); | |
72 Expect.stringEquals("http://a/b/c/g/h", base.resolve("g/./h").toString()); | |
73 Expect.stringEquals("http://a/b/c/h", base.resolve("g/../h").toString()); | |
74 Expect.stringEquals("http://a/b/c/g;x=1/y", | |
75 base.resolve("g;x=1/./y").toString()); | |
76 Expect.stringEquals("http://a/b/c/y", base.resolve("g;x=1/../y").toString()); | |
77 Expect.stringEquals("http://a/b/c/g?y/./x", | |
78 base.resolve("g?y/./x").toString()); | |
79 Expect.stringEquals("http://a/b/c/g?y/../x", | |
80 base.resolve("g?y/../x").toString()); | |
81 Expect.stringEquals("http://a/b/c/g#s/./x", | |
82 base.resolve("g#s/./x").toString()); | |
83 Expect.stringEquals("http://a/b/c/g#s/../x", | |
84 base.resolve("g#s/../x").toString()); | |
85 Expect.stringEquals("http:g", base.resolve("http:g").toString()); | |
86 | |
87 // Additional tests (not from RFC 3986). | |
88 Expect.stringEquals("http://a/b/g;p/h;s", | |
89 base.resolve("../g;p/h;s").toString()); | |
90 } | |
91 | |
92 main() { | |
93 testUri("http:", true); | |
94 testUri("file://", true); | |
95 testUri("file", false); | |
96 testUri("http://user@example.com:80/fisk?query=89&hest=silas", true); | |
97 testUri("http://user@example.com:80/fisk?query=89&hest=silas#fragment", | |
98 false); | |
99 Expect.stringEquals("http://user@example.com:80/a/b/c?query#fragment", | |
100 const Uri.fromComponents( | |
101 scheme: "http", | |
102 userInfo: "user", | |
103 domain: "example.com", | |
104 port: 80, | |
105 path: "/a/b/c", | |
106 query: "query", | |
107 fragment: "fragment").toString()); | |
108 Expect.stringEquals("null://null@null/a/b/c/?null#null", | |
109 const Uri.fromComponents( | |
110 scheme: null, | |
111 userInfo: null, | |
112 domain: null, | |
113 port: 0, | |
114 path: "/a/b/c/", | |
115 query: null, | |
116 fragment: null).toString()); | |
117 Expect.stringEquals("file://", Uri.parse("file:").toString()); | |
118 Expect.stringEquals("file://", new Uri("file:").toString()); | |
119 Expect.stringEquals("/a/g", removeDotSegments("/a/b/c/./../../g")); | |
120 Expect.stringEquals("mid/6", removeDotSegments("mid/content=5/../6")); | |
121 Expect.stringEquals("a/b/e", removeDotSegments("a/b/c/d/../../e")); | |
122 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/c/d/../../e")); | |
123 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/c/d/../../e")); | |
124 Expect.stringEquals("a/b/e", removeDotSegments("../a/b/./c/d/../../e")); | |
125 Expect.stringEquals("a/b/e", removeDotSegments("./a/b/./c/d/../../e")); | |
126 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/.")); | |
127 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/./.")); | |
128 Expect.stringEquals("a/b/e/", removeDotSegments("./a/b/./c/d/../../e/././.")); | |
129 | |
130 final urisSample = "http://a/b/c/d;p?q"; | |
131 Uri baseFromString = Uri.parse(urisSample); | |
132 testUriPerRFCs(baseFromString); | |
133 Uri base = new Uri(urisSample); | |
134 testUriPerRFCs(base); | |
135 | |
136 Expect.stringEquals( | |
137 "http://example.com", | |
138 Uri.parse("http://example.com/a/b/c").origin); | |
139 Expect.stringEquals( | |
140 "https://example.com", | |
141 Uri.parse("https://example.com/a/b/c").origin); | |
142 Expect.stringEquals( | |
143 "http://example.com:1234", | |
144 Uri.parse("http://example.com:1234/a/b/c").origin); | |
145 Expect.stringEquals( | |
146 "https://example.com:1234", | |
147 Uri.parse("https://example.com:1234/a/b/c").origin); | |
148 Expect.throws( | |
149 () => Uri.parse("http:").origin, | |
150 (e) { return e is ArgumentError; }, | |
151 "origin for uri with empty domain should fail"); | |
152 Expect.throws( | |
153 () => const Uri.fromComponents( | |
154 scheme: "http", | |
155 userInfo: null, | |
156 domain: "", | |
157 port: 80, | |
158 path: "/a/b/c", | |
159 query: "query", | |
160 fragment: "fragment").origin, | |
161 (e) { return e is ArgumentError; }, | |
162 "origin for uri with empty domain should fail"); | |
163 Expect.throws( | |
164 () => const Uri.fromComponents( | |
165 scheme: null, | |
166 userInfo: null, | |
167 domain: "", | |
168 port: 80, | |
169 path: "/a/b/c", | |
170 query: "query", | |
171 fragment: "fragment").origin, | |
172 (e) { return e is ArgumentError; }, | |
173 "origin for uri with empty scheme should fail"); | |
174 Expect.throws( | |
175 () => const Uri.fromComponents( | |
176 scheme: "http", | |
177 userInfo: null, | |
178 domain: null, | |
179 port: 80, | |
180 path: "/a/b/c", | |
181 query: "query", | |
182 fragment: "fragment").origin, | |
183 (e) { return e is ArgumentError; }, | |
184 "origin for uri with empty domain should fail"); | |
185 Expect.throws( | |
186 () => Uri.parse("http://:80").origin, | |
187 (e) { return e is ArgumentError; }, | |
188 "origin for uri with empty domain should fail"); | |
189 Expect.throws( | |
190 () => Uri.parse("file://localhost/test.txt").origin, | |
191 (e) { return e is ArgumentError; }, | |
192 "origin for non-http/https uri should fail"); | |
193 | |
194 // URI encode tests | |
195 // Create a string with code point 0x10000 encoded as a surrogate pair. | |
196 var s = decodeUtf8([0xf0, 0x90, 0x80, 0x80]); | |
197 | |
198 Expect.stringEquals("\u{10000}", s); | |
199 | |
200 testEncodeDecode("A + B", "A+%2B+B"); | |
201 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | |
202 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | |
203 testEncodeDecode("\uFFFE", "%EF%BF%BE"); | |
204 testEncodeDecode("\uFFFF", "%EF%BF%BF"); | |
205 testEncodeDecode("\x7f", "%7F"); | |
206 testEncodeDecode("\x80", "%C2%80"); | |
207 testEncodeDecode("\u0800", "%E0%A0%80"); | |
208 testEncodeDecode(":/@',;?&=+\$", ":/@',;?&=%2B\$"); | |
209 testEncodeDecode(s, "%F0%90%80%80"); | |
210 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | |
211 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | |
212 testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE"); | |
213 testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF"); | |
214 testEncodeDecodeComponent("\x7f", "%7F"); | |
215 testEncodeDecodeComponent("\x80", "%C2%80"); | |
216 testEncodeDecodeComponent("\u0800", "%E0%A0%80"); | |
217 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); | |
218 testEncodeDecodeComponent(s, "%F0%90%80%80"); | |
219 } | |
OLD | NEW |