OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 import "package:expect/expect.dart"; | |
6 | |
7 void testInvalidArguments() { | |
8 } | |
9 | |
10 void testEncodeQueryComponent() { | |
11 // This exact data is from posting a form in Chrome 26 with the one | |
12 // exception that * is encoded as %30 and ~ is not encoded as %7E. | |
13 Expect.equals( | |
14 "%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%" | |
15 "3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~", | |
16 Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); | |
17 Expect.equals("+%2B+", Uri.encodeQueryComponent(" + ")); | |
18 Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +")); | |
19 } | |
20 | |
21 void testQueryParameters() { | |
22 test(String query, Map<String, String> parameters, [String normalizedQuery]) { | |
23 if (normalizedQuery == null) normalizedQuery = query; | |
24 check(uri) { | |
25 Expect.isTrue(uri.hasQuery); | |
26 Expect.equals(normalizedQuery, uri.query); | |
27 Expect.equals("?$normalizedQuery", uri.toString()); | |
28 if (parameters.containsValue(null)) { | |
29 var map = new Map.from(parameters); | |
30 map.forEach((k, v) { if (v == null) map[k] = ""; }); | |
31 Expect.mapEquals(map, uri.queryParameters); | |
32 } else { | |
33 Expect.mapEquals(parameters, uri.queryParameters); | |
34 } | |
35 } | |
36 | |
37 var uri1 = new Uri(queryParameters: parameters); | |
38 var uri2 = new Uri(query: query); | |
39 var uri3 = Uri.parse("?$query"); | |
40 check(uri1); | |
41 if (query != "") { | |
42 check(uri2); | |
43 } else { | |
44 Expect.isFalse(uri2.hasQuery); | |
45 } | |
46 check(uri3); | |
47 Expect.equals(uri1, uri3); | |
48 if (query != "") Expect.equals(uri2, uri3); | |
49 if (parameters.containsValue(null)) { | |
50 var map = new Map.from(parameters); | |
51 map.forEach((k, v) { if (v == null) map[k] = ""; }); | |
52 Expect.mapEquals(map, Uri.splitQueryString(query)); | |
53 } else { | |
54 Expect.mapEquals(parameters, Uri.splitQueryString(query)); | |
55 } | |
56 } | |
57 | |
58 test("", {}); | |
59 test("A", {"A": null}); | |
60 test("%25", {"%": null}); | |
61 test("%41", {"A": null}, "A"); | |
62 test("%41A", {"AA": null}, "AA"); | |
63 test("A", {"A": ""}); | |
64 test("%25", {"%": ""}); | |
65 test("%41", {"A": ""}, "A"); | |
66 test("%41A", {"AA": ""}, "AA"); | |
67 test("A=a", {"A": "a"}); | |
68 test("%25=a", {"%": "a"}); | |
69 test("%41=%61", {"A": "a"}, "A=a"); | |
70 test("A=+", {"A": " "}); | |
71 test("A=%2B", {"A": "+"}); | |
72 test("A=a&B", {"A": "a", "B": null}); | |
73 test("A=a&B", {"A": "a", "B": ""}); | |
74 test("A=a&B=b", {"A": "a", "B": "b"}); | |
75 test("%41=%61&%42=%62", {"A": "a", "B": "b"}, "A=a&B=b"); | |
76 | |
77 var unreserved = "-._~0123456789" | |
78 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
79 "abcdefghijklmnopqrstuvwxyz"; | |
80 var encoded = new StringBuffer(); | |
81 var allEncoded = new StringBuffer(); | |
82 var unencoded = new StringBuffer(); | |
83 for (int i = 32; i < 128; i++) { | |
84 if (i == 32) { | |
85 encoded.write("+"); | |
86 } else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) { | |
87 encoded.writeCharCode(i); | |
88 } else { | |
89 encoded.write("%"); | |
90 encoded.write(i.toRadixString(16).toUpperCase()); | |
91 } | |
92 if (i == 32) { | |
93 allEncoded.write("+"); | |
94 } else { | |
95 allEncoded.write("%"); | |
96 allEncoded.write(i.toRadixString(16).toUpperCase()); | |
97 } | |
98 unencoded.writeCharCode(i); | |
99 } | |
100 encoded = encoded.toString(); | |
101 unencoded = unencoded.toString(); | |
102 test("a=$encoded", {"a": unencoded}); | |
103 test("a=$encoded&b=$encoded", {"a": unencoded, "b": unencoded}); | |
104 | |
105 var map = new Map(); | |
106 map[unencoded] = unencoded; | |
107 test("$encoded=$encoded", map); | |
108 test("$encoded=$allEncoded", map, "$encoded=$encoded"); | |
109 test("$allEncoded=$encoded", map, "$encoded=$encoded"); | |
110 test("$allEncoded=$allEncoded", map, "$encoded=$encoded"); | |
111 map[unencoded] = null; | |
112 test("$encoded", map); | |
113 map[unencoded] = ""; | |
114 test("$encoded", map); | |
115 } | |
116 | |
117 testInvalidQueryParameters() { | |
118 test(String query, Map<String, String> parameters) { | |
119 check(uri) { | |
120 Expect.equals(query, uri.query); | |
121 if (query.isEmpty) { | |
122 Expect.equals(query, uri.toString()); | |
123 } else { | |
124 Expect.equals("?$query", uri.toString()); | |
125 } | |
126 if (parameters.containsValue(null)) { | |
127 } else { | |
128 Expect.mapEquals(parameters, uri.queryParameters); | |
129 } | |
130 } | |
131 | |
132 var uri1 = new Uri(query: query); | |
133 var uri2 = Uri.parse("?$query"); | |
134 check(uri1); | |
135 check(uri2); | |
136 Expect.equals(uri1, uri2); | |
137 } | |
138 | |
139 test("=", {}); | |
140 test("=xxx", {}); | |
141 test("===", {}); | |
142 test("=xxx=yyy=zzz", {}); | |
143 test("=&=&=", {}); | |
144 test("=xxx&=yyy&=zzz", {}); | |
145 test("&=&=&", {}); | |
146 test("&=xxx&=xxx&", {}); | |
147 } | |
148 | |
149 testQueryParametersImmutableMap() { | |
150 test(map) { | |
151 bool isUnsupported(e) => e is UnsupportedError; | |
152 | |
153 Expect.isTrue(map.containsValue("b")); | |
154 Expect.isTrue(map.containsKey("a")); | |
155 Expect.equals("b", map["a"]); | |
156 Expect.throws(() => map["a"] = "c", isUnsupported); | |
157 Expect.throws(() => map.putIfAbsent("b", () => "e"), isUnsupported); | |
158 Expect.throws(() => map.remove("a"), isUnsupported); | |
159 Expect.throws(() => map.clear(), isUnsupported); | |
160 var count = 0; | |
161 map.forEach((key, value) => count++); | |
162 Expect.equals(2, count); | |
163 Expect.equals(2, map.keys.length); | |
164 Expect.equals(2, map.values.length); | |
165 Expect.equals(2, map.length); | |
166 Expect.isFalse(map.isEmpty); | |
167 Expect.isTrue(map.isNotEmpty); | |
168 } | |
169 | |
170 test(Uri.parse("?a=b&c=d").queryParameters); | |
171 test(new Uri(queryParameters: {"a": "b", "c": "d"}).queryParameters); | |
172 } | |
173 | |
174 main() { | |
175 testInvalidArguments(); | |
176 testEncodeQueryComponent(); | |
177 testQueryParameters(); | |
178 testInvalidQueryParameters(); | |
179 testQueryParametersImmutableMap(); | |
180 } | |
OLD | NEW |