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 | 5 |
6 import "dart:isolate"; | 6 import "dart:isolate"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 | 8 |
9 void setConnectionHeaders(HttpHeaders headers) { | 9 void setConnectionHeaders(HttpHeaders headers) { |
10 headers.add(HttpHeaders.CONNECTION, "my-connection-header1"); | 10 headers.add(HttpHeaders.CONNECTION, "my-connection-header1"); |
11 headers.add("My-Connection-Header1", "some-value1"); | 11 headers.add("My-Connection-Header1", "some-value1"); |
(...skipping 12 matching lines...) Expand all Loading... |
24 if (persistentConnection) { | 24 if (persistentConnection) { |
25 Expect.equals(2, headers[HttpHeaders.CONNECTION].length); | 25 Expect.equals(2, headers[HttpHeaders.CONNECTION].length); |
26 } else { | 26 } else { |
27 Expect.equals(3, headers[HttpHeaders.CONNECTION].length); | 27 Expect.equals(3, headers[HttpHeaders.CONNECTION].length); |
28 Expect.isTrue(headers[HttpHeaders.CONNECTION].any( | 28 Expect.isTrue(headers[HttpHeaders.CONNECTION].any( |
29 (value) => value.toLowerCase() == "close")); | 29 (value) => value.toLowerCase() == "close")); |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 void test(int totalConnections, bool clientPersistentConnection) { | 33 void test(int totalConnections, bool clientPersistentConnection) { |
34 HttpServer server = new HttpServer(); | 34 HttpServer.bind().then((server) { |
35 server.onError = (e) => Expect.fail("Unexpected error $e"); | 35 server.listen((HttpRequest request) { |
36 server.listen("127.0.0.1", 0, backlog: totalConnections); | 36 // Check expected request. |
37 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 37 Expect.equals(clientPersistentConnection, request.persistentConnection); |
38 // Check expected request. | 38 Expect.equals(clientPersistentConnection, |
39 Expect.equals(clientPersistentConnection, request.persistentConnection); | 39 request.response.persistentConnection); |
40 Expect.equals(clientPersistentConnection, response.persistentConnection); | 40 checkExpectedConnectionHeaders(request.headers, |
41 checkExpectedConnectionHeaders(request.headers, | 41 request.persistentConnection); |
42 request.persistentConnection); | |
43 | 42 |
44 // Generate response. If the client signaled non-persistent | 43 // Generate response. If the client signaled non-persistent |
45 // connection the server should not need to set it. | 44 // connection the server should not need to set it. |
46 if (request.persistentConnection) { | 45 if (request.persistentConnection) { |
47 response.persistentConnection = false; | 46 request.response.persistentConnection = false; |
| 47 } |
| 48 setConnectionHeaders(request.response.headers); |
| 49 request.response.close(); |
| 50 }); |
| 51 |
| 52 int count = 0; |
| 53 HttpClient client = new HttpClient(); |
| 54 for (int i = 0; i < totalConnections; i++) { |
| 55 client.get("127.0.0.1", server.port, "/") |
| 56 .then((HttpClientRequest request) { |
| 57 setConnectionHeaders(request.headers); |
| 58 request.persistentConnection = clientPersistentConnection; |
| 59 return request.close(); |
| 60 }) |
| 61 .then((HttpClientResponse response) { |
| 62 Expect.isFalse(response.persistentConnection); |
| 63 checkExpectedConnectionHeaders(response.headers, |
| 64 response.persistentConnection); |
| 65 response.listen((_) {}, onDone: () { |
| 66 count++; |
| 67 if (count == totalConnections) { |
| 68 client.close(); |
| 69 server.close(); |
| 70 } |
| 71 }); |
| 72 }); |
48 } | 73 } |
49 setConnectionHeaders(response.headers); | 74 }); |
50 response.outputStream.close(); | |
51 }; | |
52 | |
53 int count = 0; | |
54 HttpClient client = new HttpClient(); | |
55 for (int i = 0; i < totalConnections; i++) { | |
56 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
57 conn.onError = (e) => Expect.fail("Unexpected error $e"); | |
58 conn.onRequest = (HttpClientRequest request) { | |
59 setConnectionHeaders(request.headers); | |
60 request.persistentConnection = clientPersistentConnection; | |
61 request.outputStream.close(); | |
62 }; | |
63 conn.onResponse = (HttpClientResponse response) { | |
64 Expect.isFalse(response.persistentConnection); | |
65 checkExpectedConnectionHeaders(response.headers, | |
66 response.persistentConnection); | |
67 response.inputStream.onClosed = () { | |
68 count++; | |
69 if (count == totalConnections) { | |
70 client.shutdown(); | |
71 server.close(); | |
72 } | |
73 }; | |
74 }; | |
75 } | |
76 } | 75 } |
77 | 76 |
78 | 77 |
79 void main() { | 78 void main() { |
80 test(2, false); | 79 test(2, false); |
81 test(2, true); | 80 test(2, true); |
82 } | 81 } |
OLD | NEW |