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:async"; | 5 import "dart:async"; |
6 import "dart:io"; | 6 import "dart:io"; |
7 import "dart:typed_data"; | 7 import "dart:typed_data"; |
8 | 8 |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 | 11 |
12 void testClientRequest(Future handler(request)) { | 12 void testClientRequest(Future handler(request)) { |
13 HttpServer.bind("127.0.0.1", 0).then((server) { | 13 HttpServer.bind("127.0.0.1", 0).then((server) { |
14 server.listen((request) { | 14 server.listen((request) { |
15 request.listen((_) {}, onDone: () { | 15 request.drain() |
16 request.response.close(); | 16 .then((_) => request.response.close()) |
17 }, onError: (e) {}); | 17 .catchError((_) {}); |
18 }); | 18 }); |
19 | 19 |
20 var client = new HttpClient(); | 20 var client = new HttpClient(); |
21 client.get("127.0.0.1", server.port, "/") | 21 client.get("127.0.0.1", server.port, "/") |
22 .then((request) { | 22 .then((request) { |
23 return handler(request); | 23 return handler(request); |
24 }) | 24 }) |
25 .then((response) { | 25 .then((response) => response.drain()) |
26 response.listen((_) {}, onDone: () { | 26 .catchError((_) {}) |
27 client.close(); | 27 .whenComplete(() { |
28 server.close(); | 28 client.close(); |
29 }); | |
30 }) | |
31 .catchError((error) { | |
32 server.close(); | 29 server.close(); |
33 client.close(); | |
34 }); | 30 }); |
35 }); | 31 }); |
36 } | 32 } |
37 | 33 |
38 void testResponseDone() { | 34 void testResponseDone() { |
39 testClientRequest((request) { | 35 testClientRequest((request) { |
40 request.close().then((res1) { | 36 request.close().then((res1) { |
41 request.done.then((res2) { | 37 request.done.then((res2) { |
42 Expect.equals(res1, res2); | 38 Expect.equals(res1, res2); |
43 }); | 39 }); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 }, test: (e) => e is HttpException); | 101 }, test: (e) => e is HttpException); |
106 return request.done; | 102 return request.done; |
107 }); | 103 }); |
108 } | 104 } |
109 | 105 |
110 void main() { | 106 void main() { |
111 testResponseDone(); | 107 testResponseDone(); |
112 testBadResponseAdd(); | 108 testBadResponseAdd(); |
113 testBadResponseClose(); | 109 testBadResponseClose(); |
114 } | 110 } |
OLD | NEW |