| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "dart:isolate"; | 5 import "dart:isolate"; |
| 6 import "dart:io"; | 6 import "dart:io"; |
| 7 | 7 |
| 8 void testNoBody(int totalConnections, bool explicitContentLength) { | 8 void testNoBody(int totalConnections, bool explicitContentLength) { |
| 9 HttpServer server = new HttpServer(); | 9 HttpServer server = new HttpServer(); |
| 10 server.onError = (e) => Expect.fail("Unexpected error $e"); | 10 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 response.inputStream.onClosed = () { | 40 response.inputStream.onClosed = () { |
| 41 if (++count == totalConnections) { | 41 if (++count == totalConnections) { |
| 42 client.shutdown(); | 42 client.shutdown(); |
| 43 server.close(); | 43 server.close(); |
| 44 } | 44 } |
| 45 }; | 45 }; |
| 46 }; | 46 }; |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 void testBody(int totalConnections) { | 50 void testBody(int totalConnections, bool useHeader) { |
| 51 HttpServer server = new HttpServer(); | 51 HttpServer server = new HttpServer(); |
| 52 server.onError = (e) => Expect.fail("Unexpected error $e"); | 52 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| 53 server.listen("127.0.0.1", 0, backlog: totalConnections); | 53 server.listen("127.0.0.1", 0, backlog: totalConnections); |
| 54 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 54 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
| 55 Expect.equals("2", request.headers.value('content-length')); | 55 Expect.equals("2", request.headers.value('content-length')); |
| 56 Expect.equals(2, request.contentLength); | 56 Expect.equals(2, request.contentLength); |
| 57 response.contentLength = 2; | 57 if (useHeader) { |
| 58 response.contentLength = 2; |
| 59 } else { |
| 60 response.headers.set("content-length", 2); |
| 61 } |
| 58 OutputStream stream = response.outputStream; | 62 OutputStream stream = response.outputStream; |
| 59 stream.writeString("x"); | 63 stream.writeString("x"); |
| 60 Expect.throws(() => response.contentLength = 3, (e) => e is HttpException); | 64 Expect.throws(() => response.contentLength = 3, (e) => e is HttpException); |
| 61 stream.writeString("x"); | 65 stream.writeString("x"); |
| 62 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 66 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 63 stream.close(); | 67 stream.close(); |
| 64 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 68 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 65 }; | 69 }; |
| 66 | 70 |
| 67 int count = 0; | 71 int count = 0; |
| 68 HttpClient client = new HttpClient(); | 72 HttpClient client = new HttpClient(); |
| 69 for (int i = 0; i < totalConnections; i++) { | 73 for (int i = 0; i < totalConnections; i++) { |
| 70 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 74 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 71 conn.onError = (e) => Expect.fail("Unexpected error $e"); | 75 conn.onError = (e) => Expect.fail("Unexpected error $e"); |
| 72 conn.onRequest = (HttpClientRequest request) { | 76 conn.onRequest = (HttpClientRequest request) { |
| 73 request.contentLength = 2; | 77 if (useHeader) { |
| 78 request.contentLength = 2; |
| 79 } else { |
| 80 request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); |
| 81 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
| 82 } |
| 74 OutputStream stream = request.outputStream; | 83 OutputStream stream = request.outputStream; |
| 75 stream.writeString("x"); | 84 stream.writeString("x"); |
| 76 Expect.throws(() => request.contentLength = 3, (e) => e is HttpException); | 85 Expect.throws(() => request.contentLength = 3, (e) => e is HttpException); |
| 77 stream.writeString("x"); | 86 stream.writeString("x"); |
| 78 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 87 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 79 stream.close(); | 88 stream.close(); |
| 80 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 89 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 81 }; | 90 }; |
| 82 conn.onResponse = (HttpClientResponse response) { | 91 conn.onResponse = (HttpClientResponse response) { |
| 83 Expect.equals("2", response.headers.value('content-length')); | 92 Expect.equals("2", response.headers.value('content-length')); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 socket.onClosed = () { | 124 socket.onClosed = () { |
| 116 socket.close(true); | 125 socket.close(true); |
| 117 server.close(); | 126 server.close(); |
| 118 }; | 127 }; |
| 119 }; | 128 }; |
| 120 } | 129 } |
| 121 | 130 |
| 122 void main() { | 131 void main() { |
| 123 testNoBody(5, false); | 132 testNoBody(5, false); |
| 124 testNoBody(5, true); | 133 testNoBody(5, true); |
| 125 testBody(5); | 134 testBody(5, false); |
| 135 testBody(5, true); |
| 126 testHttp10(); | 136 testHttp10(); |
| 127 } | 137 } |
| OLD | NEW |