| 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 import "dart:async"; | 6 import "dart:async"; |
| 7 import "dart:utf"; | 7 import "dart:utf"; |
| 8 | 8 |
| 9 part '../../../sdk/lib/io/io_sink.dart'; | 9 part '../../../sdk/lib/io/io_sink.dart'; |
| 10 part "../../../sdk/lib/io/http.dart"; | 10 part "../../../sdk/lib/io/http.dart"; |
| 11 part "../../../sdk/lib/io/http_impl.dart"; | 11 part "../../../sdk/lib/io/http_impl.dart"; |
| 12 part "../../../sdk/lib/io/http_parser.dart"; | 12 part "../../../sdk/lib/io/http_parser.dart"; |
| 13 part "../../../sdk/lib/io/http_utils.dart"; | 13 part "../../../sdk/lib/io/http_utils.dart"; |
| 14 part "../../../sdk/lib/io/socket.dart"; | 14 part "../../../sdk/lib/io/socket.dart"; |
| 15 | 15 |
| 16 void testParseEncodedString() { | 16 void testParseEncodedString() { |
| 17 String encodedString = 'foo+bar%20foobar%25%26'; | 17 String encodedString = 'foo+bar%20foobar%25%26'; |
| 18 Expect.equals(_HttpUtils.decodeUrlEncodedString(encodedString), | 18 Expect.equals(_HttpUtils.decodeUrlEncodedString(encodedString), |
| 19 'foo bar foobar%&'); | 19 'foo bar foobar%&'); |
| 20 encodedString = 'A+%2B+B'; | 20 encodedString = 'A+%2B+B'; |
| 21 Expect.equals(_HttpUtils.decodeUrlEncodedString(encodedString), | 21 Expect.equals(_HttpUtils.decodeUrlEncodedString(encodedString), |
| 22 'A + B'); | 22 'A + B'); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void testParseQueryString() { | 25 void testParseQueryString() { |
| 26 test(String queryString, Map<String, String> expected) { |
| 27 Map<String, String> map = _HttpUtils.splitQueryString(queryString); |
| 28 for (String key in map.keys) { |
| 29 Expect.equals(expected[key], map[key]); |
| 30 } |
| 31 Expect.setEquals(expected.keys.toSet(), map.keys.toSet()); |
| 32 } |
| 33 |
| 26 // The query string includes escaped "?"s, "&"s, "%"s and "="s. | 34 // The query string includes escaped "?"s, "&"s, "%"s and "="s. |
| 27 // These should not affect the splitting of the string. | 35 // These should not affect the splitting of the string. |
| 28 String queryString = | 36 test('%3F=%3D&foo=bar&%26=%25&sqrt2=%E2%88%9A2&name=Franti%C5%A1ek', |
| 29 '%3F=%3D&foo=bar&%26=%25&sqrt2=%E2%88%9A2&name=Franti%C5%A1ek'; | 37 { '&' : '%', |
| 30 Map<String, String> map = _HttpUtils.splitQueryString(queryString); | 38 'foo' : 'bar', |
| 31 for (String key in map.keys) { | 39 '?' : '=', |
| 32 Expect.equals(map[key], { '&' : '%', | 40 'sqrt2' : '\u221A2', |
| 33 'foo' : 'bar', | 41 'name' : 'Franti\u0161ek'}); |
| 34 '?' : '=', | 42 |
| 35 'sqrt2' : '\u221A2', | 43 // Same query string with ; as separator. |
| 36 'name' : 'Franti\u0161ek'}[key]); | 44 test('%3F=%3D;foo=bar;%26=%25;sqrt2=%E2%88%9A2;name=Franti%C5%A1ek', |
| 37 } | 45 { '&' : '%', |
| 38 Expect.setEquals(map.keys.toSet(), ['&', '?', 'foo', 'sqrt2', 'name']); | 46 'foo' : 'bar', |
| 47 '?' : '=', |
| 48 'sqrt2' : '\u221A2', |
| 49 'name' : 'Franti\u0161ek'}); |
| 50 |
| 51 // Same query string with alternating ; and & separators. |
| 52 test('%3F=%3D&foo=bar;%26=%25&sqrt2=%E2%88%9A2;name=Franti%C5%A1ek', |
| 53 { '&' : '%', |
| 54 'foo' : 'bar', |
| 55 '?' : '=', |
| 56 'sqrt2' : '\u221A2', |
| 57 'name' : 'Franti\u0161ek'}); |
| 58 test('%3F=%3D;foo=bar&%26=%25;sqrt2=%E2%88%9A2&name=Franti%C5%A1ek', |
| 59 { '&' : '%', |
| 60 'foo' : 'bar', |
| 61 '?' : '=', |
| 62 'sqrt2' : '\u221A2', |
| 63 'name' : 'Franti\u0161ek'}); |
| 64 |
| 65 // Corner case tests. |
| 66 test('', { }); |
| 67 test('&', { }); |
| 68 test(';', { }); |
| 69 test('&;', { }); |
| 70 test(';&', { }); |
| 71 test('&&&&', { }); |
| 72 test(';;;;', { }); |
| 73 test('a', { 'a' : '' }); |
| 74 test('&a&', { 'a' : '' }); |
| 75 test(';a;', { 'a' : '' }); |
| 76 test('a=', { 'a' : '' }); |
| 77 test('a=&', { 'a' : '' }); |
| 78 test('a=;', { 'a' : '' }); |
| 79 test('a=&b', { 'a' : '', 'b' : '' }); |
| 80 test('a=;b', { 'a' : '', 'b' : '' }); |
| 81 test('a=&b', { 'a' : '', 'b' : '' }); |
| 82 test('a=&b=', { 'a' : '', 'b' : '' }); |
| 83 |
| 84 // These are not really a legal query string. |
| 85 test('=', { }); |
| 86 test('=x', { }); |
| 87 test('a==&b===', { 'a' : '=', 'b' : '==' }); |
| 39 } | 88 } |
| 40 | 89 |
| 41 void main() { | 90 void main() { |
| 42 testParseEncodedString(); | 91 testParseEncodedString(); |
| 43 testParseQueryString(); | 92 testParseQueryString(); |
| 44 } | 93 } |
| OLD | NEW |