OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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:isolate"; | 7 import "dart:isolate"; |
8 | 8 |
9 class Server { | 9 class Server { |
10 Server() { | 10 static Future<int> start() { |
11 HttpServer server = new HttpServer(); | 11 return HttpServer.bind("127.0.0.1", 0).then((server) { |
12 server.listen("127.0.0.1", 0); | 12 server.listen((HttpRequest request) { |
13 port = server.port; | 13 Timer.run(server.close); |
14 server.defaultRequestHandler = | 14 }, onError: (e) { |
15 (HttpRequest request, HttpResponse response) { | 15 Expect.fail("No server errors expected: $e"); |
16 Timer.run(server.close); | 16 }); |
17 }; | 17 return server.port; |
18 server.onError = (e) { | 18 }); |
19 Expect.fail("No server errors expected: $e"); | |
20 }; | |
21 } | 19 } |
22 int port; | |
23 } | 20 } |
24 | 21 |
25 class Client { | 22 class Client { |
26 Client(int port) { | 23 Client(int port) { |
27 ReceivePort r = new ReceivePort(); | 24 ReceivePort r = new ReceivePort(); |
28 HttpClient client = new HttpClient(); | 25 HttpClient client = new HttpClient(); |
29 HttpClientConnection c = client.get("127.0.0.1", port, "/"); | 26 client.get("127.0.0.1", port, "/") |
30 c.onRequest = (HttpClientRequest request) { | 27 .then((HttpClientRequest request) { |
31 request.outputStream.close(); | 28 return request.close(); |
32 }; | 29 }) |
33 c.onResponse = (HttpClientResponse response) { | 30 .then((HttpClientResponse response) { |
34 Expect.fail("Response should not be given, as not data was returned."); | 31 Expect.fail( |
35 }; | 32 "Response should not be given, as not data was returned."); |
36 c.onError = (e) { | 33 }) |
37 r.close(); | 34 .catchError((e) { |
38 }; | 35 r.close(); |
| 36 }); |
39 } | 37 } |
40 } | 38 } |
41 | 39 |
42 main() { | 40 main() { |
43 Server server = new Server(); | 41 Server.start().then((port) { |
44 new Client(server.port); | 42 new Client(port); |
| 43 }); |
45 } | 44 } |
OLD | NEW |