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