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 | 6 |
7 void testHttpConnectionInfo() { | 7 void testHttpConnectionInfo() { |
8 HttpServer server = new HttpServer(); | 8 HttpServer.bind("0.0.0.0", 0).then((server) { |
9 server.listen("0.0.0.0", 0); | 9 int clientPort; |
10 int clientPort; | |
11 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | |
12 Expect.isTrue(request.connectionInfo.remoteHost is String); | |
13 Expect.equals(request.connectionInfo.localPort, server.port); | |
14 Expect.isNotNull(clientPort); | |
15 Expect.equals(request.connectionInfo.remotePort, clientPort); | |
16 request.inputStream.onClosed = () { | |
17 response.outputStream.close(); | |
18 }; | |
19 }; | |
20 server.onError = (Exception e) { | |
21 Expect.fail("Unexpected error: $e"); | |
22 }; | |
23 | 10 |
| 11 server.listen((request) { |
| 12 var response = request.response; |
| 13 Expect.isTrue(request.connectionInfo.remoteHost is String); |
| 14 Expect.isTrue(response.connectionInfo.remoteHost is String); |
| 15 Expect.equals(request.connectionInfo.localPort, server.port); |
| 16 Expect.equals(response.connectionInfo.localPort, server.port); |
| 17 Expect.isNotNull(clientPort); |
| 18 Expect.equals(request.connectionInfo.remotePort, clientPort); |
| 19 Expect.equals(response.connectionInfo.remotePort, clientPort); |
| 20 request.listen( |
| 21 (_) { }, |
| 22 onDone: () { request.response.close(); }); |
| 23 }); |
24 | 24 |
25 HttpClient client = new HttpClient(); | 25 HttpClient client = new HttpClient(); |
26 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 26 client.get("127.0.0.1", server.port, "/") |
27 conn.onRequest = (HttpClientRequest request) { | 27 .then((request) { |
28 Expect.isTrue(conn.connectionInfo.remoteHost is String); | 28 Expect.isTrue(request.connectionInfo.remoteHost is String); |
29 Expect.equals(conn.connectionInfo.remotePort, server.port); | 29 Expect.equals(request.connectionInfo.remotePort, server.port); |
30 clientPort = conn.connectionInfo.localPort; | 30 clientPort = request.connectionInfo.localPort; |
31 request.outputStream.close(); | 31 return request.close(); |
32 }; | 32 }) |
33 conn.onResponse = (HttpClientResponse response) { | 33 .then((response) { |
34 response.inputStream.onClosed = () { | 34 Expect.equals(server.port, response.connectionInfo.remotePort); |
35 client.shutdown(); | 35 Expect.equals(clientPort, response.connectionInfo.localPort); |
36 server.close(); | 36 response.listen( |
37 }; | 37 (_) { }, |
38 }; | 38 onDone: () { |
39 conn.onError = (Exception e) { | 39 client.close(); |
40 Expect.fail("Unexpected error: $e"); | 40 server.close(); |
41 }; | 41 }); |
| 42 }); |
| 43 }); |
42 } | 44 } |
43 | 45 |
44 void main() { | 46 void main() { |
45 testHttpConnectionInfo(); | 47 testHttpConnectionInfo(); |
46 } | 48 } |
OLD | NEW |