OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library uriTest; | 5 library uriTest; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 testUri(String uri, bool isAbsolute) { | 10 testUri(String uri, bool isAbsolute) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 base.resolve("../g;p/h;s").toString()); | 117 base.resolve("../g;p/h;s").toString()); |
118 } | 118 } |
119 | 119 |
120 void testResolvePath(String expected, String path) { | 120 void testResolvePath(String expected, String path) { |
121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); | 121 Expect.equals(expected, new Uri().resolveUri(new Uri(path: path)).path); |
122 Expect.equals( | 122 Expect.equals( |
123 "http://localhost$expected", | 123 "http://localhost$expected", |
124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); | 124 Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString()); |
125 } | 125 } |
126 | 126 |
127 const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ"; | |
128 const DIGIT = r"0123456789"; | |
129 const PERCENT_ENCODED = "%00%ff"; | |
130 const SUBDELIM = r"!$&'()*+,;="; | |
131 | |
132 const SCHEMECHAR = "$ALPHA$DIGIT+-."; | |
133 const UNRESERVED = "$ALPHA$DIGIT-._~"; | |
134 const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED"; | |
135 const USERINFOCHAR = "$REGNAMECHAR:"; | |
136 | |
137 const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@"; | |
138 const PCHAR = "$PCHAR_NC:"; | |
139 const QUERYCHAR = "$PCHAR/?"; | |
140 | |
141 void testValidCharacters() { | |
142 // test that all valid characters are accepted. | |
143 | |
144 for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) { | |
145 for (var userinfo in ["", "@", "$USERINFOCHAR$USERINFOCHAR@", | |
146 "$USERINFOCHAR:$DIGIT@"]) { | |
147 for (var host in ["", "$REGNAMECHAR$REGNAMECHAR", | |
148 "255.255.255.256", // valid reg-name. | |
149 "[ffff::ffff:ffff]", "[ffff::255.255.255.255]"]) { | |
150 for (var port in ["", ":$DIGIT$DIGIT"]) { | |
151 var auth = "$userinfo$host$port"; | |
152 if (auth.isNotEmpty) auth = "//$auth"; | |
153 var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty. | |
154 if (auth.isNotEmpty) { | |
155 // Initial segment may be empty. | |
156 paths..add("//$PCHAR"); | |
157 } else { | |
158 // Path may begin with non-slash. | |
159 if (scheme.isEmpty) { | |
160 // Initial segment must not contain colon. | |
161 paths..add(PCHAR_NC) | |
162 ..add("$PCHAR_NC/$PCHAR") | |
163 ..add("$PCHAR_NC/$PCHAR/"); | |
164 } else { | |
165 paths..add(PCHAR) | |
166 ..add("$PCHAR/$PCHAR") | |
167 ..add("$PCHAR/$PCHAR/"); | |
168 } | |
169 } | |
170 for (var path in paths) { | |
171 for (var query in ["", "?", "?$QUERYCHAR"]) { | |
172 for (var fragment in ["", "#", "#$QUERYCHAR"]) { | |
173 var uri = "$scheme$auth$path$query$fragment"; | |
174 // Should not throw. | |
175 var result = Uri.parse(uri); | |
176 } | |
177 } | |
178 } | |
179 } | |
180 } | |
181 } | |
182 } | |
183 } | |
184 | |
185 void testInvalidUrls() { | |
186 void checkInvalid(uri) { | |
187 try { | |
188 var result = Uri.parse(uri); | |
189 Expect.fail("Invalid URI `$uri` parsed to $result\n" | |
190 " Scheme: ${result.scheme}\n" | |
191 " User-info: ${result.userInfo}\n" | |
192 " Host: ${result.host}\n" | |
193 " Port: ${result.port}\n" | |
194 " Path: ${result.path}\n" | |
195 " Query: ${result.query}\n" | |
196 " Fragment: ${result.fragment}\n"); | |
197 } on FormatException { | |
198 // Success. | |
199 } | |
200 } | |
201 | |
202 // Regression test for http://dartbug.com/16081 | |
203 checkInvalid("http://www.example.org/red%09ros\u00E9#red"); | |
204 checkInvalid("http://r\u00E9sum\u00E9.example.org"); | |
205 | |
206 // Invalid characters. The characters must be rejected, even if normalizing | |
207 // the input would cause them to be valid (normalization happens after | |
208 // validation). | |
209 var invalidChars = [ | |
210 "\xe7", // Arbitrary non-ASCII letter | |
211 " ", // Space, not allowed anywhere. | |
212 '"', // Quote, not allowed anywhere | |
213 "\x7f", // DEL, not allowed anywhere | |
214 "\xdf", // German lower-case scharf-S. Becomes ASCII when upper-cased. | |
215 "\u0130" // Latin capital dotted I, becomes ASCII lower-case in Turkish. | |
216 "%\uFB03", // % + Ligature ffi, becomes ASCII when upper-cased, | |
217 // should not be read as "%FFI". | |
218 "\u212a", // Kelvin sign. Becomes ASCII when lower-cased. | |
219 "%1g", // Invalid escape. | |
220 ]; | |
221 for (var invalid in invalidChars) { | |
222 checkInvalid("A${invalid}b:///"); | |
223 checkInvalid("${invalid}b:///"); | |
224 checkInvalid("s://user${invalid}info@x.x/"); | |
225 checkInvalid("s://reg${invalid}name/"); | |
226 checkInvalid("s://regname:12${invalid}45/"); | |
227 checkInvalid("s://regname/p${invalid}ath/"); | |
228 checkInvalid("/p${invalid}ath/"); | |
229 checkInvalid("p${invalid}ath/"); | |
230 checkInvalid("s://regname/path/?x${invalid}x"); | |
231 checkInvalid("s://regname/path/#x${invalid}x"); | |
232 checkInvalid("s://regname/path/?#x${invalid}x"); | |
233 } | |
234 checkInvalid("s%41://x.x/"); // No escapes in scheme, | |
235 // and no colon before slash in path. | |
236 checkInvalid("1a://x.x/"); // Scheme must start with letter, | |
237 // and no colon before slash in path. | |
238 checkInvalid(".a://x.x/"); // Scheme must start with letter, | |
239 // and no colon before slash in path. | |
240 checkInvalid("_:"); // Character not valid in scheme, | |
241 // and no colon before slash in path. | |
242 checkInvalid(":"); // Scheme must start with letter, | |
243 // and no colon before slash in path. | |
244 checkInvalid("s://x@x@x.x/"); // At most one @ in userinfo. | |
245 checkInvalid("s://x@x:x/"); // No colon in host except before a port. | |
246 checkInvalid("s://x@x:9:9/"); // At most one port. | |
247 checkInvalid("s://x/x#foo#bar"); // At most one #. | |
248 checkInvalid("s://:/"); // Colon in host requires port, | |
249 // and path may not start with //. | |
250 checkInvalid("s@://x:9/x?x#x"); // @ not allowed in scheme. | |
251 } | |
252 | |
253 void testNormalization() { | |
254 // The Uri constructor and the Uri.parse function performs RFC-3986 | |
255 // syntax based normalization. | |
256 | |
257 var uri; | |
258 | |
259 // Scheme: Only case normalization. Schemes cannot contain escapes. | |
260 uri = Uri.parse("A:"); | |
261 Expect.equals("a", uri.scheme); | |
262 uri = Uri.parse("Z:"); | |
263 Expect.equals("z", uri.scheme); | |
264 uri = Uri.parse("$SCHEMECHAR:"); | |
265 Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme); | |
266 | |
267 // Percent escape normalization. | |
268 // Escapes of unreserved characters are converted to the character, | |
269 // subject to case normalization in reg-name. | |
270 for (var i = 0; i < UNRESERVED.length; i++) { | |
271 var char = UNRESERVED[i]; | |
272 var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf. | |
273 | |
274 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | |
275 "?vV${escape}vV#wW${escape}wW"); | |
276 Expect.equals("xX${char}xX", uri.userInfo); | |
277 Expect.equals("yY${char}yY".toLowerCase(), uri.host); | |
278 Expect.equals("/zZ${char}zZ", uri.path); | |
279 Expect.equals("vV${char}vV", uri.query); | |
280 Expect.equals("wW${char}wW", uri.fragment); | |
281 } | |
282 | |
283 // Escapes of reserved characters are kept, but upper-cased. | |
284 for (var escape in ["%00", "%1f", "%7F", "%fF"]) { | |
285 uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ" | |
286 "?vV${escape}vV#wW${escape}wW"); | |
287 var normalizedEscape = escape.toUpperCase(); | |
288 Expect.equals("xX${normalizedEscape}xX", uri.userInfo); | |
289 Expect.equals("yy${normalizedEscape}yy", uri.host); | |
290 Expect.equals("/zZ${normalizedEscape}zZ", uri.path); | |
291 Expect.equals("vV${normalizedEscape}vV", uri.query); | |
292 Expect.equals("wW${normalizedEscape}wW", uri.fragment); | |
293 } | |
294 | |
295 // Some host normalization edge cases. | |
296 uri = Uri.parse("x://x%61X%41x%41X%61x/"); | |
297 Expect.equals("xaxaxaxax", uri.host); | |
298 | |
299 uri = Uri.parse("x://Xxxxxxxx/"); | |
300 Expect.equals("xxxxxxxx", uri.host); | |
301 | |
302 uri = Uri.parse("x://xxxxxxxX/"); | |
303 Expect.equals("xxxxxxxx", uri.host); | |
304 | |
305 uri = Uri.parse("x://xxxxxxxx%61/"); | |
306 Expect.equals("xxxxxxxxa", uri.host); | |
307 | |
308 uri = Uri.parse("x://%61xxxxxxxx/"); | |
309 Expect.equals("axxxxxxxx", uri.host); | |
310 | |
311 uri = Uri.parse("x://X/"); | |
312 Expect.equals("x", uri.host); | |
313 | |
314 uri = Uri.parse("x://%61/"); | |
315 Expect.equals("a", uri.host); | |
316 | |
317 // TODO(lrn): Also do path normalization: /./ -> / and /x/../ -> / | |
318 } | |
319 | |
320 main() { | 127 main() { |
321 testUri("http:", true); | 128 testUri("http:", true); |
322 testUri("file://", true); | 129 testUri("file://", true); |
323 testUri("file", false); | 130 testUri("file", false); |
324 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); | 131 testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true); |
325 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", | 132 testUri("http://user@example.com:8080/fisk?query=89&hest=silas#fragment", |
326 false); | 133 false); |
327 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", | 134 Expect.stringEquals("http://user@example.com/a/b/c?query#fragment", |
328 new Uri( | 135 new Uri( |
329 scheme: "http", | 136 scheme: "http", |
330 userInfo: "user", | 137 userInfo: "user", |
331 host: "example.com", | 138 host: "example.com", |
332 port: 80, | 139 port: 80, |
333 path: "/a/b/c", | 140 path: "/a/b/c", |
334 query: "query", | 141 query: "query", |
335 fragment: "fragment").toString()); | 142 fragment: "fragment").toString()); |
336 Expect.stringEquals("//null@null/a/b/c/", | 143 Expect.stringEquals("//null@null/a/b/c/", |
337 new Uri( | 144 new Uri( |
338 scheme: null, | 145 scheme: null, |
339 userInfo: null, | 146 userInfo: null, |
340 host: null, | 147 host: null, |
341 port: 0, | 148 port: 0, |
342 path: "/a/b/c/", | 149 path: "/a/b/c/", |
343 query: null, | 150 query: null, |
344 fragment: null).toString()); | 151 fragment: null).toString()); |
345 Expect.stringEquals("file://", Uri.parse("file:").toString()); | 152 Expect.stringEquals("file://", Uri.parse("file:").toString()); |
| 153 |
346 testResolvePath("/a/g", "/a/b/c/./../../g"); | 154 testResolvePath("/a/g", "/a/b/c/./../../g"); |
347 testResolvePath("/a/g", "/a/b/c/./../../g"); | 155 testResolvePath("/a/g", "/a/b/c/./../../g"); |
348 testResolvePath("/mid/6", "mid/content=5/../6"); | 156 testResolvePath("/mid/6", "mid/content=5/../6"); |
349 testResolvePath("/a/b/e", "a/b/c/d/../../e"); | 157 testResolvePath("/a/b/e", "a/b/c/d/../../e"); |
350 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); | 158 testResolvePath("/a/b/e", "../a/b/c/d/../../e"); |
351 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); | 159 testResolvePath("/a/b/e", "./a/b/c/d/../../e"); |
352 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); | 160 testResolvePath("/a/b/e", "../a/b/./c/d/../../e"); |
353 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); | 161 testResolvePath("/a/b/e", "./a/b/./c/d/../../e"); |
354 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); | 162 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/."); |
355 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); | 163 testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./."); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); | 254 testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24"); |
447 testEncodeDecodeComponent(s, "%F0%90%80%80"); | 255 testEncodeDecodeComponent(s, "%F0%90%80%80"); |
448 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); | 256 testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B"); |
449 testEncodeDecodeQueryComponent( | 257 testEncodeDecodeQueryComponent( |
450 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); | 258 "æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null); |
451 | 259 |
452 // Invalid URI - : and @ is swapped, port ("host") should be numeric. | 260 // Invalid URI - : and @ is swapped, port ("host") should be numeric. |
453 Expect.throws( | 261 Expect.throws( |
454 () => Uri.parse("file://user@password:host/path"), | 262 () => Uri.parse("file://user@password:host/path"), |
455 (e) => e is FormatException); | 263 (e) => e is FormatException); |
456 | |
457 testValidCharacters(); | |
458 testInvalidUrls(); | |
459 testNormalization(); | |
460 } | 264 } |
OLD | NEW |