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 test(int totalConnections, [String body]) { | |
10 HttpServer server = new HttpServer(); | |
11 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
12 server.listen("127.0.0.1", 0, totalConnections); | |
13 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | |
14 // Cannot mutate request headers. | |
15 Expect.throws(() => request.headers.add("X-Request-Header", "value"), | |
16 (e) => e is HttpException); | |
17 Expect.equals("value", request.headers.value("X-Request-Header")); | |
18 OutputStream stream = response.outputStream; | |
19 // Can still mutate response headers as long as no data has been sent. | |
20 response.headers.add("X-Response-Header", "value"); | |
21 if (body != null) { | |
22 stream.writeString(body); | |
23 // Cannot mutate response headers when data has been sent. | |
24 Expect.throws(() => request.headers.add("X-Request-Header", "value2"), | |
25 (e) => e is HttpException); | |
26 } | |
27 stream.close(); | |
28 // Cannot mutate response headers when data has been sent. | |
29 Expect.throws(() => request.headers.add("X-Request-Header", "value3"), | |
30 (e) => e is HttpException); | |
31 }; | |
32 | |
33 int count = 0; | |
34 HttpClient client = new HttpClient(); | |
35 for (int i = 0; i < totalConnections; i++) { | |
36 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
37 conn.onError = (e) => Expect.fail("Unexpected error $e"); | |
38 conn.onRequest = (HttpClientRequest request) { | |
39 if (body != null) { | |
40 request.contentLength = -1; | |
41 } | |
42 OutputStream stream = request.outputStream; | |
43 // Can still mutate request headers as long as no data has been sent. | |
44 request.headers.add("X-Request-Header", "value"); | |
45 if (body != null) { | |
46 stream.writeString(body); | |
47 // Cannot mutate request headers when data has been sent. | |
48 Expect.throws(() => request.headers.add("X-Request-Header", "value2"), | |
49 (e) => e is HttpException); | |
50 } | |
51 stream.close(); | |
52 // Cannot mutate request headers when data has been sent. | |
53 Expect.throws(() => request.headers.add("X-Request-Header", "value3"), | |
54 (e) => e is HttpException); | |
55 }; | |
56 conn.onResponse = (HttpClientResponse response) { | |
57 // Cannot mutate response headers. | |
58 Expect.throws(() => response.headers.add("X-Response-Header", "value"), | |
59 (e) => e is HttpException); | |
60 Expect.equals("value", response.headers.value("X-Response-Header")); | |
61 count++; | |
62 if (count == totalConnections) { | |
63 client.shutdown(); | |
64 server.close(); | |
65 } | |
66 }; | |
67 } | |
68 } | |
69 | |
70 void main() { | |
71 test(5); | |
72 test(5, "Hello and goodbye"); | |
73 } | |
OLD | NEW |