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 void test(String mimeType, | 11 void test(String mimeType, |
12 List<int> content, | 12 List<int> content, |
13 dynamic expectedBody, | 13 dynamic expectedBody, |
14 String type, | 14 String type, |
15 [bool shouldFail = false]) { | 15 [bool shouldFail = false]) { |
16 HttpServer.bind().then((server) { | 16 HttpServer.bind("127.0.0.1", 0).then((server) { |
17 server.listen((request) { | 17 server.listen((request) { |
18 request.listen( | 18 request.listen( |
19 (_) {}, | 19 (_) {}, |
20 onDone: () { | 20 onDone: () { |
21 request.response.headers.contentType = | 21 request.response.headers.contentType = |
22 new ContentType.fromString(mimeType); | 22 new ContentType.fromString(mimeType); |
23 request.response.add(content); | 23 request.response.add(content); |
24 request.response.close(); | 24 request.response.close(); |
25 }); | 25 }); |
26 }); | 26 }); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 HttpServer.bind().then((server) { | 94 HttpServer.bind("127.0.0.1", 0).then((server) { |
95 server.transform(new HttpBodyHandler()) | 95 server.transform(new HttpBodyHandler()) |
96 .listen((body) { | 96 .listen((body) { |
97 if (shouldFail) Expect.fail("Error expected"); | 97 if (shouldFail) Expect.fail("Error expected"); |
98 Expect.equals(type, body.type); | 98 Expect.equals(type, body.type); |
99 switch (type) { | 99 switch (type) { |
100 case "text": | 100 case "text": |
101 Expect.equals(body.mimeType, "text/plain"); | 101 Expect.equals(body.mimeType, "text/plain"); |
102 Expect.equals(expectedBody, body.body); | 102 Expect.equals(expectedBody, body.body); |
103 break; | 103 break; |
104 | 104 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 "json", | 162 "json", |
163 true); | 163 true); |
164 | 164 |
165 test(null, "body".codeUnits, "body".codeUnits, "binary"); | 165 test(null, "body".codeUnits, "body".codeUnits, "binary"); |
166 } | 166 } |
167 | 167 |
168 void main() { | 168 void main() { |
169 testHttpClientResponseBody(); | 169 testHttpClientResponseBody(); |
170 testHttpServerRequestBody(); | 170 testHttpServerRequestBody(); |
171 } | 171 } |
OLD | NEW |