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 | |
13 void testClientRequest(Future handler(request)) { | 12 void testClientRequest(Future handler(request)) { |
14 HttpServer.bind("127.0.0.1", 0).then((server) { | 13 HttpServer.bind("127.0.0.1", 0).then((server) { |
15 server.listen((request) { | 14 server.listen((request) { |
16 request.drain() | 15 request.drain().then((_) => request.response.close()).catchError((_) {}); |
17 .then((_) => request.response.close()) | |
18 .catchError((_) {}); | |
19 }); | 16 }); |
20 | 17 |
21 var client = new HttpClient(); | 18 var client = new HttpClient(); |
22 client.get("127.0.0.1", server.port, "/") | 19 client |
23 .then((request) { | 20 .get("127.0.0.1", server.port, "/") |
24 return handler(request); | 21 .then((request) { |
25 }) | 22 return handler(request); |
26 .then((response) => response.drain()) | 23 }) |
27 .catchError((_) {}) | 24 .then((response) => response.drain()) |
28 .whenComplete(() { | 25 .catchError((_) {}) |
29 client.close(); | 26 .whenComplete(() { |
30 server.close(); | 27 client.close(); |
31 }); | 28 server.close(); |
| 29 }); |
32 }); | 30 }); |
33 } | 31 } |
34 | 32 |
35 | |
36 void testResponseDone() { | 33 void testResponseDone() { |
37 testClientRequest((request) { | 34 testClientRequest((request) { |
38 request.close().then((res1) { | 35 request.close().then((res1) { |
39 request.done.then((res2) { | 36 request.done.then((res2) { |
40 Expect.equals(res1, res2); | 37 Expect.equals(res1, res2); |
41 }); | 38 }); |
42 }); | 39 }); |
43 return request.done; | 40 return request.done; |
44 }); | 41 }); |
45 } | 42 } |
46 | 43 |
47 | |
48 void testBadResponseAdd() { | 44 void testBadResponseAdd() { |
49 asyncStart(); | 45 asyncStart(); |
50 testClientRequest((request) { | 46 testClientRequest((request) { |
51 request.contentLength = 0; | 47 request.contentLength = 0; |
52 request.add([0]); | 48 request.add([0]); |
53 request.close(); | 49 request.close(); |
54 request.done.catchError((error) { | 50 request.done.catchError((error) { |
55 asyncEnd(); | 51 asyncEnd(); |
56 }, test: (e) => e is HttpException); | 52 }, test: (e) => e is HttpException); |
57 return request.done; | 53 return request.done; |
(...skipping 18 matching lines...) Expand all Loading... |
76 request.add(new Uint8List(64 * 1024)); | 72 request.add(new Uint8List(64 * 1024)); |
77 request.add(new Uint8List(64 * 1024)); | 73 request.add(new Uint8List(64 * 1024)); |
78 request.close(); | 74 request.close(); |
79 request.done.catchError((error) { | 75 request.done.catchError((error) { |
80 asyncEnd(); | 76 asyncEnd(); |
81 }, test: (e) => e is HttpException); | 77 }, test: (e) => e is HttpException); |
82 return request.done; | 78 return request.done; |
83 }); | 79 }); |
84 } | 80 } |
85 | 81 |
86 | |
87 void testBadResponseClose() { | 82 void testBadResponseClose() { |
88 asyncStart(); | 83 asyncStart(); |
89 testClientRequest((request) { | 84 testClientRequest((request) { |
90 request.contentLength = 5; | 85 request.contentLength = 5; |
91 request.close(); | 86 request.close(); |
92 request.done.catchError((error) { | 87 request.done.catchError((error) { |
93 asyncEnd(); | 88 asyncEnd(); |
94 }, test: (e) => e is HttpException); | 89 }, test: (e) => e is HttpException); |
95 return request.done; | 90 return request.done; |
96 }); | 91 }); |
97 | 92 |
98 asyncStart(); | 93 asyncStart(); |
99 testClientRequest((request) { | 94 testClientRequest((request) { |
100 request.contentLength = 5; | 95 request.contentLength = 5; |
101 request.add([0]); | 96 request.add([0]); |
102 request.close(); | 97 request.close(); |
103 request.done.catchError((error) { | 98 request.done.catchError((error) { |
104 asyncEnd(); | 99 asyncEnd(); |
105 }, test: (e) => e is HttpException); | 100 }, test: (e) => e is HttpException); |
106 return request.done; | 101 return request.done; |
107 }); | 102 }); |
108 } | 103 } |
109 | 104 |
110 | |
111 void main() { | 105 void main() { |
112 testResponseDone(); | 106 testResponseDone(); |
113 testBadResponseAdd(); | 107 testBadResponseAdd(); |
114 testBadResponseClose(); | 108 testBadResponseClose(); |
115 } | 109 } |
OLD | NEW |