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() { |
11 new HttpBodyHandler(); | 11 new HttpBodyHandler(); |
12 void test(String mimeType, | 12 void test(String mimeType, |
13 List<int> content, | 13 List<int> content, |
14 dynamic expectedBody, | 14 dynamic expectedBody, |
15 String type, | 15 String type, |
16 [bool shouldFail = false]) { | 16 [bool shouldFail = false]) { |
17 HttpServer.bind().then((server) { | 17 HttpServer.bind().then((server) { |
18 server.listen((request) { | 18 server.listen((request) { |
19 request.listen( | 19 request.listen( |
20 (_) {}, | 20 (_) {}, |
21 onDone: () { | 21 onDone: () { |
22 request.response.headers.contentType = | 22 request.response.headers.contentType = |
23 new ContentType.fromString(mimeType); | 23 new ContentType.fromString(mimeType); |
24 request.response.writeBytes(content); | 24 request.response.add(content); |
25 request.response.close(); | 25 request.response.close(); |
26 }); | 26 }); |
27 }); | 27 }); |
28 | 28 |
29 var client = new HttpClient(); | 29 var client = new HttpClient(); |
30 client.get("127.0.0.1", server.port, "/") | 30 client.get("127.0.0.1", server.port, "/") |
31 .then((request) => request.close()) | 31 .then((request) => request.close()) |
32 .then(HttpBodyHandler.processResponse) | 32 .then(HttpBodyHandler.processResponse) |
33 .then((body) { | 33 .then((body) { |
34 if (shouldFail) Expect.fail("Error expected"); | 34 if (shouldFail) Expect.fail("Error expected"); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 body.response.close(); | 111 body.response.close(); |
112 }, onError: (error) { | 112 }, onError: (error) { |
113 if (!shouldFail) Expect.fail("Error unexpected"); | 113 if (!shouldFail) Expect.fail("Error unexpected"); |
114 }); | 114 }); |
115 | 115 |
116 var client = new HttpClient(); | 116 var client = new HttpClient(); |
117 client.post("127.0.0.1", server.port, "/") | 117 client.post("127.0.0.1", server.port, "/") |
118 .then((request) { | 118 .then((request) { |
119 request.headers.contentType = | 119 request.headers.contentType = |
120 new ContentType.fromString(mimeType); | 120 new ContentType.fromString(mimeType); |
121 request.writeBytes(content); | 121 request.add(content); |
122 return request.close(); | 122 return request.close(); |
123 }) | 123 }) |
124 .then((response) { | 124 .then((response) { |
125 if (shouldFail) { | 125 if (shouldFail) { |
126 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 126 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
127 } | 127 } |
128 response.fold(null, (x, y) {}); | 128 response.fold(null, (x, y) {}); |
129 client.close(); | 129 client.close(); |
130 server.close(); | 130 server.close(); |
131 }); | 131 }); |
(...skipping 20 matching lines...) Expand all Loading... |
152 null, | 152 null, |
153 "json", | 153 "json", |
154 true); | 154 true); |
155 } | 155 } |
156 | 156 |
157 | 157 |
158 void main() { | 158 void main() { |
159 testHttpClientResponseBody(); | 159 testHttpClientResponseBody(); |
160 testHttpServerRequestBody(); | 160 testHttpServerRequestBody(); |
161 } | 161 } |
OLD | NEW |