OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.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 pub_tests; | 5 library pub_tests; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
11 import 'package:scheduled_test/scheduled_process.dart'; | 11 import 'package:scheduled_test/scheduled_process.dart'; |
12 import 'package:scheduled_test/scheduled_test.dart'; | 12 import 'package:scheduled_test/scheduled_test.dart'; |
13 | 13 |
14 import '../descriptor.dart' as d; | 14 import '../descriptor.dart' as d; |
15 import '../test_pub.dart'; | 15 import '../test_pub.dart'; |
16 | 16 |
17 /// The pub process running "pub serve". | 17 /// The pub process running "pub serve". |
18 ScheduledProcess _pubServer; | 18 ScheduledProcess _pubServer; |
19 | 19 |
20 /// The ephemeral port assigned to the running server. | 20 /// The ephemeral port assigned to the running server. |
21 int _port; | 21 int _port; |
22 | 22 |
23 /// Schedules starting the "pub serve" process. | 23 /// Schedules starting the "pub serve" process. |
24 void startPubServe() { | 24 /// |
| 25 /// If [shouldInstallFirst] is `true`, validates that pub install is run first. |
| 26 void startPubServe({bool shouldInstallFirst: false}) { |
25 // Use port 0 to get an ephemeral port. | 27 // Use port 0 to get an ephemeral port. |
26 _pubServer = startPub(args: ["serve", "--port=0"]); | 28 _pubServer = startPub(args: ["serve", "--port=0"]); |
27 | 29 |
28 expect(_pubServer.nextLine().then((line) { | 30 if (shouldInstallFirst) { |
29 var match = new RegExp(r"localhost:(\d+)").firstMatch(line); | 31 expect(_pubServer.nextLine(), |
30 assert(match != null); | 32 completion(startsWith("Dependencies have changed"))); |
31 _port = int.parse(match[1]); | 33 expect(_pubServer.nextLine(), |
32 }), completes); | 34 completion(startsWith("Resolving dependencies..."))); |
| 35 expect(_pubServer.nextLine(), |
| 36 completion(equals("Dependencies installed!"))); |
| 37 } |
| 38 |
| 39 expect(_pubServer.nextLine().then(_parsePort), completes); |
| 40 } |
| 41 |
| 42 /// Parses the port number from the "Serving blah on localhost:1234" line |
| 43 /// printed by pub serve. |
| 44 void _parsePort(String line) { |
| 45 var match = new RegExp(r"localhost:(\d+)").firstMatch(line); |
| 46 assert(match != null); |
| 47 _port = int.parse(match[1]); |
33 } | 48 } |
34 | 49 |
35 void endPubServe() { | 50 void endPubServe() { |
36 _pubServer.kill(); | 51 _pubServer.kill(); |
37 } | 52 } |
38 | 53 |
39 /// Schedules an HTTP request to the running pub server with [urlPath] and | 54 /// Schedules an HTTP request to the running pub server with [urlPath] and |
40 /// verifies that it responds with [expected]. | 55 /// verifies that it responds with [expected]. |
41 void requestShouldSucceed(String urlPath, String expected) { | 56 void requestShouldSucceed(String urlPath, String expected) { |
42 schedule(() { | 57 schedule(() { |
43 return http.get("http://localhost:$_port/$urlPath").then((response) { | 58 return http.get("http://localhost:$_port/$urlPath").then((response) { |
44 expect(response.body, equals(expected)); | 59 expect(response.body, equals(expected)); |
45 }); | 60 }); |
46 }, "request $urlPath"); | 61 }, "request $urlPath"); |
47 } | 62 } |
48 | 63 |
49 /// Schedules an HTTP request to the running pub server with [urlPath] and | 64 /// Schedules an HTTP request to the running pub server with [urlPath] and |
50 /// verifies that it responds with a 404. | 65 /// verifies that it responds with a 404. |
51 void requestShould404(String urlPath) { | 66 void requestShould404(String urlPath) { |
52 schedule(() { | 67 schedule(() { |
53 return http.get("http://localhost:$_port/$urlPath").then((response) { | 68 return http.get("http://localhost:$_port/$urlPath").then((response) { |
54 expect(response.statusCode, equals(404)); | 69 expect(response.statusCode, equals(404)); |
55 }); | 70 }); |
56 }, "request $urlPath"); | 71 }, "request $urlPath"); |
57 } | 72 } |
OLD | NEW |