OLD | NEW |
| (Empty) |
1 // Copyright (c) 2017, 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 uri.examples; | |
6 | |
7 // Examples from the Uri class documentation. | |
8 // Get an error if the documentation starts to be wrong. | |
9 // REMEMBER TO UPDATE BOTH. | |
10 | |
11 import "package:expect/expect.dart"; | |
12 import 'dart:convert'; | |
13 | |
14 main() { | |
15 // Uri.http | |
16 test("http://example.org/path?q=dart", | |
17 new Uri.http("example.org", "/path", {"q": "dart"})); | |
18 test("http://user:pass@localhost:8080", | |
19 new Uri.http("user:pass@localhost:8080", "")); | |
20 test("http://example.org/a%20b", new Uri.http("example.org", "a b")); | |
21 test("http://example.org/a%252F", new Uri.http("example.org", "/a%2F")); | |
22 | |
23 // Uri.file | |
24 test("xxx/yyy", new Uri.file("xxx/yyy", windows: false)); | |
25 test("xxx/yyy/", new Uri.file("xxx/yyy/", windows: false)); | |
26 test("file:///xxx/yyy", new Uri.file("/xxx/yyy", windows: false)); | |
27 test("file:///xxx/yyy/", new Uri.file("/xxx/yyy/", windows: false)); | |
28 test("C%3A", new Uri.file("C:", windows: false)); | |
29 test("xxx/yyy", new Uri.file(r"xxx\yyy", windows: true)); | |
30 test("xxx/yyy/", new Uri.file(r"xxx\yyy\", windows: true)); | |
31 test("file:///xxx/yyy", new Uri.file(r"\xxx\yyy", windows: true)); | |
32 test("file:///xxx/yyy/", new Uri.file(r"\xxx\yyy/", windows: true)); | |
33 test("file:///C:/xxx/yyy", new Uri.file(r"C:\xxx\yyy", windows: true)); | |
34 test("file://server/share/file", | |
35 new Uri.file(r"\\server\share\file", windows: true)); | |
36 Expect.throws(() => new Uri.file(r"C:", windows: true)); | |
37 Expect.throws(() => new Uri.file(r"C:xxx\yyy", windows: true)); | |
38 | |
39 // isScheme. | |
40 var uri = Uri.parse("http://example.com/"); | |
41 Expect.isTrue(uri.isScheme("HTTP")); | |
42 | |
43 // toFilePath. | |
44 Expect.equals(r"xxx/yyy", Uri.parse("xxx/yyy").toFilePath(windows: false)); | |
45 Expect.equals(r"xxx/yyy/", Uri.parse("xxx/yyy/").toFilePath(windows: false)); | |
46 Expect.equals( | |
47 r"/xxx/yyy", Uri.parse("file:///xxx/yyy").toFilePath(windows: false)); | |
48 Expect.equals( | |
49 r"/xxx/yyy/", Uri.parse("file:///xxx/yyy/").toFilePath(windows: false)); | |
50 Expect.equals(r"/C:", Uri.parse("file:///C:").toFilePath(windows: false)); | |
51 Expect.equals(r"/C:a", Uri.parse("file:///C:a").toFilePath(windows: false)); | |
52 | |
53 Expect.equals(r"xxx\yyy", Uri.parse("xxx/yyy").toFilePath(windows: true)); | |
54 Expect.equals(r"xxx\yyy\", Uri.parse("xxx/yyy/").toFilePath(windows: true)); | |
55 Expect.equals( | |
56 r"\xxx\yyy", Uri.parse("file:///xxx/yyy").toFilePath(windows: true)); | |
57 Expect.equals( | |
58 r"\xxx\yyy\", Uri.parse("file:///xxx/yyy/").toFilePath(windows: true)); | |
59 Expect.equals( | |
60 r"C:\xxx\yyy", Uri.parse("file:///C:/xxx/yyy").toFilePath(windows: true)); | |
61 Expect.throws(() => Uri.parse("file:C:xxx/yyy").toFilePath(windows: true)); | |
62 Expect.equals(r"\\server\share\file", | |
63 Uri.parse("file://server/share/file").toFilePath(windows: true)); // | |
64 | |
65 // replace. | |
66 Uri uri1 = Uri.parse("a://b@c:4/d/e?f#g"); | |
67 Uri uri2 = uri1.replace(scheme: "A", path: "D/E/E", fragment: "G"); | |
68 Expect.equals("a://b@c:4/D/E/E?f#G", "$uri2"); | |
69 Uri uri3 = new Uri( | |
70 scheme: "A", | |
71 userInfo: uri1.userInfo, | |
72 host: uri1.host, | |
73 port: uri1.port, | |
74 path: "D/E/E", | |
75 query: uri1.query, | |
76 fragment: "G"); | |
77 Expect.equals("a://b@c:4/D/E/E?f#G", "$uri3"); | |
78 Expect.equals(uri2, uri3); | |
79 | |
80 // UriData.mimeType | |
81 var data = UriData.parse("data:text/plain;charset=utf-8,Hello%20World!"); | |
82 Expect.equals("text/plain", data.mimeType); | |
83 Expect.equals("utf-8", data.charset); | |
84 | |
85 // Uri.parseIPv6Address - shouldn't throw. | |
86 Uri.parseIPv6Address("::1"); | |
87 Uri.parseIPv6Address("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"); | |
88 Uri.parseIPv6Address("3ffe:2a00:100:7031::1"); | |
89 Uri.parseIPv6Address("::FFFF:129.144.52.38"); | |
90 Uri.parseIPv6Address("2010:836B:4179::836B:4179"); | |
91 } | |
92 | |
93 test(String result, Uri value) { | |
94 Expect.equals(Uri.parse(result), value); | |
95 Expect.equals(result, value.toString()); | |
96 } | |
OLD | NEW |