| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 #import("dart:isolate"); |
| 11 #import("dart:io"); |
| 12 |
| 13 class TestServerMain { |
| 14 TestServerMain() |
| 15 : _statusPort = new ReceivePort(), |
| 16 _serverPort = null { |
| 17 new TestServer().spawn().then((SendPort port) { |
| 18 _serverPort = port; |
| 19 }); |
| 20 } |
| 21 |
| 22 void setServerStartedHandler(void startedCallback(int port)) { |
| 23 _startedCallback = startedCallback; |
| 24 } |
| 25 |
| 26 void start() { |
| 27 // Handle status messages from the server. |
| 28 _statusPort.receive((var status, SendPort replyTo) { |
| 29 if (status.isStarted) { |
| 30 _startedCallback(status.port); |
| 31 } |
| 32 }); |
| 33 |
| 34 // Send server start message to the server. |
| 35 var command = new TestServerCommand.start(); |
| 36 _serverPort.send(command, _statusPort.toSendPort()); |
| 37 } |
| 38 |
| 39 void shutdown() { |
| 40 // Send server stop message to the server. |
| 41 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort()); |
| 42 _statusPort.close(); |
| 43 } |
| 44 |
| 45 void chunkedEncoding() { |
| 46 // Send chunked encoding message to the server. |
| 47 _serverPort.send( |
| 48 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort()); |
| 49 } |
| 50 |
| 51 ReceivePort _statusPort; // Port for receiving messages from the server. |
| 52 SendPort _serverPort; // Port for sending messages to the server. |
| 53 var _startedCallback; |
| 54 } |
| 55 |
| 56 |
| 57 class TestServerCommand { |
| 58 static final START = 0; |
| 59 static final STOP = 1; |
| 60 static final CHUNKED_ENCODING = 2; |
| 61 |
| 62 TestServerCommand.start() : _command = START; |
| 63 TestServerCommand.stop() : _command = STOP; |
| 64 TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING; |
| 65 |
| 66 bool get isStart() => _command == START; |
| 67 bool get isStop() => _command == STOP; |
| 68 bool get isChunkedEncoding() => _command == CHUNKED_ENCODING; |
| 69 |
| 70 int _command; |
| 71 } |
| 72 |
| 73 |
| 74 class TestServerStatus { |
| 75 static final STARTED = 0; |
| 76 static final STOPPED = 1; |
| 77 static final ERROR = 2; |
| 78 |
| 79 TestServerStatus.started(this._port) : _state = STARTED; |
| 80 TestServerStatus.stopped() : _state = STOPPED; |
| 81 TestServerStatus.error() : _state = ERROR; |
| 82 |
| 83 bool get isStarted() => _state == STARTED; |
| 84 bool get isStopped() => _state == STOPPED; |
| 85 bool get isError() => _state == ERROR; |
| 86 |
| 87 int get port() => _port; |
| 88 |
| 89 int _state; |
| 90 int _port; |
| 91 } |
| 92 |
| 93 |
| 94 class TestServer extends Isolate { |
| 95 // Echo the request content back to the response. |
| 96 void _echoHandler(HttpRequest request, HttpResponse response) { |
| 97 Expect.equals("POST", request.method); |
| 98 response.contentLength = request.contentLength; |
| 99 request.inputStream.pipe(response.outputStream); |
| 100 } |
| 101 |
| 102 // Return a 404. |
| 103 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
| 104 response.statusCode = HttpStatus.NOT_FOUND; |
| 105 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
| 106 response.outputStream.writeString("Page not found"); |
| 107 response.outputStream.close(); |
| 108 } |
| 109 |
| 110 |
| 111 void main() { |
| 112 // Setup request handlers. |
| 113 _requestHandlers = new Map(); |
| 114 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
| 115 _echoHandler(request, response); |
| 116 }; |
| 117 |
| 118 this.port.receive((var message, SendPort replyTo) { |
| 119 if (message.isStart) { |
| 120 _server = new HttpServer(); |
| 121 try { |
| 122 _server.listen("127.0.0.1", 0); |
| 123 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
| 124 _requestReceivedHandler(req, rsp); |
| 125 }; |
| 126 replyTo.send(new TestServerStatus.started(_server.port), null); |
| 127 } catch (var e) { |
| 128 replyTo.send(new TestServerStatus.error(), null); |
| 129 } |
| 130 } else if (message.isStop) { |
| 131 _server.close(); |
| 132 this.port.close(); |
| 133 replyTo.send(new TestServerStatus.stopped(), null); |
| 134 } else if (message.isChunkedEncoding) { |
| 135 _chunkedEncoding = true; |
| 136 } |
| 137 }); |
| 138 } |
| 139 |
| 140 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { |
| 141 var requestHandler =_requestHandlers[request.path]; |
| 142 if (requestHandler != null) { |
| 143 requestHandler(request, response); |
| 144 } else { |
| 145 _notFoundHandler(request, response); |
| 146 } |
| 147 } |
| 148 |
| 149 HttpServer _server; // HTTP server instance. |
| 150 Map _requestHandlers; |
| 151 bool _chunkedEncoding = false; |
| 152 } |
| 153 |
| 154 void testReadInto(bool chunkedEncoding) { |
| 155 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 156 final int kMessageCount = 10; |
| 157 |
| 158 TestServerMain testServerMain = new TestServerMain(); |
| 159 |
| 160 void runTest(int port) { |
| 161 int count = 0; |
| 162 HttpClient httpClient = new HttpClient(); |
| 163 void sendRequest() { |
| 164 HttpClientConnection conn = |
| 165 httpClient.post("127.0.0.1", port, "/echo"); |
| 166 conn.onRequest = (HttpClientRequest request) { |
| 167 if (chunkedEncoding) { |
| 168 request.outputStream.writeString(data.substring(0, 10)); |
| 169 request.outputStream.writeString(data.substring(10, data.length)); |
| 170 } else { |
| 171 request.contentLength = data.length; |
| 172 request.outputStream.write(data.charCodes()); |
| 173 } |
| 174 request.outputStream.close(); |
| 175 }; |
| 176 conn.onResponse = (HttpClientResponse response) { |
| 177 Expect.equals(HttpStatus.OK, response.statusCode); |
| 178 InputStream stream = response.inputStream; |
| 179 List<int> body = new List<int>(); |
| 180 stream.onData = () { |
| 181 List tmp = new List(3); |
| 182 int bytes = stream.readInto(tmp); |
| 183 body.addAll(tmp.getRange(0, bytes)); |
| 184 }; |
| 185 stream.onClosed = () { |
| 186 Expect.equals(data, new String.fromCharCodes(body)); |
| 187 count++; |
| 188 if (count < kMessageCount) { |
| 189 sendRequest(); |
| 190 } else { |
| 191 httpClient.shutdown(); |
| 192 testServerMain.shutdown(); |
| 193 } |
| 194 }; |
| 195 }; |
| 196 } |
| 197 |
| 198 sendRequest(); |
| 199 } |
| 200 |
| 201 testServerMain.setServerStartedHandler(runTest); |
| 202 if (chunkedEncoding) { |
| 203 testServerMain.chunkedEncoding(); |
| 204 } |
| 205 testServerMain.start(); |
| 206 } |
| 207 |
| 208 void testReadShort(bool chunkedEncoding) { |
| 209 String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 210 final int kMessageCount = 10; |
| 211 |
| 212 TestServerMain testServerMain = new TestServerMain(); |
| 213 |
| 214 void runTest(int port) { |
| 215 int count = 0; |
| 216 HttpClient httpClient = new HttpClient(); |
| 217 void sendRequest() { |
| 218 HttpClientConnection conn = |
| 219 httpClient.post("127.0.0.1", port, "/echo"); |
| 220 conn.onRequest = (HttpClientRequest request) { |
| 221 if (chunkedEncoding) { |
| 222 request.outputStream.writeString(data.substring(0, 10)); |
| 223 request.outputStream.writeString(data.substring(10, data.length)); |
| 224 } else { |
| 225 request.contentLength = data.length; |
| 226 request.outputStream.write(data.charCodes()); |
| 227 } |
| 228 request.outputStream.close(); |
| 229 }; |
| 230 conn.onResponse = (HttpClientResponse response) { |
| 231 Expect.equals(HttpStatus.OK, response.statusCode); |
| 232 InputStream stream = response.inputStream; |
| 233 List<int> body = new List<int>(); |
| 234 stream.onData = () { |
| 235 List tmp = stream.read(2); |
| 236 body.addAll(tmp); |
| 237 }; |
| 238 stream.onClosed = () { |
| 239 Expect.equals(data, new String.fromCharCodes(body)); |
| 240 count++; |
| 241 if (count < kMessageCount) { |
| 242 sendRequest(); |
| 243 } else { |
| 244 httpClient.shutdown(); |
| 245 testServerMain.shutdown(); |
| 246 } |
| 247 }; |
| 248 }; |
| 249 } |
| 250 |
| 251 sendRequest(); |
| 252 } |
| 253 |
| 254 testServerMain.setServerStartedHandler(runTest); |
| 255 if (chunkedEncoding) { |
| 256 testServerMain.chunkedEncoding(); |
| 257 } |
| 258 testServerMain.start(); |
| 259 } |
| 260 |
| 261 void main() { |
| 262 testReadInto(true); |
| 263 testReadInto(false); |
| 264 testReadShort(true); |
| 265 testReadShort(false); |
| 266 } |
| OLD | NEW |