| 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 'dart:io'; |
| 6 import 'dart:utf'; |
| 7 |
| 8 import 'package:expect/expect.dart'; |
| 9 |
| 10 void testHttpClientResponseBody() { |
| 11 void test(String mimeType, |
| 12 List<int> content, |
| 13 dynamic expectedBody, |
| 14 HttpBodyType type, |
| 15 [bool shouldFail = false]) { |
| 16 HttpServer.bind().then((server) { |
| 17 server.listen((request) { |
| 18 request.listen( |
| 19 (_) {}, |
| 20 onDone: () { |
| 21 request.response.headers.contentType = |
| 22 new ContentType.fromString(mimeType); |
| 23 request.response.writeBytes(content); |
| 24 request.response.close(); |
| 25 }); |
| 26 }); |
| 27 |
| 28 var client = new HttpClient(); |
| 29 client.get("127.0.0.1", server.port, "/") |
| 30 .then((request) => request.close()) |
| 31 .then(HttpBodyHandler.processResponse) |
| 32 .then((body) { |
| 33 if (shouldFail) Expect.fail("Error expected"); |
| 34 Expect.equals(type, body.type); |
| 35 switch (type) { |
| 36 case HttpBodyType.TEXT: |
| 37 Expect.equals(expectedBody, body.body); |
| 38 break; |
| 39 |
| 40 case HttpBodyType.JSON: |
| 41 Expect.mapEquals(expectedBody, body.body); |
| 42 break; |
| 43 |
| 44 default: |
| 45 Expect.fail("bad body type"); |
| 46 } |
| 47 }, onError: (error) { |
| 48 if (!shouldFail) Expect.fail("Error unexpected"); |
| 49 }) |
| 50 .whenComplete(() { |
| 51 client.close(); |
| 52 server.close(); |
| 53 }); |
| 54 }); |
| 55 } |
| 56 test("text/plain", "body".codeUnits, "body", HttpBodyType.TEXT); |
| 57 test("text/plain; charset=utf-8", |
| 58 "body".codeUnits, |
| 59 "body", |
| 60 HttpBodyType.TEXT); |
| 61 test("text/plain; charset=iso-8859-1", |
| 62 "body".codeUnits, |
| 63 "body", |
| 64 HttpBodyType.TEXT); |
| 65 test("text/plain; charset=us-ascii", |
| 66 "body".codeUnits, |
| 67 "body", |
| 68 HttpBodyType.TEXT); |
| 69 test("text/plain; charset=utf-8", [42], "*", HttpBodyType.TEXT); |
| 70 test("text/plain; charset=us-ascii", [142], "?", HttpBodyType.TEXT); |
| 71 test("text/plain; charset=utf-8", |
| 72 [142], |
| 73 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| 74 HttpBodyType.TEXT); |
| 75 |
| 76 test("application/json", |
| 77 '{"val": 5}'.codeUnits, |
| 78 { "val" : 5 }, |
| 79 HttpBodyType.JSON); |
| 80 test("application/json", |
| 81 '{ bad json }'.codeUnits, |
| 82 null, |
| 83 HttpBodyType.JSON, |
| 84 true); |
| 85 } |
| 86 |
| 87 void testHttpServerRequestBody() { |
| 88 void test(String mimeType, |
| 89 List<int> content, |
| 90 dynamic expectedBody, |
| 91 int type, |
| 92 [bool shouldFail = false]) { |
| 93 HttpServer.bind().then((server) { |
| 94 server.transform(new HttpBodyHandler()) |
| 95 .listen((body) { |
| 96 if (shouldFail) Expect.fail("Error expected"); |
| 97 Expect.equals(type, body.type); |
| 98 switch (type) { |
| 99 case HttpBodyType.TEXT: |
| 100 Expect.equals(expectedBody, body.body); |
| 101 break; |
| 102 |
| 103 case HttpBodyType.JSON: |
| 104 Expect.mapEquals(expectedBody, body.body); |
| 105 break; |
| 106 |
| 107 default: |
| 108 Expect.fail("bad body type"); |
| 109 } |
| 110 body.response.close(); |
| 111 }, onError: (error) { |
| 112 if (!shouldFail) Expect.fail("Error unexpected"); |
| 113 }); |
| 114 |
| 115 var client = new HttpClient(); |
| 116 client.post("127.0.0.1", server.port, "/") |
| 117 .then((request) { |
| 118 request.headers.contentType = |
| 119 new ContentType.fromString(mimeType); |
| 120 request.writeBytes(content); |
| 121 return request.close(); |
| 122 }) |
| 123 .then((response) { |
| 124 if (shouldFail) { |
| 125 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 126 } |
| 127 response.fold(null, (x, y) {}); |
| 128 client.close(); |
| 129 server.close(); |
| 130 }); |
| 131 }); |
| 132 } |
| 133 test("text/plain", "body".codeUnits, "body", HttpBodyType.TEXT); |
| 134 test("text/plain; charset=utf-8", |
| 135 "body".codeUnits, |
| 136 "body", |
| 137 HttpBodyType.TEXT); |
| 138 test("text/plain; charset=utf-8", [42], "*", HttpBodyType.TEXT); |
| 139 test("text/plain; charset=us-ascii", [142], "?", HttpBodyType.TEXT); |
| 140 test("text/plain; charset=utf-8", |
| 141 [142], |
| 142 new String.fromCharCodes([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT]), |
| 143 HttpBodyType.TEXT); |
| 144 |
| 145 test("application/json", |
| 146 '{"val": 5}'.codeUnits, |
| 147 { "val" : 5 }, |
| 148 HttpBodyType.JSON); |
| 149 test("application/json", |
| 150 '{ bad json }'.codeUnits, |
| 151 null, |
| 152 HttpBodyType.JSON, |
| 153 true); |
| 154 } |
| 155 |
| 156 |
| 157 void main() { |
| 158 testHttpClientResponseBody(); |
| 159 testHttpServerRequestBody(); |
| 160 } |
| OLD | NEW |