| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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:async"; | 5 import "dart:async"; |
| 6 import "dart:io"; |
| 6 import "dart:isolate"; | 7 import "dart:isolate"; |
| 7 import "dart:io"; | 8 |
| 8 import "package:sync_http/sync_http.dart"; | 9 import "package:sync_http/sync_http.dart"; |
| 9 import "package:test/test.dart"; | 10 import "package:test/test.dart"; |
| 10 | 11 |
| 11 typedef void ServerCallback(int port); | 12 typedef void ServerCallback(int port); |
| 12 | 13 |
| 13 class TestServerMain { | 14 class TestServerMain { |
| 14 TestServerMain() : _statusPort = new ReceivePort(); | 15 TestServerMain() : _statusPort = new ReceivePort(); |
| 15 | 16 |
| 16 ReceivePort _statusPort; // Port for receiving messages from the server. | 17 ReceivePort _statusPort; // Port for receiving messages from the server. |
| 17 SendPort _serverPort; // Port for sending messages to the server. | 18 SendPort _serverPort; // Port for sending messages to the server. |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 _requestHandlers["/0123456789"] = _zeroToTenHandler; | 167 _requestHandlers["/0123456789"] = _zeroToTenHandler; |
| 167 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; | 168 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; |
| 168 _requestHandlers["/host"] = _hostHandler; | 169 _requestHandlers["/host"] = _hostHandler; |
| 169 _requestHandlers["/huge"] = _hugeHandler; | 170 _requestHandlers["/huge"] = _hugeHandler; |
| 170 _dispatchPort = new ReceivePort(); | 171 _dispatchPort = new ReceivePort(); |
| 171 _dispatchPort.listen(dispatch); | 172 _dispatchPort.listen(dispatch); |
| 172 } | 173 } |
| 173 | 174 |
| 174 SendPort get dispatchSendPort => _dispatchPort.sendPort; | 175 SendPort get dispatchSendPort => _dispatchPort.sendPort; |
| 175 | 176 |
| 176 void dispatch(var message) { | 177 dispatch(var message) async { |
| 177 TestServerCommand command = message[0]; | 178 TestServerCommand command = message[0]; |
| 178 SendPort replyTo = message[1]; | 179 SendPort replyTo = message[1]; |
| 179 if (command.isStart) { | 180 if (command.isStart) { |
| 180 try { | 181 try { |
| 181 HttpServer.bind("127.0.0.1", 0).then((server) { | 182 var addr = (await InternetAddress.lookup("localhost"))[0]; |
| 183 HttpServer.bind(addr, 0).then((server) { |
| 182 _server = server; | 184 _server = server; |
| 183 _server.listen(_requestReceivedHandler); | 185 _server.listen(_requestReceivedHandler); |
| 184 replyTo.send(new TestServerStatus.started(_server.port)); | 186 replyTo.send(new TestServerStatus.started(_server.port)); |
| 185 }); | 187 }); |
| 186 } catch (e) { | 188 } catch (e) { |
| 187 replyTo.send(new TestServerStatus.error()); | 189 replyTo.send(new TestServerStatus.error()); |
| 188 } | 190 } |
| 189 } else if (command.isStop) { | 191 } else if (command.isStop) { |
| 190 _server.close(); | 192 _server.close(); |
| 191 _dispatchPort.close(); | 193 _dispatchPort.close(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 216 }); | 218 }); |
| 217 testServerMain.start(); | 219 testServerMain.start(); |
| 218 return completer.future; | 220 return completer.future; |
| 219 } | 221 } |
| 220 | 222 |
| 221 Future testGET() async { | 223 Future testGET() async { |
| 222 Completer completer = new Completer(); | 224 Completer completer = new Completer(); |
| 223 TestServerMain testServerMain = new TestServerMain(); | 225 TestServerMain testServerMain = new TestServerMain(); |
| 224 testServerMain.setServerStartedHandler((int port) { | 226 testServerMain.setServerStartedHandler((int port) { |
| 225 var request = | 227 var request = |
| 226 SyncHttpClient.getUrl(new Uri.http("127.0.0.1:$port", "/0123456789")); | 228 SyncHttpClient.getUrl(new Uri.http("localhost:$port", "/0123456789")); |
| 227 var response = request.close(); | 229 var response = request.close(); |
| 228 expect(HttpStatus.OK, equals(response.statusCode)); | 230 expect(HttpStatus.OK, equals(response.statusCode)); |
| 229 expect(11, equals(response.contentLength)); | 231 expect(11, equals(response.contentLength)); |
| 230 expect("01234567890", equals(response.body)); | 232 expect("01234567890", equals(response.body)); |
| 231 testServerMain.close(); | 233 testServerMain.close(); |
| 232 completer.complete(); | 234 completer.complete(); |
| 233 }); | 235 }); |
| 234 testServerMain.start(); | 236 testServerMain.start(); |
| 235 return completer.future; | 237 return completer.future; |
| 236 } | 238 } |
| 237 | 239 |
| 238 Future testPOST() async { | 240 Future testPOST() async { |
| 239 Completer completer = new Completer(); | 241 Completer completer = new Completer(); |
| 240 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; | 242 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; |
| 241 final int kMessageCount = 10; | 243 final int kMessageCount = 10; |
| 242 | 244 |
| 243 TestServerMain testServerMain = new TestServerMain(); | 245 TestServerMain testServerMain = new TestServerMain(); |
| 244 | 246 |
| 245 void runTest(int port) { | 247 void runTest(int port) { |
| 246 int count = 0; | 248 int count = 0; |
| 247 void sendRequest() { | 249 void sendRequest() { |
| 248 var request = | 250 var request = |
| 249 SyncHttpClient.postUrl(new Uri.http("127.0.0.1:$port", "/echo")); | 251 SyncHttpClient.postUrl(new Uri.http("localhost:$port", "/echo")); |
| 250 request.write(data); | 252 request.write(data); |
| 251 var response = request.close(); | 253 var response = request.close(); |
| 252 expect(HttpStatus.OK, equals(response.statusCode)); | 254 expect(HttpStatus.OK, equals(response.statusCode)); |
| 253 expect(data, equals(response.body)); | 255 expect(data, equals(response.body)); |
| 254 count++; | 256 count++; |
| 255 if (count < kMessageCount) { | 257 if (count < kMessageCount) { |
| 256 sendRequest(); | 258 sendRequest(); |
| 257 } else { | 259 } else { |
| 258 testServerMain.close(); | 260 testServerMain.close(); |
| 259 completer.complete(); | 261 completer.complete(); |
| 260 } | 262 } |
| 261 } | 263 } |
| 262 | 264 |
| 263 sendRequest(); | 265 sendRequest(); |
| 264 } | 266 } |
| 265 | 267 |
| 266 testServerMain.setServerStartedHandler(runTest); | 268 testServerMain.setServerStartedHandler(runTest); |
| 267 testServerMain.start(); | 269 testServerMain.start(); |
| 268 return completer.future; | 270 return completer.future; |
| 269 } | 271 } |
| 270 | 272 |
| 271 Future test404() async { | 273 Future test404() async { |
| 272 Completer completer = new Completer(); | 274 Completer completer = new Completer(); |
| 273 TestServerMain testServerMain = new TestServerMain(); | 275 TestServerMain testServerMain = new TestServerMain(); |
| 274 testServerMain.setServerStartedHandler((int port) { | 276 testServerMain.setServerStartedHandler((int port) { |
| 275 var request = SyncHttpClient | 277 var request = SyncHttpClient |
| 276 .getUrl(new Uri.http("127.0.0.1:$port", "/thisisnotfound")); | 278 .getUrl(new Uri.http("localhost:$port", "/thisisnotfound")); |
| 277 var response = request.close(); | 279 var response = request.close(); |
| 278 expect(HttpStatus.NOT_FOUND, equals(response.statusCode)); | 280 expect(HttpStatus.NOT_FOUND, equals(response.statusCode)); |
| 279 expect("Page not found", equals(response.body)); | 281 expect("Page not found", equals(response.body)); |
| 280 testServerMain.close(); | 282 testServerMain.close(); |
| 281 completer.complete(); | 283 completer.complete(); |
| 282 }); | 284 }); |
| 283 testServerMain.start(); | 285 testServerMain.start(); |
| 284 return completer.future; | 286 return completer.future; |
| 285 } | 287 } |
| 286 | 288 |
| 287 Future testReasonPhrase() async { | 289 Future testReasonPhrase() async { |
| 288 Completer completer = new Completer(); | 290 Completer completer = new Completer(); |
| 289 TestServerMain testServerMain = new TestServerMain(); | 291 TestServerMain testServerMain = new TestServerMain(); |
| 290 testServerMain.setServerStartedHandler((int port) { | 292 testServerMain.setServerStartedHandler((int port) { |
| 291 var request = SyncHttpClient | 293 var request = SyncHttpClient |
| 292 .getUrl(new Uri.http("127.0.0.1:$port", "/reasonformoving")); | 294 .getUrl(new Uri.http("localhost:$port", "/reasonformoving")); |
| 293 var response = request.close(); | 295 var response = request.close(); |
| 294 expect(HttpStatus.MOVED_PERMANENTLY, equals(response.statusCode)); | 296 expect(HttpStatus.MOVED_PERMANENTLY, equals(response.statusCode)); |
| 295 expect( | 297 expect( |
| 296 "Don't come looking here any more\r\n", equals(response.reasonPhrase)); | 298 "Don't come looking here any more\r\n", equals(response.reasonPhrase)); |
| 297 testServerMain.close(); | 299 testServerMain.close(); |
| 298 completer.complete(); | 300 completer.complete(); |
| 299 }); | 301 }); |
| 300 testServerMain.start(); | 302 testServerMain.start(); |
| 301 return completer.future; | 303 return completer.future; |
| 302 } | 304 } |
| 303 | 305 |
| 304 Future testHuge() async { | 306 Future testHuge() async { |
| 305 Completer completer = new Completer(); | 307 Completer completer = new Completer(); |
| 306 TestServerMain testServerMain = new TestServerMain(); | 308 TestServerMain testServerMain = new TestServerMain(); |
| 307 testServerMain.setServerStartedHandler((int port) { | 309 testServerMain.setServerStartedHandler((int port) { |
| 308 var request = | 310 var request = |
| 309 SyncHttpClient.getUrl(new Uri.http("127.0.0.1:$port", "/huge")); | 311 SyncHttpClient.getUrl(new Uri.http("localhost:$port", "/huge")); |
| 310 var response = request.close(); | 312 var response = request.close(); |
| 311 String expected = | 313 String expected = |
| 312 new List<int>.generate((1 << 20), (i) => (i + 1) % 256).toString(); | 314 new List<int>.generate((1 << 20), (i) => (i + 1) % 256).toString(); |
| 313 expect(HttpStatus.OK, equals(response.statusCode)); | 315 expect(HttpStatus.OK, equals(response.statusCode)); |
| 314 expect(expected.length, equals(response.contentLength)); | 316 expect(expected.length, equals(response.contentLength)); |
| 315 expect(expected.toString(), equals(response.body)); | 317 expect(expected.toString(), equals(response.body)); |
| 316 testServerMain.close(); | 318 testServerMain.close(); |
| 317 completer.complete(); | 319 completer.complete(); |
| 318 }); | 320 }); |
| 319 testServerMain.start(); | 321 testServerMain.start(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 333 test("Sync HTTP 404 test", () async { | 335 test("Sync HTTP 404 test", () async { |
| 334 await test404(); | 336 await test404(); |
| 335 }); | 337 }); |
| 336 test("Sync HTTP moved test", () async { | 338 test("Sync HTTP moved test", () async { |
| 337 await testReasonPhrase(); | 339 await testReasonPhrase(); |
| 338 }); | 340 }); |
| 339 test("Sync HTTP huge test", () async { | 341 test("Sync HTTP huge test", () async { |
| 340 await testHuge(); | 342 await testHuge(); |
| 341 }); | 343 }); |
| 342 } | 344 } |
| OLD | NEW |