Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 | |
| 6 #import("dart:isolate"); | |
| 7 #import("dart:io"); | |
| 8 | |
| 9 void setConnectionHeaders(HttpHeaders headers) { | |
| 10 headers.add(HttpHeaders.CONNECTION, "my-connection-header1"); | |
| 11 headers.add("My-Connection-Header1", "some-value1"); | |
| 12 headers.add(HttpHeaders.CONNECTION, "my-connection-header2"); | |
| 13 headers.add("My-Connection-Header2", "some-value2"); | |
| 14 } | |
| 15 | |
| 16 void checkExpectedConnectionHeaders(HttpHeaders headers, | |
| 17 bool persistentConnection) { | |
| 18 Expect.equals("some-value1", headers.value("My-Connection-Header1")); | |
| 19 Expect.equals("some-value2", headers.value("My-Connection-Header2")); | |
| 20 if (persistentConnection) { | |
| 21 Expect.equals(2, headers[HttpHeaders.CONNECTION].length); | |
|
Anders Johnsen
2012/05/02 07:37:58
This looks kind of shaky. Maybe change to a listEq
Søren Gjesse
2012/05/02 11:56:05
Added check of the actual values.
| |
| 22 } else { | |
| 23 Expect.equals(3, headers[HttpHeaders.CONNECTION].length); | |
|
Anders Johnsen
2012/05/02 07:37:58
Ditto.
Søren Gjesse
2012/05/02 11:56:05
Ditto.
| |
| 24 } | |
| 25 } | |
| 26 | |
| 27 void test(int totalConnections, bool clientPersistentConnection) { | |
| 28 HttpServer server = new HttpServer(); | |
| 29 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 30 server.listen("127.0.0.1", 0, totalConnections); | |
| 31 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | |
| 32 // Check expected request. | |
| 33 Expect.equals(clientPersistentConnection, request.persistentConnection); | |
| 34 Expect.equals(clientPersistentConnection, response.persistentConnection); | |
| 35 checkExpectedConnectionHeaders(request.headers, | |
| 36 request.persistentConnection); | |
| 37 | |
| 38 // Generate response. If the client signaled non-persistent | |
| 39 // connection the server should not need to set it. | |
| 40 if (request.persistentConnection) { | |
| 41 response.persistentConnection = false; | |
| 42 } | |
| 43 setConnectionHeaders(response.headers); | |
| 44 response.outputStream.close(); | |
| 45 }; | |
| 46 | |
| 47 int count = 0; | |
| 48 HttpClient client = new HttpClient(); | |
| 49 for (int i = 0; i < totalConnections; i++) { | |
| 50 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
| 51 conn.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 52 conn.onRequest = (HttpClientRequest request) { | |
| 53 setConnectionHeaders(request.headers); | |
| 54 request.persistentConnection = clientPersistentConnection; | |
| 55 request.outputStream.close(); | |
| 56 }; | |
| 57 conn.onResponse = (HttpClientResponse response) { | |
| 58 Expect.isFalse(response.persistentConnection); | |
| 59 checkExpectedConnectionHeaders(response.headers, | |
| 60 response.persistentConnection); | |
| 61 count++; | |
| 62 if (count == totalConnections) { | |
| 63 client.shutdown(); | |
| 64 server.close(); | |
| 65 } | |
| 66 }; | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 | |
| 71 void main() { | |
| 72 test(2, false); | |
| 73 test(2, true); | |
| 74 } | |
| OLD | NEW |