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