OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import "dart:async"; |
| 6 import "dart:isolate"; |
| 7 import "dart:io"; |
| 8 import "package:sync_http/sync_http.dart"; |
| 9 import "package:test/test.dart"; |
| 10 |
| 11 typedef void ServerCallback(int port); |
| 12 |
| 13 class TestServerMain { |
| 14 TestServerMain() : _statusPort = new ReceivePort(); |
| 15 |
| 16 ReceivePort _statusPort; // Port for receiving messages from the server. |
| 17 SendPort _serverPort; // Port for sending messages to the server. |
| 18 ServerCallback _startedCallback; |
| 19 |
| 20 void setServerStartedHandler(ServerCallback startedCallback) { |
| 21 _startedCallback = startedCallback; |
| 22 } |
| 23 |
| 24 void start() { |
| 25 ReceivePort receivePort = new ReceivePort(); |
| 26 Isolate.spawn(startTestServer, receivePort.sendPort); |
| 27 receivePort.first.then((port) { |
| 28 _serverPort = port; |
| 29 |
| 30 // Send server start message to the server. |
| 31 var command = new TestServerCommand.start(); |
| 32 port.send([command, _statusPort.sendPort]); |
| 33 }); |
| 34 |
| 35 // Handle status messages from the server. |
| 36 _statusPort.listen((var status) { |
| 37 if (status.isStarted) { |
| 38 _startedCallback(status.port); |
| 39 } |
| 40 }); |
| 41 } |
| 42 |
| 43 void close() { |
| 44 // Send server stop message to the server. |
| 45 _serverPort.send([new TestServerCommand.stop(), _statusPort.sendPort]); |
| 46 _statusPort.close(); |
| 47 } |
| 48 } |
| 49 |
| 50 enum TestServerCommandState { |
| 51 start, |
| 52 stop, |
| 53 } |
| 54 |
| 55 class TestServerCommand { |
| 56 TestServerCommand.start() : _command = TestServerCommandState.start; |
| 57 TestServerCommand.stop() : _command = TestServerCommandState.stop; |
| 58 |
| 59 bool get isStart => (_command == TestServerCommandState.start); |
| 60 bool get isStop => (_command == TestServerCommandState.stop); |
| 61 |
| 62 TestServerCommandState _command; |
| 63 } |
| 64 |
| 65 enum TestServerStatusState { |
| 66 started, |
| 67 stopped, |
| 68 error, |
| 69 } |
| 70 |
| 71 class TestServerStatus { |
| 72 TestServerStatus.started(this._port) : _state = TestServerStatusState.started; |
| 73 TestServerStatus.stopped() : _state = TestServerStatusState.stopped; |
| 74 TestServerStatus.error() : _state = TestServerStatusState.error; |
| 75 |
| 76 bool get isStarted => (_state == TestServerStatusState.started); |
| 77 bool get isStopped => (_state == TestServerStatusState.stopped); |
| 78 bool get isError => (_state == TestServerStatusState.error); |
| 79 |
| 80 int get port => _port; |
| 81 |
| 82 TestServerStatusState _state; |
| 83 int _port; |
| 84 } |
| 85 |
| 86 void startTestServer(SendPort replyTo) { |
| 87 var server = new TestServer(); |
| 88 server.init(); |
| 89 replyTo.send(server.dispatchSendPort); |
| 90 } |
| 91 |
| 92 class TestServer { |
| 93 // Echo the request content back to the response. |
| 94 void _echoHandler(HttpRequest request) { |
| 95 var response = request.response; |
| 96 if (request.method != "POST") { |
| 97 response.close(); |
| 98 return; |
| 99 } |
| 100 response.contentLength = request.contentLength; |
| 101 request.listen((List<int> data) { |
| 102 var string = new String.fromCharCodes(data); |
| 103 response.write(string); |
| 104 response.close(); |
| 105 }); |
| 106 } |
| 107 |
| 108 // Echo the request content back to the response. |
| 109 void _zeroToTenHandler(HttpRequest request) { |
| 110 var response = request.response; |
| 111 String msg = "01234567890"; |
| 112 if (request.method != "GET") { |
| 113 response.close(); |
| 114 return; |
| 115 } |
| 116 response.contentLength = msg.length; |
| 117 response.write(msg); |
| 118 response.close(); |
| 119 } |
| 120 |
| 121 // Return a 404. |
| 122 void _notFoundHandler(HttpRequest request) { |
| 123 var response = request.response; |
| 124 response.statusCode = HttpStatus.NOT_FOUND; |
| 125 String msg = "Page not found"; |
| 126 response.contentLength = msg.length; |
| 127 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
| 128 response.write(msg); |
| 129 response.close(); |
| 130 } |
| 131 |
| 132 // Return a 301 with a custom reason phrase. |
| 133 void _reasonForMovingHandler(HttpRequest request) { |
| 134 var response = request.response; |
| 135 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 136 response.reasonPhrase = "Don't come looking here any more"; |
| 137 response.close(); |
| 138 } |
| 139 |
| 140 // Check the "Host" header. |
| 141 void _hostHandler(HttpRequest request) { |
| 142 var response = request.response; |
| 143 expect(1, equals(request.headers["Host"].length)); |
| 144 expect("www.dartlang.org:1234", equals(request.headers["Host"][0])); |
| 145 expect("www.dartlang.org", equals(request.headers.host)); |
| 146 expect(1234, equals(request.headers.port)); |
| 147 response.statusCode = HttpStatus.OK; |
| 148 response.close(); |
| 149 } |
| 150 |
| 151 void _hugeHandler(HttpRequest request) { |
| 152 var response = request.response; |
| 153 List<int> expected = |
| 154 new List<int>.generate((1 << 20), (i) => (i + 1) % 256); |
| 155 String msg = expected.toString(); |
| 156 response.contentLength = msg.length; |
| 157 response.statusCode = HttpStatus.OK; |
| 158 response.write(msg); |
| 159 response.close(); |
| 160 } |
| 161 |
| 162 void init() { |
| 163 // Setup request handlers. |
| 164 _requestHandlers = new Map(); |
| 165 _requestHandlers["/echo"] = _echoHandler; |
| 166 _requestHandlers["/0123456789"] = _zeroToTenHandler; |
| 167 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; |
| 168 _requestHandlers["/host"] = _hostHandler; |
| 169 _requestHandlers["/huge"] = _hugeHandler; |
| 170 _dispatchPort = new ReceivePort(); |
| 171 _dispatchPort.listen(dispatch); |
| 172 } |
| 173 |
| 174 SendPort get dispatchSendPort => _dispatchPort.sendPort; |
| 175 |
| 176 void dispatch(var message) { |
| 177 TestServerCommand command = message[0]; |
| 178 SendPort replyTo = message[1]; |
| 179 if (command.isStart) { |
| 180 try { |
| 181 HttpServer.bind("127.0.0.1", 0).then((server) { |
| 182 _server = server; |
| 183 _server.listen(_requestReceivedHandler); |
| 184 replyTo.send(new TestServerStatus.started(_server.port)); |
| 185 }); |
| 186 } catch (e) { |
| 187 replyTo.send(new TestServerStatus.error()); |
| 188 } |
| 189 } else if (command.isStop) { |
| 190 _server.close(); |
| 191 _dispatchPort.close(); |
| 192 replyTo.send(new TestServerStatus.stopped()); |
| 193 } |
| 194 } |
| 195 |
| 196 void _requestReceivedHandler(HttpRequest request) { |
| 197 var requestHandler = _requestHandlers[request.uri.path]; |
| 198 if (requestHandler != null) { |
| 199 requestHandler(request); |
| 200 } else { |
| 201 _notFoundHandler(request); |
| 202 } |
| 203 } |
| 204 |
| 205 HttpServer _server; // HTTP server instance. |
| 206 ReceivePort _dispatchPort; |
| 207 Map _requestHandlers; |
| 208 } |
| 209 |
| 210 Future testStartStop() async { |
| 211 Completer completer = new Completer(); |
| 212 TestServerMain testServerMain = new TestServerMain(); |
| 213 testServerMain.setServerStartedHandler((int port) { |
| 214 testServerMain.close(); |
| 215 completer.complete(); |
| 216 }); |
| 217 testServerMain.start(); |
| 218 return completer.future; |
| 219 } |
| 220 |
| 221 Future testGET() async { |
| 222 Completer completer = new Completer(); |
| 223 TestServerMain testServerMain = new TestServerMain(); |
| 224 testServerMain.setServerStartedHandler((int port) { |
| 225 var request = |
| 226 SyncHttpClient.getUrl(new Uri.http("127.0.0.1:$port", "/0123456789")); |
| 227 var response = request.close(); |
| 228 expect(HttpStatus.OK, equals(response.statusCode)); |
| 229 expect(11, equals(response.contentLength)); |
| 230 expect("01234567890", equals(response.body)); |
| 231 testServerMain.close(); |
| 232 completer.complete(); |
| 233 }); |
| 234 testServerMain.start(); |
| 235 return completer.future; |
| 236 } |
| 237 |
| 238 Future testPOST() async { |
| 239 Completer completer = new Completer(); |
| 240 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; |
| 241 final int kMessageCount = 10; |
| 242 |
| 243 TestServerMain testServerMain = new TestServerMain(); |
| 244 |
| 245 void runTest(int port) { |
| 246 int count = 0; |
| 247 void sendRequest() { |
| 248 var request = |
| 249 SyncHttpClient.postUrl(new Uri.http("127.0.0.1:$port", "/echo")); |
| 250 request.write(data); |
| 251 var response = request.close(); |
| 252 expect(HttpStatus.OK, equals(response.statusCode)); |
| 253 expect(data, equals(response.body)); |
| 254 count++; |
| 255 if (count < kMessageCount) { |
| 256 sendRequest(); |
| 257 } else { |
| 258 testServerMain.close(); |
| 259 completer.complete(); |
| 260 } |
| 261 } |
| 262 |
| 263 sendRequest(); |
| 264 } |
| 265 |
| 266 testServerMain.setServerStartedHandler(runTest); |
| 267 testServerMain.start(); |
| 268 return completer.future; |
| 269 } |
| 270 |
| 271 Future test404() async { |
| 272 Completer completer = new Completer(); |
| 273 TestServerMain testServerMain = new TestServerMain(); |
| 274 testServerMain.setServerStartedHandler((int port) { |
| 275 var request = SyncHttpClient |
| 276 .getUrl(new Uri.http("127.0.0.1:$port", "/thisisnotfound")); |
| 277 var response = request.close(); |
| 278 expect(HttpStatus.NOT_FOUND, equals(response.statusCode)); |
| 279 expect("Page not found", equals(response.body)); |
| 280 testServerMain.close(); |
| 281 completer.complete(); |
| 282 }); |
| 283 testServerMain.start(); |
| 284 return completer.future; |
| 285 } |
| 286 |
| 287 Future testReasonPhrase() async { |
| 288 Completer completer = new Completer(); |
| 289 TestServerMain testServerMain = new TestServerMain(); |
| 290 testServerMain.setServerStartedHandler((int port) { |
| 291 var request = SyncHttpClient |
| 292 .getUrl(new Uri.http("127.0.0.1:$port", "/reasonformoving")); |
| 293 var response = request.close(); |
| 294 expect(HttpStatus.MOVED_PERMANENTLY, equals(response.statusCode)); |
| 295 expect( |
| 296 "Don't come looking here any more\r\n", equals(response.reasonPhrase)); |
| 297 testServerMain.close(); |
| 298 completer.complete(); |
| 299 }); |
| 300 testServerMain.start(); |
| 301 return completer.future; |
| 302 } |
| 303 |
| 304 Future testHuge() async { |
| 305 Completer completer = new Completer(); |
| 306 TestServerMain testServerMain = new TestServerMain(); |
| 307 testServerMain.setServerStartedHandler((int port) { |
| 308 var request = |
| 309 SyncHttpClient.getUrl(new Uri.http("127.0.0.1:$port", "/huge")); |
| 310 var response = request.close(); |
| 311 String expected = |
| 312 new List<int>.generate((1 << 20), (i) => (i + 1) % 256).toString(); |
| 313 expect(HttpStatus.OK, equals(response.statusCode)); |
| 314 expect(expected.length, equals(response.contentLength)); |
| 315 expect(expected.toString(), equals(response.body)); |
| 316 testServerMain.close(); |
| 317 completer.complete(); |
| 318 }); |
| 319 testServerMain.start(); |
| 320 return completer.future; |
| 321 } |
| 322 |
| 323 void main() { |
| 324 test("Simple server test", () async { |
| 325 await testStartStop(); |
| 326 }); |
| 327 test("Sync HTTP GET test", () async { |
| 328 await testGET(); |
| 329 }); |
| 330 test("Sync HTTP POST test", () async { |
| 331 await testPOST(); |
| 332 }); |
| 333 test("Sync HTTP 404 test", () async { |
| 334 await test404(); |
| 335 }); |
| 336 test("Sync HTTP moved test", () async { |
| 337 await testReasonPhrase(); |
| 338 }); |
| 339 test("Sync HTTP huge test", () async { |
| 340 await testHuge(); |
| 341 }); |
| 342 } |
OLD | NEW |