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