OLD | NEW |
1 // Copyright (c) 2012, 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:isolate"; | 6 import "dart:isolate"; |
7 | 7 |
8 void testListenOn() { | 8 void testListenOn() { |
9 ServerSocket socket = new ServerSocket("127.0.0.1", 0, 5); | 9 ServerSocket socket; |
10 | 10 HttpServer server; |
11 socket.onError = (Exception e) { | |
12 Expect.fail("ServerSocket closed unexpected"); | |
13 }; | |
14 | 11 |
15 void test(void onDone()) { | 12 void test(void onDone()) { |
16 HttpServer server = new HttpServer(); | |
17 Expect.throws(() => server.port); | |
18 | 13 |
19 ReceivePort serverPort = new ReceivePort(); | |
20 server.defaultRequestHandler = | |
21 (HttpRequest request, HttpResponse response) { | |
22 request.inputStream.onClosed = () { | |
23 response.outputStream.close(); | |
24 serverPort.close(); | |
25 }; | |
26 }; | |
27 | |
28 server.onError = (Exception e) { | |
29 Expect.fail("Unexpected error in Http Server: $e"); | |
30 }; | |
31 | |
32 server.listenOn(socket); | |
33 Expect.equals(socket.port, server.port); | 14 Expect.equals(socket.port, server.port); |
34 | 15 |
| 16 ReceivePort clientPort = new ReceivePort(); |
35 HttpClient client = new HttpClient(); | 17 HttpClient client = new HttpClient(); |
36 HttpClientConnection conn = client.get("127.0.0.1", socket.port, "/"); | 18 client.get("127.0.0.1", socket.port, "/") |
37 conn.onRequest = (HttpClientRequest request) { | 19 .then((request) { |
38 request.outputStream.close(); | 20 return request.close(); |
39 }; | 21 }) |
40 ReceivePort clientPort = new ReceivePort(); | 22 .then((response) { |
41 conn.onResponse = (HttpClientResponse response) { | 23 response.listen( |
42 response.inputStream.onClosed = () { | 24 (_) {}, |
43 client.shutdown(); | 25 onDone: () { |
44 clientPort.close(); | 26 client.close(); |
| 27 clientPort.close(); |
| 28 onDone(); |
| 29 }); |
| 30 }) |
| 31 .catchError((error) { |
| 32 Expect.fail("Unexpected error in Http Client: $error"); |
| 33 }); |
| 34 } |
| 35 |
| 36 // Test two connection after each other. |
| 37 ServerSocket.bind().then((s) { |
| 38 socket = s; |
| 39 server = new HttpServer.listenOn(socket); |
| 40 ReceivePort serverPort = new ReceivePort(); |
| 41 server.listen((HttpRequest request) { |
| 42 request.listen( |
| 43 (_) {}, |
| 44 onDone: () { |
| 45 request.response.close(); |
| 46 serverPort.close(); |
| 47 }); |
| 48 }); |
| 49 |
| 50 test(() { |
| 51 test(() { |
45 server.close(); | 52 server.close(); |
46 Expect.throws(() => server.port); | 53 Expect.throws(() => server.port); |
47 onDone(); | 54 socket.close(); |
48 }; | 55 }); |
49 }; | |
50 conn.onError = (Exception e) { | |
51 Expect.fail("Unexpected error in Http Client: $e"); | |
52 }; | |
53 }; | |
54 | |
55 // Test two connection after each other. | |
56 test(() { | |
57 test(() { | |
58 socket.close(); | |
59 }); | 56 }); |
60 }); | 57 }); |
61 } | 58 } |
62 | 59 |
63 void main() { | 60 void main() { |
64 testListenOn(); | 61 testListenOn(); |
65 } | 62 } |
OLD | NEW |