| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library scheduled_test.scheduled_server; | 5 library scheduled_test.scheduled_server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 /// complete once the schedule reaches the point where that handler was | 31 /// complete once the schedule reaches the point where that handler was |
| 32 /// scheduled. | 32 /// scheduled. |
| 33 final _handlers = new Queue<Handler>(); | 33 final _handlers = new Queue<Handler>(); |
| 34 | 34 |
| 35 /// The number of servers created. Used for auto-generating descriptions; | 35 /// The number of servers created. Used for auto-generating descriptions; |
| 36 static var _count = 0; | 36 static var _count = 0; |
| 37 | 37 |
| 38 ScheduledServer._(this._server, this.description); | 38 ScheduledServer._(this._server, this.description); |
| 39 | 39 |
| 40 /// Creates a new server listening on an automatically-allocated port on | 40 /// Creates a new server listening on an automatically-allocated port on |
| 41 /// localhost. [description] is used to refer to the server in debugging | 41 /// 127.0.0.1. [description] is used to refer to the server in debugging |
| 42 /// messages. | 42 /// messages. |
| 43 factory ScheduledServer([String description]) { | 43 factory ScheduledServer([String description]) { |
| 44 var id = _count++; | 44 var id = _count++; |
| 45 if (description == null) description = 'scheduled server $id'; | 45 if (description == null) description = 'scheduled server $id'; |
| 46 | 46 |
| 47 var scheduledServer; | 47 var scheduledServer; |
| 48 scheduledServer = new ScheduledServer._(schedule(() { | 48 scheduledServer = new ScheduledServer._(schedule(() { |
| 49 return SafeHttpServer.bind("localhost", 0).then((server) { | 49 return SafeHttpServer.bind("127.0.0.1", 0).then((server) { |
| 50 server.listen(scheduledServer._handleRequest, | 50 server.listen(scheduledServer._handleRequest, |
| 51 onError: (e) => currentSchedule.signalError(e)); | 51 onError: (e) => currentSchedule.signalError(e)); |
| 52 currentSchedule.onComplete.schedule(server.close); | 52 currentSchedule.onComplete.schedule(server.close); |
| 53 return server; | 53 return server; |
| 54 }); | 54 }); |
| 55 }, "starting '$description'"), description); | 55 }, "starting '$description'"), description); |
| 56 return scheduledServer; | 56 return scheduledServer; |
| 57 } | 57 } |
| 58 | 58 |
| 59 /// The port on which the server is listening. | 59 /// The port on which the server is listening. |
| 60 Future<int> get port => _server.then((s) => s.port); | 60 Future<int> get port => _server.then((s) => s.port); |
| 61 | 61 |
| 62 /// The base URL of the server, including its port. | 62 /// The base URL of the server, including its port. |
| 63 Future<Uri> get url => port.then((p) => Uri.parse("http://localhost:$p")); | 63 Future<Uri> get url => port.then((p) => Uri.parse("http://127.0.0.1:$p")); |
| 64 | 64 |
| 65 /// Schedules [handler] to handle a request to the server with [method] and | 65 /// Schedules [handler] to handle a request to the server with [method] and |
| 66 /// [path]. The schedule will wait until an HTTP request is received. If that | 66 /// [path]. The schedule will wait until an HTTP request is received. If that |
| 67 /// request doesn't have the expected [method] and [path], it will fail. | 67 /// request doesn't have the expected [method] and [path], it will fail. |
| 68 /// Otherwise, it will run [fn]. If [fn] returns a [Future], the schedule will | 68 /// Otherwise, it will run [fn]. If [fn] returns a [Future], the schedule will |
| 69 /// wait until that [Future] completes. | 69 /// wait until that [Future] completes. |
| 70 /// | 70 /// |
| 71 /// The request must be received at the point in the schedule at which | 71 /// The request must be received at the point in the schedule at which |
| 72 /// [handle] was called, or in the task immediately prior (to allow for | 72 /// [handle] was called, or in the task immediately prior (to allow for |
| 73 /// non-deterministic asynchronicity). Otherwise, an error will be thrown. | 73 /// non-deterministic asynchronicity). Otherwise, an error will be thrown. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 95 "when no more requests were expected."); | 95 "when no more requests were expected."); |
| 96 } | 96 } |
| 97 return _handlers.removeFirst().fn(request); | 97 return _handlers.removeFirst().fn(request); |
| 98 }).catchError((e) { | 98 }).catchError((e) { |
| 99 // Close the server so that we don't leave a dangling request. | 99 // Close the server so that we don't leave a dangling request. |
| 100 _server.then((s) => s.close()); | 100 _server.then((s) => s.close()); |
| 101 throw e; | 101 throw e; |
| 102 }), 'receiving ${request.method} ${request.uri}'); | 102 }), 'receiving ${request.method} ${request.uri}'); |
| 103 } | 103 } |
| 104 } | 104 } |
| OLD | NEW |