| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 void testInvalidArguments() { | 7 void testInvalidArguments() {} |
| 8 } | |
| 9 | 8 |
| 10 void testEncodeQueryComponent() { | 9 void testEncodeQueryComponent() { |
| 11 // This exact data is from posting a form in Chrome 26 with the one | 10 // 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. | 11 // exception that * is encoded as %30 and ~ is not encoded as %7E. |
| 13 Expect.equals( | 12 Expect.equals( |
| 14 "%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%" | 13 "%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~", | 14 "3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~", |
| 16 Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); | 15 Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); |
| 17 Expect.equals("+%2B+", Uri.encodeQueryComponent(" + ")); | 16 Expect.equals("+%2B+", Uri.encodeQueryComponent(" + ")); |
| 18 Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +")); | 17 Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +")); |
| 19 } | 18 } |
| 20 | 19 |
| 21 void testQueryParameters() { | 20 void testQueryParameters() { |
| 22 test(String query, Map<String, String> parameters, [String normalizedQuery]) { | 21 test(String query, Map<String, String> parameters, [String normalizedQuery]) { |
| 23 if (normalizedQuery == null) normalizedQuery = query; | 22 if (normalizedQuery == null) normalizedQuery = query; |
| 24 check(uri) { | 23 check(uri) { |
| 25 Expect.isTrue(uri.hasQuery); | 24 Expect.isTrue(uri.hasQuery); |
| 26 Expect.equals(normalizedQuery, uri.query); | 25 Expect.equals(normalizedQuery, uri.query); |
| 27 Expect.equals("?$normalizedQuery", uri.toString()); | 26 Expect.equals("?$normalizedQuery", uri.toString()); |
| 28 if (parameters.containsValue(null)) { | 27 if (parameters.containsValue(null)) { |
| 29 var map = new Map.from(parameters); | 28 var map = new Map.from(parameters); |
| 30 map.forEach((k, v) { if (v == null) map[k] = ""; }); | 29 map.forEach((k, v) { |
| 30 if (v == null) map[k] = ""; |
| 31 }); |
| 31 Expect.mapEquals(map, uri.queryParameters); | 32 Expect.mapEquals(map, uri.queryParameters); |
| 32 } else { | 33 } else { |
| 33 Expect.mapEquals(parameters, uri.queryParameters); | 34 Expect.mapEquals(parameters, uri.queryParameters); |
| 34 } | 35 } |
| 35 } | 36 } |
| 36 | 37 |
| 37 var uri1 = new Uri(queryParameters: parameters); | 38 var uri1 = new Uri(queryParameters: parameters); |
| 38 var uri2 = new Uri(query: query); | 39 var uri2 = new Uri(query: query); |
| 39 var uri3 = Uri.parse("?$query"); | 40 var uri3 = Uri.parse("?$query"); |
| 40 check(uri1); | 41 check(uri1); |
| 41 if (query != "") { | 42 if (query != "") { |
| 42 check(uri2); | 43 check(uri2); |
| 43 } else { | 44 } else { |
| 44 Expect.isFalse(uri2.hasQuery); | 45 Expect.isFalse(uri2.hasQuery); |
| 45 } | 46 } |
| 46 check(uri3); | 47 check(uri3); |
| 47 Expect.equals(uri1, uri3); | 48 Expect.equals(uri1, uri3); |
| 48 if (query != "") Expect.equals(uri2, uri3); | 49 if (query != "") Expect.equals(uri2, uri3); |
| 49 if (parameters.containsValue(null)) { | 50 if (parameters.containsValue(null)) { |
| 50 var map = new Map.from(parameters); | 51 var map = new Map.from(parameters); |
| 51 map.forEach((k, v) { if (v == null) map[k] = ""; }); | 52 map.forEach((k, v) { |
| 53 if (v == null) map[k] = ""; |
| 54 }); |
| 52 Expect.mapEquals(map, Uri.splitQueryString(query)); | 55 Expect.mapEquals(map, Uri.splitQueryString(query)); |
| 53 } else { | 56 } else { |
| 54 Expect.mapEquals(parameters, Uri.splitQueryString(query)); | 57 Expect.mapEquals(parameters, Uri.splitQueryString(query)); |
| 55 } | 58 } |
| 56 } | 59 } |
| 57 | 60 |
| 58 test("", {}); | 61 test("", {}); |
| 59 test("A", {"A": null}); | 62 test("A", {"A": null}); |
| 60 test("%25", {"%": null}); | 63 test("%25", {"%": null}); |
| 61 test("%41", {"A": null}, "A"); | 64 test("%41", {"A": null}, "A"); |
| 62 test("%41A", {"AA": null}, "AA"); | 65 test("%41A", {"AA": null}, "AA"); |
| 63 test("A", {"A": ""}); | 66 test("A", {"A": ""}); |
| 64 test("%25", {"%": ""}); | 67 test("%25", {"%": ""}); |
| 65 test("%41", {"A": ""}, "A"); | 68 test("%41", {"A": ""}, "A"); |
| 66 test("%41A", {"AA": ""}, "AA"); | 69 test("%41A", {"AA": ""}, "AA"); |
| 67 test("A=a", {"A": "a"}); | 70 test("A=a", {"A": "a"}); |
| 68 test("%25=a", {"%": "a"}); | 71 test("%25=a", {"%": "a"}); |
| 69 test("%41=%61", {"A": "a"}, "A=a"); | 72 test("%41=%61", {"A": "a"}, "A=a"); |
| 70 test("A=+", {"A": " "}); | 73 test("A=+", {"A": " "}); |
| 71 test("A=%2B", {"A": "+"}); | 74 test("A=%2B", {"A": "+"}); |
| 72 test("A=a&B", {"A": "a", "B": null}); | 75 test("A=a&B", {"A": "a", "B": null}); |
| 73 test("A=a&B", {"A": "a", "B": ""}); | 76 test("A=a&B", {"A": "a", "B": ""}); |
| 74 test("A=a&B=b", {"A": "a", "B": "b"}); | 77 test("A=a&B=b", {"A": "a", "B": "b"}); |
| 75 test("%41=%61&%42=%62", {"A": "a", "B": "b"}, "A=a&B=b"); | 78 test("%41=%61&%42=%62", {"A": "a", "B": "b"}, "A=a&B=b"); |
| 76 | 79 |
| 77 var unreserved = "-._~0123456789" | 80 var unreserved = "-._~0123456789" |
| 78 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 81 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 79 "abcdefghijklmnopqrstuvwxyz"; | 82 "abcdefghijklmnopqrstuvwxyz"; |
| 80 var encoded = new StringBuffer(); | 83 var encoded = new StringBuffer(); |
| 81 var allEncoded = new StringBuffer(); | 84 var allEncoded = new StringBuffer(); |
| 82 var unencoded = new StringBuffer(); | 85 var unencoded = new StringBuffer(); |
| 83 for (int i = 32; i < 128; i++) { | 86 for (int i = 32; i < 128; i++) { |
| 84 if (i == 32) { | 87 if (i == 32) { |
| 85 encoded.write("+"); | 88 encoded.write("+"); |
| 86 } else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) { | 89 } else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) { |
| 87 encoded.writeCharCode(i); | 90 encoded.writeCharCode(i); |
| 88 } else { | 91 } else { |
| 89 encoded.write("%"); | 92 encoded.write("%"); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 116 | 119 |
| 117 testInvalidQueryParameters() { | 120 testInvalidQueryParameters() { |
| 118 test(String query, Map<String, String> parameters) { | 121 test(String query, Map<String, String> parameters) { |
| 119 check(uri) { | 122 check(uri) { |
| 120 Expect.equals(query, uri.query); | 123 Expect.equals(query, uri.query); |
| 121 if (query.isEmpty) { | 124 if (query.isEmpty) { |
| 122 Expect.equals(query, uri.toString()); | 125 Expect.equals(query, uri.toString()); |
| 123 } else { | 126 } else { |
| 124 Expect.equals("?$query", uri.toString()); | 127 Expect.equals("?$query", uri.toString()); |
| 125 } | 128 } |
| 126 if (parameters.containsValue(null)) { | 129 if (parameters.containsValue(null)) {} else { |
| 127 } else { | |
| 128 Expect.mapEquals(parameters, uri.queryParameters); | 130 Expect.mapEquals(parameters, uri.queryParameters); |
| 129 } | 131 } |
| 130 } | 132 } |
| 131 | 133 |
| 132 var uri1 = new Uri(query: query); | 134 var uri1 = new Uri(query: query); |
| 133 var uri2 = Uri.parse("?$query"); | 135 var uri2 = Uri.parse("?$query"); |
| 134 check(uri1); | 136 check(uri1); |
| 135 check(uri2); | 137 check(uri2); |
| 136 Expect.equals(uri1, uri2); | 138 Expect.equals(uri1, uri2); |
| 137 } | 139 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 test(new Uri(queryParameters: {"a": "b", "c": "d"}).queryParameters); | 173 test(new Uri(queryParameters: {"a": "b", "c": "d"}).queryParameters); |
| 172 } | 174 } |
| 173 | 175 |
| 174 main() { | 176 main() { |
| 175 testInvalidArguments(); | 177 testInvalidArguments(); |
| 176 testEncodeQueryComponent(); | 178 testEncodeQueryComponent(); |
| 177 testQueryParameters(); | 179 testQueryParameters(); |
| 178 testInvalidQueryParameters(); | 180 testInvalidQueryParameters(); |
| 179 testQueryParametersImmutableMap(); | 181 testQueryParametersImmutableMap(); |
| 180 } | 182 } |
| OLD | NEW |