OLD | NEW |
1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "dart:isolate"; | 7 import "dart:isolate"; |
8 import "dart:io"; | 8 import "dart:io"; |
9 | 9 |
10 void setConnectionHeaders(HttpHeaders headers) { | 10 void setConnectionHeaders(HttpHeaders headers) { |
11 headers.add(HttpHeaders.CONNECTION, "my-connection-header1"); | 11 headers.add(HttpHeaders.CONNECTION, "my-connection-header1"); |
12 headers.add("My-Connection-Header1", "some-value1"); | 12 headers.add("My-Connection-Header1", "some-value1"); |
13 headers.add(HttpHeaders.CONNECTION, "my-connection-header2"); | 13 headers.add(HttpHeaders.CONNECTION, "my-connection-header2"); |
14 headers.add("My-Connection-Header2", "some-value2"); | 14 headers.add("My-Connection-Header2", "some-value2"); |
15 } | 15 } |
16 | 16 |
17 void checkExpectedConnectionHeaders(HttpHeaders headers, | 17 void checkExpectedConnectionHeaders( |
18 bool persistentConnection) { | 18 HttpHeaders headers, bool persistentConnection) { |
19 Expect.equals("some-value1", headers.value("My-Connection-Header1")); | 19 Expect.equals("some-value1", headers.value("My-Connection-Header1")); |
20 Expect.equals("some-value2", headers.value("My-Connection-Header2")); | 20 Expect.equals("some-value2", headers.value("My-Connection-Header2")); |
21 Expect.isTrue(headers[HttpHeaders.CONNECTION].any( | 21 Expect.isTrue(headers[HttpHeaders.CONNECTION] |
22 (value) => value.toLowerCase() == "my-connection-header1")); | 22 .any((value) => value.toLowerCase() == "my-connection-header1")); |
23 Expect.isTrue(headers[HttpHeaders.CONNECTION].any( | 23 Expect.isTrue(headers[HttpHeaders.CONNECTION] |
24 (value) => value.toLowerCase() == "my-connection-header2")); | 24 .any((value) => value.toLowerCase() == "my-connection-header2")); |
25 if (persistentConnection) { | 25 if (persistentConnection) { |
26 Expect.equals(2, headers[HttpHeaders.CONNECTION].length); | 26 Expect.equals(2, headers[HttpHeaders.CONNECTION].length); |
27 } else { | 27 } else { |
28 Expect.equals(3, headers[HttpHeaders.CONNECTION].length); | 28 Expect.equals(3, headers[HttpHeaders.CONNECTION].length); |
29 Expect.isTrue(headers[HttpHeaders.CONNECTION].any( | 29 Expect.isTrue(headers[HttpHeaders.CONNECTION] |
30 (value) => value.toLowerCase() == "close")); | 30 .any((value) => value.toLowerCase() == "close")); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 void test(int totalConnections, bool clientPersistentConnection) { | 34 void test(int totalConnections, bool clientPersistentConnection) { |
35 HttpServer.bind("127.0.0.1", 0).then((server) { | 35 HttpServer.bind("127.0.0.1", 0).then((server) { |
36 server.listen((HttpRequest request) { | 36 server.listen((HttpRequest request) { |
37 // Check expected request. | 37 // Check expected request. |
38 Expect.equals(clientPersistentConnection, request.persistentConnection); | 38 Expect.equals(clientPersistentConnection, request.persistentConnection); |
39 Expect.equals(clientPersistentConnection, | 39 Expect.equals( |
40 request.response.persistentConnection); | 40 clientPersistentConnection, request.response.persistentConnection); |
41 checkExpectedConnectionHeaders(request.headers, | 41 checkExpectedConnectionHeaders( |
42 request.persistentConnection); | 42 request.headers, request.persistentConnection); |
43 | 43 |
44 // Generate response. If the client signaled non-persistent | 44 // Generate response. If the client signaled non-persistent |
45 // connection the server should not need to set it. | 45 // connection the server should not need to set it. |
46 if (request.persistentConnection) { | 46 if (request.persistentConnection) { |
47 request.response.persistentConnection = false; | 47 request.response.persistentConnection = false; |
48 } | 48 } |
49 setConnectionHeaders(request.response.headers); | 49 setConnectionHeaders(request.response.headers); |
50 request.response.close(); | 50 request.response.close(); |
51 }); | 51 }); |
52 | 52 |
53 int count = 0; | 53 int count = 0; |
54 HttpClient client = new HttpClient(); | 54 HttpClient client = new HttpClient(); |
55 for (int i = 0; i < totalConnections; i++) { | 55 for (int i = 0; i < totalConnections; i++) { |
56 client.get("127.0.0.1", server.port, "/") | 56 client |
57 .then((HttpClientRequest request) { | 57 .get("127.0.0.1", server.port, "/") |
58 setConnectionHeaders(request.headers); | 58 .then((HttpClientRequest request) { |
59 request.persistentConnection = clientPersistentConnection; | 59 setConnectionHeaders(request.headers); |
60 return request.close(); | 60 request.persistentConnection = clientPersistentConnection; |
61 }) | 61 return request.close(); |
62 .then((HttpClientResponse response) { | 62 }).then((HttpClientResponse response) { |
63 Expect.isFalse(response.persistentConnection); | 63 Expect.isFalse(response.persistentConnection); |
64 checkExpectedConnectionHeaders(response.headers, | 64 checkExpectedConnectionHeaders( |
65 response.persistentConnection); | 65 response.headers, response.persistentConnection); |
66 response.listen((_) {}, onDone: () { | 66 response.listen((_) {}, onDone: () { |
67 count++; | 67 count++; |
68 if (count == totalConnections) { | 68 if (count == totalConnections) { |
69 client.close(); | 69 client.close(); |
70 server.close(); | 70 server.close(); |
71 } | 71 } |
72 }); | |
73 }); | 72 }); |
| 73 }); |
74 } | 74 } |
75 }); | 75 }); |
76 } | 76 } |
77 | 77 |
78 | |
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 |