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