| 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 } | 8 } |
| 9 | 9 |
| 10 void testEncodeQueryComponent() { | 10 void testEncodeQueryComponent() { |
| 11 // This exact data is from posting a form in Chrome 26 with the one | 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. | 12 // exception that * is encoded as %30 and ~ is not encoded as %7E. |
| 13 Expect.equals( | 13 Expect.equals( |
| 14 "%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%" | 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~", | 15 "3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~", |
| 16 Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); | 16 Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~")); |
| 17 Expect.equals("+%2B+", Uri.encodeQueryComponent(" + ")); | 17 Expect.equals("+%2B+", Uri.encodeQueryComponent(" + ")); |
| 18 Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +")); | 18 Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +")); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void testQueryParameters() { | 21 void testQueryParameters() { |
| 22 test(String query, Map<String, String> parameters) { | 22 test(String query, Map<String, String> parameters, [String normalizedQuery]) { |
| 23 if (normalizedQuery === null) normalizedQuery = query; |
| 23 check(uri) { | 24 check(uri) { |
| 24 Expect.equals(query, uri.query); | 25 Expect.equals(normalizedQuery, uri.query); |
| 25 if (query.isEmpty) { | 26 if (query.isEmpty) { |
| 26 Expect.equals(query, uri.toString()); | 27 Expect.equals(normalizedQuery, uri.toString()); |
| 27 } else { | 28 } else { |
| 28 Expect.equals("?$query", uri.toString()); | 29 Expect.equals("?$normalizedQuery", uri.toString()); |
| 29 } | 30 } |
| 30 if (parameters.containsValue(null)) { | 31 if (parameters.containsValue(null)) { |
| 31 } else { | 32 } else { |
| 32 Expect.mapEquals(parameters, uri.queryParameters); | 33 Expect.mapEquals(parameters, uri.queryParameters); |
| 33 } | 34 } |
| 34 } | 35 } |
| 35 | 36 |
| 36 var uri1 = new Uri(queryParameters: parameters); | 37 var uri1 = new Uri(queryParameters: parameters); |
| 37 var uri2 = new Uri(query: query); | 38 var uri2 = new Uri(query: query); |
| 38 var uri3 = Uri.parse("?$query"); | 39 var uri3 = Uri.parse("?$query"); |
| 39 check(uri1); | 40 check(uri1); |
| 40 check(uri2); | 41 check(uri2); |
| 41 check(uri3); | 42 check(uri3); |
| 42 Expect.equals(uri1, uri3); | 43 Expect.equals(uri1, uri3); |
| 43 Expect.equals(uri2, uri3); | 44 Expect.equals(uri2, uri3); |
| 44 } | 45 } |
| 45 | 46 |
| 46 test("", {}); | 47 test("", {}); |
| 47 test("A", {"A": null}); | 48 test("A", {"A": null}); |
| 49 test("%25", {"%": null}); |
| 50 test("%41", {"A": null}, "A"); |
| 51 test("%41A", {"AA": null}, "AA"); |
| 48 test("A", {"A": ""}); | 52 test("A", {"A": ""}); |
| 53 test("%25", {"%": ""}); |
| 54 test("%41", {"A": ""}, "A"); |
| 55 test("%41A", {"AA": ""}, "AA"); |
| 49 test("A=a", {"A": "a"}); | 56 test("A=a", {"A": "a"}); |
| 57 test("%25=a", {"%": "a"}); |
| 58 test("%41=%61", {"A": "a"}, "A=a"); |
| 50 test("A=+", {"A": " "}); | 59 test("A=+", {"A": " "}); |
| 51 test("A=%2B", {"A": "+"}); | 60 test("A=%2B", {"A": "+"}); |
| 52 test("A=a&B", {"A": "a", "B": null}); | 61 test("A=a&B", {"A": "a", "B": null}); |
| 53 test("A=a&B", {"A": "a", "B": ""}); | 62 test("A=a&B", {"A": "a", "B": ""}); |
| 54 test("A=a&B=b", {"A": "a", "B": "b"}); | 63 test("A=a&B=b", {"A": "a", "B": "b"}); |
| 64 test("%41=%61&%42=%62", {"A": "a", "B": "b"}, "A=a&B=b"); |
| 55 | 65 |
| 56 var unreserved = "-._~0123456789" | 66 var unreserved = "-._~0123456789" |
| 57 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | 67 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 58 "abcdefghijklmnopqrstuvwxyz"; | 68 "abcdefghijklmnopqrstuvwxyz"; |
| 59 var encoded = new StringBuffer(); | 69 var encoded = new StringBuffer(); |
| 70 var allEncoded = new StringBuffer(); |
| 60 var unencoded = new StringBuffer(); | 71 var unencoded = new StringBuffer(); |
| 61 for (int i = 32; i < 128; i++) { | 72 for (int i = 32; i < 128; i++) { |
| 62 if (i == 32) { | 73 if (i == 32) { |
| 63 encoded.write("+"); | 74 encoded.write("+"); |
| 64 } else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) { | 75 } else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) { |
| 65 encoded.writeCharCode(i); | 76 encoded.writeCharCode(i); |
| 66 } else { | 77 } else { |
| 67 encoded.write("%"); | 78 encoded.write("%"); |
| 68 encoded.write(i.toRadixString(16).toUpperCase()); | 79 encoded.write(i.toRadixString(16).toUpperCase()); |
| 69 } | 80 } |
| 81 if (i == 32) { |
| 82 allEncoded.write("+"); |
| 83 } else { |
| 84 allEncoded.write("%"); |
| 85 allEncoded.write(i.toRadixString(16).toUpperCase()); |
| 86 } |
| 70 unencoded.writeCharCode(i); | 87 unencoded.writeCharCode(i); |
| 71 } | 88 } |
| 72 encoded = encoded.toString(); | 89 encoded = encoded.toString(); |
| 73 unencoded = unencoded.toString(); | 90 unencoded = unencoded.toString(); |
| 74 print(encoded); | |
| 75 print(unencoded); | |
| 76 test("a=$encoded", {"a": unencoded}); | 91 test("a=$encoded", {"a": unencoded}); |
| 77 test("a=$encoded&b=$encoded", {"a": unencoded, "b": unencoded}); | 92 test("a=$encoded&b=$encoded", {"a": unencoded, "b": unencoded}); |
| 78 | 93 |
| 79 var map = new Map(); | 94 var map = new Map(); |
| 80 map[unencoded] = unencoded; | 95 map[unencoded] = unencoded; |
| 81 test("$encoded=$encoded", map); | 96 test("$encoded=$encoded", map); |
| 97 test("$encoded=$allEncoded", map, "$encoded=$encoded"); |
| 98 test("$allEncoded=$encoded", map, "$encoded=$encoded"); |
| 99 test("$allEncoded=$allEncoded", map, "$encoded=$encoded"); |
| 100 map[unencoded] = null; |
| 101 test("$encoded", map); |
| 102 map[unencoded] = ""; |
| 103 test("$encoded", map); |
| 82 } | 104 } |
| 83 | 105 |
| 84 testInvalidQueryParameters() { | 106 testInvalidQueryParameters() { |
| 85 test(String query, Map<String, String> parameters) { | 107 test(String query, Map<String, String> parameters) { |
| 86 check(uri) { | 108 check(uri) { |
| 87 Expect.equals(query, uri.query); | 109 Expect.equals(query, uri.query); |
| 88 if (query.isEmpty) { | 110 if (query.isEmpty) { |
| 89 Expect.equals(query, uri.toString()); | 111 Expect.equals(query, uri.toString()); |
| 90 } else { | 112 } else { |
| 91 Expect.equals("?$query", uri.toString()); | 113 Expect.equals("?$query", uri.toString()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 112 test("&=&=&", {}); | 134 test("&=&=&", {}); |
| 113 test("&=xxx&=xxx&", {}); | 135 test("&=xxx&=xxx&", {}); |
| 114 } | 136 } |
| 115 | 137 |
| 116 main() { | 138 main() { |
| 117 testInvalidArguments(); | 139 testInvalidArguments(); |
| 118 testEncodeQueryComponent(); | 140 testEncodeQueryComponent(); |
| 119 testQueryParameters(); | 141 testQueryParameters(); |
| 120 testInvalidQueryParameters(); | 142 testInvalidQueryParameters(); |
| 121 } | 143 } |
| OLD | NEW |