Index: tests/standalone/io/http_content_length_test.dart |
diff --git a/tests/standalone/io/http_content_length_test.dart b/tests/standalone/io/http_content_length_test.dart |
index 62902181737a62d972e0c5dedb429937e8c00d46..14d4000fd170a61e24464303ab8826f85f1ac3e9 100644 |
--- a/tests/standalone/io/http_content_length_test.dart |
+++ b/tests/standalone/io/http_content_length_test.dart |
@@ -6,192 +6,229 @@ import "dart:isolate"; |
import "dart:io"; |
void testNoBody(int totalConnections, bool explicitContentLength) { |
- HttpServer server = new HttpServer(); |
- server.onError = (e) => Expect.fail("Unexpected error $e"); |
- server.listen("127.0.0.1", 0, backlog: totalConnections); |
- server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
- Expect.equals("0", request.headers.value('content-length')); |
- Expect.equals(0, request.contentLength); |
- response.contentLength = 0; |
- OutputStream stream = response.outputStream; |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
+ var errors = 0; |
+ HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { |
+ server.listen( |
+ (HttpRequest request) { |
+ Expect.equals("0", request.headers.value('content-length')); |
+ Expect.equals(0, request.contentLength); |
+ var response = request.response; |
+ response.contentLength = 0; |
+ response.done |
+ .then((_) { |
+ Expect.fail("Unexpected successful response completion"); |
+ }) |
+ .catchError((e) { |
+ Expect.isTrue(e.error is HttpException); |
+ }); |
+ // addString with content length 0 closes the connection and |
+ // reports an error. |
+ response.addString("x"); |
+ // Subsequent addString are ignored as there is already an |
+ // error. |
+ response.addString("x"); |
+ // After an explicit close, addString becomes a state error |
+ // because we have said we will not add more. |
+ response.close(); |
+ Expect.throws(() => response.addString("x"), |
+ (e) => e is StateError); |
+ }, |
+ onError: (e) { |
+ Expect.fail("Unexpected server error $e"); |
+ }); |
- int count = 0; |
- HttpClient client = new HttpClient(); |
- for (int i = 0; i < totalConnections; i++) { |
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
- conn.onError = (e) => Expect.fail("Unexpected error $e"); |
- conn.onRequest = (HttpClientRequest request) { |
- OutputStream stream = request.outputStream; |
- if (explicitContentLength) { |
- request.contentLength = 0; |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- } |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
- conn.onResponse = (HttpClientResponse response) { |
- Expect.equals("0", response.headers.value('content-length')); |
- Expect.equals(0, response.contentLength); |
- response.inputStream.onData = response.inputStream.read; |
- response.inputStream.onClosed = () { |
- if (++count == totalConnections) { |
- client.shutdown(); |
- server.close(); |
- } |
- }; |
- }; |
- } |
+ int count = 0; |
+ HttpClient client = new HttpClient(); |
+ for (int i = 0; i < totalConnections; i++) { |
+ client.get("127.0.0.1", server.port, "/") |
+ .then((request) { |
+ if (explicitContentLength) { |
+ request.contentLength = 0; |
+ } |
+ return request.close(); |
+ }) |
+ .then((response) { |
+ Expect.equals("0", response.headers.value('content-length')); |
+ Expect.equals(0, response.contentLength); |
+ response.listen( |
+ (d) {}, |
+ onDone: () { |
+ if (++count == totalConnections) { |
+ client.close(); |
+ server.close(); |
+ } |
+ }); |
+ }) |
+ .catchError((e) { |
+ Expect.fail("Unexpected error $e"); |
+ }); |
+ } |
+ }); |
} |
void testBody(int totalConnections, bool useHeader) { |
- HttpServer server = new HttpServer(); |
- server.onError = (e) => Expect.fail("Unexpected error $e"); |
- server.listen("127.0.0.1", 0, backlog: totalConnections); |
- server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
- Expect.equals("2", request.headers.value('content-length')); |
- Expect.equals(2, request.contentLength); |
- if (useHeader) { |
- response.contentLength = 2; |
- } else { |
- response.headers.set("content-length", 2); |
- } |
- request.inputStream.onData = request.inputStream.read; |
- request.inputStream.onClosed = () { |
- OutputStream stream = response.outputStream; |
- stream.writeString("x"); |
- Expect.throws(() => response.contentLength = 3, (e) => e is HttpException); |
- stream.writeString("x"); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
- }; |
+ HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { |
+ server.listen( |
+ (HttpRequest request) { |
+ Expect.equals("2", request.headers.value('content-length')); |
+ Expect.equals(2, request.contentLength); |
+ var response = request.response; |
+ if (useHeader) { |
+ response.contentLength = 2; |
+ } else { |
+ response.headers.set("content-length", 2); |
+ } |
+ request.listen( |
+ (d) {}, |
+ onDone: () { |
+ response.addString("x"); |
+ Expect.throws(() => response.contentLength = 3, |
+ (e) => e is HttpException); |
+ response.addString("x"); |
+ response.addString("x"); |
+ response.done |
+ .then((_) { |
+ Expect.fail("Unexpected successful response completion"); |
+ }) |
+ .catchError((e) { |
+ Expect.isTrue(e.error is HttpException); |
+ }); |
+ response.close(); |
+ Expect.throws(() => response.addString("x"), |
+ (e) => e is StateError); |
+ }); |
+ }, |
+ onError: (e) => Expect.fail("Unexpected error $e")); |
- int count = 0; |
- HttpClient client = new HttpClient(); |
- for (int i = 0; i < totalConnections; i++) { |
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
- conn.onError = (e) => Expect.fail("Unexpected error $e"); |
- conn.onRequest = (HttpClientRequest request) { |
- if (useHeader) { |
- request.contentLength = 2; |
- } else { |
- request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); |
- request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
- } |
- OutputStream stream = request.outputStream; |
- stream.writeString("x"); |
- Expect.throws(() => request.contentLength = 3, (e) => e is HttpException); |
- stream.writeString("x"); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
- conn.onResponse = (HttpClientResponse response) { |
- Expect.equals("2", response.headers.value('content-length')); |
- Expect.equals(2, response.contentLength); |
- response.inputStream.onData = response.inputStream.read; |
- response.inputStream.onClosed = () { |
- if (++count == totalConnections) { |
- client.shutdown(); |
- server.close(); |
- } |
- }; |
- }; |
- } |
+ int count = 0; |
+ HttpClient client = new HttpClient(); |
+ for (int i = 0; i < totalConnections; i++) { |
+ client.get("127.0.0.1", server.port, "/") |
+ .then((request) { |
+ if (useHeader) { |
+ request.contentLength = 2; |
+ } else { |
+ request.headers.add(HttpHeaders.CONTENT_LENGTH, "7"); |
+ request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
+ } |
+ request.addString("x"); |
+ Expect.throws(() => request.contentLength = 3, |
+ (e) => e is HttpException); |
+ request.addString("x"); |
+ return request.close(); |
+ }) |
+ .then((response) { |
+ Expect.equals("2", response.headers.value('content-length')); |
+ Expect.equals(2, response.contentLength); |
+ response.listen( |
+ (d) {}, |
+ onDone: () { |
+ if (++count == totalConnections) { |
+ client.close(); |
+ server.close(); |
+ } |
+ }); |
+ }); |
+ } |
+ }); |
} |
void testBodyChunked(int totalConnections, bool useHeader) { |
- HttpServer server = new HttpServer(); |
- server.onError = (e) => Expect.fail("Unexpected error $e"); |
- server.listen("127.0.0.1", 0, backlog: totalConnections); |
- server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
- Expect.isNull(request.headers.value('content-length')); |
- Expect.equals(-1, request.contentLength); |
- if (useHeader) { |
- response.contentLength = 2; |
- response.headers.chunkedTransferEncoding = true; |
- } else { |
- response.headers.set("content-length", 2); |
- response.headers.set("transfer-encoding", "chunked"); |
- } |
- request.inputStream.onData = request.inputStream.read; |
- request.inputStream.onClosed = () { |
- OutputStream stream = response.outputStream; |
- stream.writeString("x"); |
- Expect.throws(() => response.headers.chunkedTransferEncoding = false, |
+ HttpServer.bind("127.0.0.1", 0, totalConnections).then((server) { |
+ server.listen( |
+ (HttpRequest request) { |
+ Expect.isNull(request.headers.value('content-length')); |
+ Expect.equals(-1, request.contentLength); |
+ var response = request.response; |
+ if (useHeader) { |
+ response.contentLength = 2; |
+ response.headers.chunkedTransferEncoding = true; |
+ } else { |
+ response.headers.set("content-length", 2); |
+ response.headers.set("transfer-encoding", "chunked"); |
+ } |
+ request.listen( |
+ (d) {}, |
+ onDone: () { |
+ response.addString("x"); |
+ Expect.throws( |
+ () => response.headers.chunkedTransferEncoding = false, |
(e) => e is HttpException); |
- stream.writeString("x"); |
- stream.writeString("x"); |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
- }; |
+ response.addString("x"); |
+ response.addString("x"); |
+ response.close(); |
+ Expect.throws(() => response.addString("x"), |
+ (e) => e is StateError); |
+ }); |
+ }, |
+ onError: (e) => Expect.fail("Unexpected error $e")); |
- int count = 0; |
- HttpClient client = new HttpClient(); |
- for (int i = 0; i < totalConnections; i++) { |
- HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
- conn.onError = (e) => Expect.fail("Unexpected error $e"); |
- conn.onRequest = (HttpClientRequest request) { |
- if (useHeader) { |
- request.contentLength = 2; |
- request.headers.chunkedTransferEncoding = true; |
- } else { |
- request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
- request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
- } |
- OutputStream stream = request.outputStream; |
- stream.writeString("x"); |
- Expect.throws(() => request.headers.chunkedTransferEncoding = false, |
- (e) => e is HttpException); |
- stream.writeString("x"); |
- stream.writeString("x"); |
- stream.close(); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- }; |
- conn.onResponse = (HttpClientResponse response) { |
- Expect.isNull(response.headers.value('content-length')); |
- Expect.equals(-1, response.contentLength); |
- response.inputStream.onData = response.inputStream.read; |
- response.inputStream.onClosed = () { |
- if (++count == totalConnections) { |
- client.shutdown(); |
- server.close(); |
- } |
- }; |
- }; |
- } |
+ int count = 0; |
+ HttpClient client = new HttpClient(); |
+ for (int i = 0; i < totalConnections; i++) { |
+ client.get("127.0.0.1", server.port, "/") |
+ .then((request) { |
+ if (useHeader) { |
+ request.contentLength = 2; |
+ request.headers.chunkedTransferEncoding = true; |
+ } else { |
+ request.headers.add(HttpHeaders.CONTENT_LENGTH, "2"); |
+ request.headers.set(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
+ } |
+ request.addString("x"); |
+ Expect.throws(() => request.headers.chunkedTransferEncoding = false, |
+ (e) => e is HttpException); |
+ request.addString("x"); |
+ request.addString("x"); |
+ return request.close(); |
+ }) |
+ .then((response) { |
+ Expect.isNull(response.headers.value('content-length')); |
+ Expect.equals(-1, response.contentLength); |
+ response.listen( |
+ (d) {}, |
+ onDone: () { |
+ if (++count == totalConnections) { |
+ client.close(); |
+ server.close(); |
+ } |
+ }); |
+ }) |
+ .catchError((e) => Expect.fail("Unexpected error $e")); |
+ } |
+ }); |
} |
void testHttp10() { |
- HttpServer server = new HttpServer(); |
- server.onError = (e) => Expect.fail("Unexpected error $e"); |
- server.listen("127.0.0.1", 0, backlog: 5); |
- server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
- Expect.isNull(request.headers.value('content-length')); |
- Expect.equals(-1, request.contentLength); |
- response.contentLength = 0; |
- OutputStream stream = response.outputStream; |
- Expect.equals("1.0", request.protocolVersion); |
- Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
- stream.close(); |
- }; |
+ HttpServer.bind("127.0.0.1", 0, 5).then((server) { |
+ server.listen( |
+ (HttpRequest request) { |
+ Expect.isNull(request.headers.value('content-length')); |
+ Expect.equals(-1, request.contentLength); |
+ var response = request.response; |
+ response.contentLength = 0; |
+ Expect.equals("1.0", request.protocolVersion); |
+ response.done |
+ .then((_) => Expect.fail("Unexpected response completion")) |
+ .catchError((e) => Expect.isTrue(e.error is HttpException)); |
+ response.addString("x"); |
+ response.close(); |
+ Expect.throws(() => response.addString("x"), |
+ (e) => e is StateError); |
+ }, |
+ onError: (e) => Expect.fail("Unexpected error $e")); |
- Socket socket = new Socket("127.0.0.1", server.port); |
- socket.onConnect = () { |
- List<int> buffer = new List<int>.fixedLength(1024); |
- socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); |
- socket.onData = () => socket.readList(buffer, 0, buffer.length); |
- socket.onClosed = () { |
- socket.close(true); |
- server.close(); |
- }; |
- }; |
+ Socket.connect("127.0.0.1", server.port).then((socket) { |
+ socket.addString("GET / HTTP/1.0\r\n\r\n"); |
+ socket.close(); |
+ socket.listen( |
+ (d) { }, |
+ onDone: () { |
+ socket.destroy(); |
+ server.close(); |
+ }); |
+ }); |
+ }); |
} |
void main() { |