| 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 | 5 |
| 6 #import("dart:isolate"); | 6 #import("dart:isolate"); |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 | 8 |
| 9 void testNoBody(int totalConnections) { | 9 void testNoBody(int totalConnections, bool explicitContentLength) { |
| 10 HttpServer server = new HttpServer(); | 10 HttpServer server = new HttpServer(); |
| 11 server.onError = (e) => Expect.fail("Unexpected error $e"); | 11 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| 12 server.listen("127.0.0.1", 0, backlog: totalConnections); | 12 server.listen("127.0.0.1", 0, backlog: totalConnections); |
| 13 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 13 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
| 14 Expect.isNull(request.headers.value('content-length')); |
| 15 Expect.equals(0, request.contentLength); |
| 14 response.contentLength = 0; | 16 response.contentLength = 0; |
| 15 OutputStream stream = response.outputStream; | 17 OutputStream stream = response.outputStream; |
| 16 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 18 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 17 stream.close(); | 19 stream.close(); |
| 18 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 20 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 19 }; | 21 }; |
| 20 | 22 |
| 21 int count = 0; | 23 int count = 0; |
| 22 HttpClient client = new HttpClient(); | 24 HttpClient client = new HttpClient(); |
| 23 for (int i = 0; i < totalConnections; i++) { | 25 for (int i = 0; i < totalConnections; i++) { |
| 24 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 26 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 25 conn.onError = (e) => Expect.fail("Unexpected error $e"); | 27 conn.onError = (e) => Expect.fail("Unexpected error $e"); |
| 26 conn.onRequest = (HttpClientRequest request) { | 28 conn.onRequest = (HttpClientRequest request) { |
| 27 OutputStream stream = request.outputStream; | 29 OutputStream stream = request.outputStream; |
| 28 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 30 if (explicitContentLength) { |
| 31 request.contentLength = 0; |
| 32 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 33 } |
| 29 stream.close(); | 34 stream.close(); |
| 30 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 35 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 31 }; | 36 }; |
| 32 conn.onResponse = (HttpClientResponse response) { | 37 conn.onResponse = (HttpClientResponse response) { |
| 38 Expect.equals("0", response.headers.value('content-length')); |
| 39 Expect.equals(0, response.contentLength); |
| 33 count++; | 40 count++; |
| 34 if (count == totalConnections) { | 41 if (count == totalConnections) { |
| 35 client.shutdown(); | 42 client.shutdown(); |
| 36 server.close(); | 43 server.close(); |
| 37 } | 44 } |
| 38 }; | 45 }; |
| 39 } | 46 } |
| 40 } | 47 } |
| 41 | 48 |
| 42 void testBody(int totalConnections) { | 49 void testBody(int totalConnections) { |
| 43 HttpServer server = new HttpServer(); | 50 HttpServer server = new HttpServer(); |
| 44 server.onError = (e) => Expect.fail("Unexpected error $e"); | 51 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| 45 server.listen("127.0.0.1", 0, backlog: totalConnections); | 52 server.listen("127.0.0.1", 0, backlog: totalConnections); |
| 46 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 53 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
| 54 Expect.equals("2", request.headers.value('content-length')); |
| 55 Expect.equals(2, request.contentLength); |
| 47 response.contentLength = 2; | 56 response.contentLength = 2; |
| 48 OutputStream stream = response.outputStream; | 57 OutputStream stream = response.outputStream; |
| 49 stream.writeString("x"); | 58 stream.writeString("x"); |
| 50 Expect.throws(() => response.contentLength = 3, (e) => e is HttpException); | 59 Expect.throws(() => response.contentLength = 3, (e) => e is HttpException); |
| 51 stream.writeString("x"); | 60 stream.writeString("x"); |
| 52 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 61 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 53 stream.close(); | 62 stream.close(); |
| 54 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 63 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 55 }; | 64 }; |
| 56 | 65 |
| 57 int count = 0; | 66 int count = 0; |
| 58 HttpClient client = new HttpClient(); | 67 HttpClient client = new HttpClient(); |
| 59 for (int i = 0; i < totalConnections; i++) { | 68 for (int i = 0; i < totalConnections; i++) { |
| 60 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 69 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 61 conn.onError = (e) => Expect.fail("Unexpected error $e"); | 70 conn.onError = (e) => Expect.fail("Unexpected error $e"); |
| 62 conn.onRequest = (HttpClientRequest request) { | 71 conn.onRequest = (HttpClientRequest request) { |
| 63 request.contentLength = 2; | 72 request.contentLength = 2; |
| 64 OutputStream stream = request.outputStream; | 73 OutputStream stream = request.outputStream; |
| 65 stream.writeString("x"); | 74 stream.writeString("x"); |
| 66 Expect.throws(() => request.contentLength = 3, (e) => e is HttpException); | 75 Expect.throws(() => request.contentLength = 3, (e) => e is HttpException); |
| 67 stream.writeString("x"); | 76 stream.writeString("x"); |
| 68 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 77 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 69 stream.close(); | 78 stream.close(); |
| 70 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 79 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 71 }; | 80 }; |
| 72 conn.onResponse = (HttpClientResponse response) { | 81 conn.onResponse = (HttpClientResponse response) { |
| 82 Expect.equals("2", response.headers.value('content-length')); |
| 83 Expect.equals(2, response.contentLength); |
| 73 count++; | 84 count++; |
| 74 if (count == totalConnections) { | 85 if (count == totalConnections) { |
| 75 client.shutdown(); | 86 client.shutdown(); |
| 76 server.close(); | 87 server.close(); |
| 77 } | 88 } |
| 78 }; | 89 }; |
| 79 } | 90 } |
| 80 } | 91 } |
| 81 | 92 |
| 82 void testHttp10() { | 93 void testHttp10() { |
| 83 HttpServer server = new HttpServer(); | 94 HttpServer server = new HttpServer(); |
| 84 server.onError = (e) => Expect.fail("Unexpected error $e"); | 95 server.onError = (e) => Expect.fail("Unexpected error $e"); |
| 85 server.listen("127.0.0.1", 0, backlog: 5); | 96 server.listen("127.0.0.1", 0, backlog: 5); |
| 86 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { | 97 server.defaultRequestHandler = (HttpRequest request, HttpResponse response) { |
| 98 Expect.isNull(request.headers.value('content-length')); |
| 99 Expect.equals(0, request.contentLength); |
| 100 response.contentLength = 0; |
| 87 OutputStream stream = response.outputStream; | 101 OutputStream stream = response.outputStream; |
| 88 Expect.equals("1.0", request.protocolVersion); | 102 Expect.equals("1.0", request.protocolVersion); |
| 89 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); | 103 Expect.throws(() => stream.writeString("x"), (e) => e is HttpException); |
| 90 response.contentLength = 0; | |
| 91 stream.close(); | 104 stream.close(); |
| 92 }; | 105 }; |
| 93 | 106 |
| 94 Socket socket = new Socket("127.0.0.1", server.port); | 107 Socket socket = new Socket("127.0.0.1", server.port); |
| 95 socket.onConnect = () { | 108 socket.onConnect = () { |
| 96 List<int> buffer = new List<int>(1024); | 109 List<int> buffer = new List<int>(1024); |
| 97 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); | 110 socket.outputStream.writeString("GET / HTTP/1.0\r\n\r\n"); |
| 98 socket.onData = () => socket.readList(buffer, 0, buffer.length); | 111 socket.onData = () => socket.readList(buffer, 0, buffer.length); |
| 99 socket.onClosed = () { | 112 socket.onClosed = () { |
| 100 socket.close(true); | 113 socket.close(true); |
| 101 server.close(); | 114 server.close(); |
| 102 }; | 115 }; |
| 103 }; | 116 }; |
| 104 } | 117 } |
| 105 | 118 |
| 106 void main() { | 119 void main() { |
| 107 testNoBody(5); | 120 testNoBody(5, false); |
| 121 testNoBody(5, true); |
| 108 testBody(5); | 122 testBody(5); |
| 109 testHttp10(); | 123 testHttp10(); |
| 110 } | 124 } |
| OLD | NEW |