| 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 'dart:io'; | 5 import 'dart:io'; |
| 6 import 'dart:utf'; | 6 import 'dart:utf'; |
| 7 | 7 |
| 8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
| 9 | 9 |
| 10 void testHttpClientResponseBody() { | 10 void testHttpClientResponseBody() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 break; | 39 break; |
| 40 | 40 |
| 41 case "json": | 41 case "json": |
| 42 Expect.mapEquals(expectedBody, body.body); | 42 Expect.mapEquals(expectedBody, body.body); |
| 43 break; | 43 break; |
| 44 | 44 |
| 45 default: | 45 default: |
| 46 Expect.fail("bad body type"); | 46 Expect.fail("bad body type"); |
| 47 } | 47 } |
| 48 }, onError: (error) { | 48 }, onError: (error) { |
| 49 if (!shouldFail) Expect.fail("Error unexpected"); | 49 if (!shouldFail) throw error; |
| 50 }) | 50 }) |
| 51 .whenComplete(() { | 51 .whenComplete(() { |
| 52 client.close(); | 52 client.close(); |
| 53 server.close(); | 53 server.close(); |
| 54 }); | 54 }); |
| 55 }); | 55 }); |
| 56 } | 56 } |
| 57 test("text/plain", "body".codeUnits, "body", "text"); | 57 test("text/plain", "body".codeUnits, "body", "text"); |
| 58 test("text/plain; charset=utf-8", | 58 test("text/plain; charset=utf-8", |
| 59 "body".codeUnits, | 59 "body".codeUnits, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 83 null, | 83 null, |
| 84 "json", | 84 "json", |
| 85 true); | 85 true); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void testHttpServerRequestBody() { | 88 void testHttpServerRequestBody() { |
| 89 void test(String mimeType, | 89 void test(String mimeType, |
| 90 List<int> content, | 90 List<int> content, |
| 91 dynamic expectedBody, | 91 dynamic expectedBody, |
| 92 String type, | 92 String type, |
| 93 {bool shouldFail: false}) { | 93 {bool shouldFail: false, |
| 94 Encoding defaultEncoding: Encoding.UTF_8}) { |
| 94 HttpServer.bind("127.0.0.1", 0).then((server) { | 95 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 95 server.transform(new HttpBodyHandler()) | 96 server.transform(new HttpBodyHandler(defaultEncoding: defaultEncoding)) |
| 96 .listen((body) { | 97 .listen((body) { |
| 97 if (shouldFail) Expect.fail("Error expected"); | 98 if (shouldFail) Expect.fail("Error expected"); |
| 98 Expect.equals(type, body.type); | 99 Expect.equals(type, body.type); |
| 99 switch (type) { | 100 switch (type) { |
| 100 case "text": | 101 case "text": |
| 101 Expect.equals(body.contentType.mimeType, "text/plain"); | 102 Expect.equals(body.contentType.mimeType, "text/plain"); |
| 102 Expect.equals(expectedBody, body.body); | 103 Expect.equals(expectedBody, body.body); |
| 103 break; | 104 break; |
| 104 | 105 |
| 105 case "json": | 106 case "json": |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 .then((response) { | 160 .then((response) { |
| 160 if (shouldFail) { | 161 if (shouldFail) { |
| 161 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 162 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 162 } | 163 } |
| 163 response.fold(null, (x, y) {}); | 164 response.fold(null, (x, y) {}); |
| 164 client.close(); | 165 client.close(); |
| 165 server.close(); | 166 server.close(); |
| 166 }); | 167 }); |
| 167 }); | 168 }); |
| 168 } | 169 } |
| 170 |
| 169 test("text/plain", "body".codeUnits, "body", "text"); | 171 test("text/plain", "body".codeUnits, "body", "text"); |
| 170 test("text/plain; charset=utf-8", | 172 test("text/plain; charset=utf-8", |
| 171 "body".codeUnits, | 173 "body".codeUnits, |
| 172 "body", | 174 "body", |
| 173 "text"); | 175 "text"); |
| 174 test("text/plain; charset=utf-8", [42], "*", "text"); | 176 test("text/plain; charset=utf-8", [42], "*", "text"); |
| 175 test("text/plain; charset=us-ascii", [142], "?", "text"); | 177 test("text/plain; charset=us-ascii", [142], "?", "text"); |
| 176 test("text/plain; charset=utf-8", | 178 test("text/plain; charset=utf-8", |
| 177 [142], | 179 [142], |
| 178 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), | 180 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 'contentType': 'application/json', | 245 'contentType': 'application/json', |
| 244 'content': 'File content'} }, | 246 'content': 'File content'} }, |
| 245 "form"); | 247 "form"); |
| 246 | 248 |
| 247 test('application/x-www-form-urlencoded', | 249 test('application/x-www-form-urlencoded', |
| 248 '%E5%B9%B3%3D%E4%BB%AE%E5%90%8D=%26%2324179%3B%26%2320206%3B%26%' | 250 '%E5%B9%B3%3D%E4%BB%AE%E5%90%8D=%26%2324179%3B%26%2320206%3B%26%' |
| 249 '2321517%3B&b=%26%2324179%3B%26%2320206%3B%26%2321517%3B'.codeUnits, | 251 '2321517%3B&b=%26%2324179%3B%26%2320206%3B%26%2321517%3B'.codeUnits, |
| 250 { 'b' : '平仮名', | 252 { 'b' : '平仮名', |
| 251 '平=仮名' : '平仮名'}, | 253 '平=仮名' : '平仮名'}, |
| 252 "form"); | 254 "form"); |
| 255 |
| 256 test('application/x-www-form-urlencoded', |
| 257 'a=%F8+%26%23548%3B'.codeUnits, |
| 258 { 'a' : '\u{FFFD}\u{0224}' }, |
| 259 "form"); |
| 260 |
| 261 test('application/x-www-form-urlencoded', |
| 262 'a=%F8+%26%23548%3B'.codeUnits, |
| 263 { 'a' : 'ø Ȥ' }, |
| 264 "form", |
| 265 defaultEncoding: Encoding.ISO_8859_1); |
| 266 |
| 267 test('application/x-www-form-urlencoded', |
| 268 'a=%C3%B8+%C8%A4'.codeUnits, |
| 269 { 'a' : 'ø Ȥ' }, |
| 270 "form"); |
| 253 } | 271 } |
| 254 | 272 |
| 255 void main() { | 273 void main() { |
| 256 testHttpClientResponseBody(); | 274 testHttpClientResponseBody(); |
| 257 testHttpServerRequestBody(); | 275 testHttpServerRequestBody(); |
| 258 } | 276 } |
| OLD | NEW |