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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 response.inputStream.onClosed = () { | 95 response.inputStream.onClosed = () { |
96 if (++count == totalConnections) { | 96 if (++count == totalConnections) { |
97 client.shutdown(); | 97 client.shutdown(); |
98 server.close(); | 98 server.close(); |
99 } | 99 } |
100 }; | 100 }; |
101 }; | 101 }; |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
| 105 void testBodyChunked(int totalConnections, bool useHeader) { |
| 106 HttpServer server = new HttpServer(); |
| 107 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| 108 server.listen("127.0.0.1", 0, backlog: totalConnections); |
| 109 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
| 110 Expect.isNull(request.headers.value('content-length')); |
| 111 Expect.equals(-1, request.contentLength); |
| 112 if (useHeader) { |
| 113 response.contentLength = 2; |
| 114 response.headers.chunkedTransferEncoding = true; |
| 115 } else { |
| 116 response.headers.set("content-length", 2); |
| 117 response.headers.set("transfer-encoding", "chunked"); |
| 118 } |
| 119 OutputStream stream = response.outputStream; |
| 120 stream.writeString("x"); |
| 121 Expect.throws(() => response.headers.chunkedTransferEncoding = false, |
| 122 (e) => e is HttpException); |
| 123 stream.writeString("x"); |
| 124 stream.writeString("x"); |
| 125 stream.close(); |
| 126 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 127 }; |
| 128 |
| 129 int count = 0; |
| 130 HttpClient client = new HttpClient(); |
| 131 for (int i = 0; i < totalConnections; i++) { |
| 132 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 133 conn.onError = (e) => Expect.fail("Unexpected error $e"); |
| 134 conn.onRequest = (HttpClientRequest request) { |
| 135 if (useHeader) { |
| 136 request.contentLength = 2; |
| 137 request.headers.chunkedTransferEncoding = true; |
| 138 } else { |
| 139 request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
| 140 request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
| 141 } |
| 142 OutputStream stream = request.outputStream; |
| 143 stream.writeString("x"); |
| 144 Expect.throws(() => request.headers.chunkedTransferEncoding = false, |
| 145 (e) => e is HttpException); |
| 146 stream.writeString("x"); |
| 147 stream.writeString("x"); |
| 148 stream.close(); |
| 149 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 150 }; |
| 151 conn.onResponse = (HttpClientResponse response) { |
| 152 Expect.isNull(response.headers.value('content-length')); |
| 153 Expect.equals(-1, response.contentLength); |
| 154 response.inputStream.onData = response.inputStream.read; |
| 155 response.inputStream.onClosed = () { |
| 156 if (++count == totalConnections) { |
| 157 client.shutdown(); |
| 158 server.close(); |
| 159 } |
| 160 }; |
| 161 }; |
| 162 } |
| 163 } |
| 164 |
105 void testHttp10() { | 165 void testHttp10() { |
106 HttpServer server = new HttpServer(); | 166 HttpServer server = new HttpServer(); |
107 server.onError = (e) => Expect.fail("Unexpected error $e"); | 167 server.onError = (e) => Expect.fail("Unexpected error $e"); |
108 server.listen("127.0.0.1", 0, backlog: 5); | 168 server.listen("127.0.0.1", 0, backlog: 5); |
109 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 169 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
110 Expect.isNull(request.headers.value('content-length')); | 170 Expect.isNull(request.headers.value('content-length')); |
111 Expect.equals(-1, request.contentLength); | 171 Expect.equals(-1, request.contentLength); |
112 response.contentLength = 0; | 172 response.contentLength = 0; |
113 OutputStream stream = response.outputStream; | 173 OutputStream stream = response.outputStream; |
114 Expect.equals("1.0", request.protocolVersion); | 174 Expect.equals("1.0", request.protocolVersion); |
(...skipping 11 matching lines...) Expand all Loading... |
126 server.close(); | 186 server.close(); |
127 }; | 187 }; |
128 }; | 188 }; |
129 } | 189 } |
130 | 190 |
131 void main() { | 191 void main() { |
132 testNoBody(5, false); | 192 testNoBody(5, false); |
133 testNoBody(5, true); | 193 testNoBody(5, true); |
134 testBody(5, false); | 194 testBody(5, false); |
135 testBody(5, true); | 195 testBody(5, true); |
| 196 testBodyChunked(5, false); |
| 197 testBodyChunked(5, true); |
136 testHttp10(); | 198 testHttp10(); |
137 } | 199 } |
OLD | NEW |