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 |
| 11 import 'package:http_multi_server/http_multi_server.dart'; |
11 import 'package:shelf/shelf.dart' as shelf; | 12 import 'package:shelf/shelf.dart' as shelf; |
12 import 'package:shelf/shelf_io.dart' as shelf_io; | 13 import 'package:shelf/shelf_io.dart' as shelf_io; |
13 | 14 |
14 import 'scheduled_test.dart'; | 15 import 'scheduled_test.dart'; |
15 import 'src/scheduled_server/handler.dart'; | 16 import 'src/scheduled_server/handler.dart'; |
16 import 'src/utils.dart'; | 17 import 'src/utils.dart'; |
17 | 18 |
18 /// A class representing an HTTP server that's scheduled to run in the course of | 19 /// A class representing an HTTP server that's scheduled to run in the course of |
19 /// the test. This class allows the server's request handling to be scheduled | 20 /// the test. This class allows the server's request handling to be scheduled |
20 /// synchronously. | 21 /// synchronously. |
(...skipping 11 matching lines...) Expand all Loading... |
32 /// complete once the schedule reaches the point where that handler was | 33 /// complete once the schedule reaches the point where that handler was |
33 /// scheduled. | 34 /// scheduled. |
34 final _handlers = new Queue<Handler>(); | 35 final _handlers = new Queue<Handler>(); |
35 | 36 |
36 /// The number of servers created. Used for auto-generating descriptions; | 37 /// The number of servers created. Used for auto-generating descriptions; |
37 static var _count = 0; | 38 static var _count = 0; |
38 | 39 |
39 ScheduledServer._(this._server, this.description); | 40 ScheduledServer._(this._server, this.description); |
40 | 41 |
41 /// Creates a new server listening on an automatically-allocated port on | 42 /// Creates a new server listening on an automatically-allocated port on |
42 /// 127.0.0.1. [description] is used to refer to the server in debugging | 43 /// localhost (both IPv4 and IPv6, if available). |
43 /// messages. | 44 /// |
| 45 /// [description] is used to refer to the server in debugging messages. |
44 factory ScheduledServer([String description]) { | 46 factory ScheduledServer([String description]) { |
45 var id = _count++; | 47 var id = _count++; |
46 if (description == null) description = 'scheduled server $id'; | 48 if (description == null) description = 'scheduled server $id'; |
47 | 49 |
48 var scheduledServer; | 50 var scheduledServer; |
49 scheduledServer = new ScheduledServer._(schedule(() { | 51 scheduledServer = new ScheduledServer._(schedule(() { |
50 return shelf_io.serve(scheduledServer._handleRequest, "127.0.0.1", 0) | 52 return HttpMultiServer.loopback(0).then((server) { |
51 .then((server) { | 53 shelf_io.serveRequests(server, scheduledServer._handleRequest); |
52 currentSchedule.onComplete.schedule(() => server.close(force: true)); | 54 currentSchedule.onComplete.schedule(() => server.close(force: true)); |
53 return server; | 55 return server; |
54 }); | 56 }); |
55 }, "starting '$description'"), description); | 57 }, "starting '$description'"), description); |
56 return scheduledServer; | 58 return scheduledServer; |
57 } | 59 } |
58 | 60 |
59 /// The port on which the server is listening. | 61 /// The port on which the server is listening. |
60 Future<int> get port => _server.then((s) => s.port); | 62 Future<int> get port => _server.then((s) => s.port); |
61 | 63 |
62 /// The base URL of the server, including its port. | 64 /// The base URL of the server, including its port. |
63 Future<Uri> get url => port.then((p) => Uri.parse("http://127.0.0.1:$p")); | 65 Future<Uri> get url => port.then((p) => Uri.parse("http://localhost:$p")); |
64 | 66 |
65 /// Schedules [handler] to handle a request to the server with [method] and | 67 /// 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 | 68 /// [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. | 69 /// 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 | 70 /// Otherwise, it will run [fn]. If [fn] returns a [Future], the schedule will |
69 /// wait until that [Future] completes. | 71 /// wait until that [Future] completes. |
70 /// | 72 /// |
71 /// The request must be received at the point in the schedule at which | 73 /// 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 | 74 /// [handle] was called, or in the task immediately prior (to allow for |
73 /// non-deterministic asynchronicity). Otherwise, an error will be thrown. | 75 /// non-deterministic asynchronicity). Otherwise, an error will be thrown. |
(...skipping 21 matching lines...) Expand all Loading... |
95 }), 'receiving ${request.method} ${request.url.path}').catchError((error) { | 97 }), 'receiving ${request.method} ${request.url.path}').catchError((error) { |
96 // Don't let errors bubble up to the shelf handler. It will print them to | 98 // Don't let errors bubble up to the shelf handler. It will print them to |
97 // stderr, but the user will already be notified via the scheduled_test | 99 // stderr, but the user will already be notified via the scheduled_test |
98 // infrastructure. | 100 // infrastructure. |
99 return new shelf.Response.internalServerError( | 101 return new shelf.Response.internalServerError( |
100 body: error.toString(), | 102 body: error.toString(), |
101 headers: {'content-type': 'text/plain'}); | 103 headers: {'content-type': 'text/plain'}); |
102 }); | 104 }); |
103 } | 105 } |
104 } | 106 } |
OLD | NEW |