| 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 | 8 |
| 9 import 'package:http/http.dart' as http; | 9 import 'package:http/http.dart' as http; |
| 10 import 'package:scheduled_test/scheduled_process.dart'; | 10 import 'package:scheduled_test/scheduled_process.dart'; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 """; | 85 """; |
| 86 } | 86 } |
| 87 | 87 |
| 88 /// Schedules starting the "pub serve" process. | 88 /// Schedules starting the "pub serve" process. |
| 89 /// | 89 /// |
| 90 /// If [shouldInstallFirst] is `true`, validates that pub install is run first. | 90 /// If [shouldInstallFirst] is `true`, validates that pub install is run first. |
| 91 /// | 91 /// |
| 92 /// Returns the `pub serve` process. | 92 /// Returns the `pub serve` process. |
| 93 ScheduledProcess startPubServe({bool shouldInstallFirst: false}) { | 93 ScheduledProcess startPubServe({bool shouldInstallFirst: false}) { |
| 94 // Use port 0 to get an ephemeral port. | 94 // Use port 0 to get an ephemeral port. |
| 95 _pubServer = startPub(args: ["serve", "--port=0", "--hostname=127.0.0.1"]); | 95 _pubServer = startPub(args: ["serve", "--port=0"]); |
| 96 | 96 |
| 97 if (shouldInstallFirst) { | 97 if (shouldInstallFirst) { |
| 98 expect(_pubServer.nextLine(), | 98 expect(_pubServer.nextLine(), |
| 99 completion(startsWith("Dependencies have changed"))); | 99 completion(startsWith("Dependencies have changed"))); |
| 100 expect(_pubServer.nextLine(), | 100 expect(_pubServer.nextLine(), |
| 101 completion(startsWith("Resolving dependencies..."))); | 101 completion(startsWith("Resolving dependencies..."))); |
| 102 expect(_pubServer.nextLine(), | 102 expect(_pubServer.nextLine(), |
| 103 completion(equals("Dependencies installed!"))); | 103 completion(equals("Dependencies installed!"))); |
| 104 } | 104 } |
| 105 | 105 |
| 106 expect(_pubServer.nextLine().then(_parsePort), completes); | 106 expect(_pubServer.nextLine().then(_parsePort), completes); |
| 107 return _pubServer; | 107 return _pubServer; |
| 108 } | 108 } |
| 109 | 109 |
| 110 /// Parses the port number from the "Serving blah on 127.0.0.1:1234" line | 110 /// Parses the port number from the "Serving blah on localhost:1234" line |
| 111 /// printed by pub serve. | 111 /// printed by pub serve. |
| 112 void _parsePort(String line) { | 112 void _parsePort(String line) { |
| 113 var match = new RegExp(r"127\.0\.0\.1:(\d+)").firstMatch(line); | 113 var match = new RegExp(r"localhost:(\d+)").firstMatch(line); |
| 114 assert(match != null); | 114 assert(match != null); |
| 115 _port = int.parse(match[1]); | 115 _port = int.parse(match[1]); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void endPubServe() { | 118 void endPubServe() { |
| 119 _pubServer.kill(); | 119 _pubServer.kill(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 /// Schedules an HTTP request to the running pub server with [urlPath] and | 122 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 123 /// verifies that it responds with [expected]. | 123 /// verifies that it responds with [expected]. |
| 124 void requestShouldSucceed(String urlPath, String expected) { | 124 void requestShouldSucceed(String urlPath, String expected) { |
| 125 schedule(() { | 125 schedule(() { |
| 126 return http.get("http://127.0.0.1:$_port/$urlPath").then((response) { | 126 return http.get("http://localhost:$_port/$urlPath").then((response) { |
| 127 expect(response.body, equals(expected)); | 127 expect(response.body, equals(expected)); |
| 128 }); | 128 }); |
| 129 }, "request $urlPath"); | 129 }, "request $urlPath"); |
| 130 } | 130 } |
| 131 | 131 |
| 132 /// Schedules an HTTP request to the running pub server with [urlPath] and | 132 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 133 /// verifies that it responds with a 404. | 133 /// verifies that it responds with a 404. |
| 134 void requestShould404(String urlPath) { | 134 void requestShould404(String urlPath) { |
| 135 schedule(() { | 135 schedule(() { |
| 136 return http.get("http://127.0.0.1:$_port/$urlPath").then((response) { | 136 return http.get("http://localhost:$_port/$urlPath").then((response) { |
| 137 expect(response.statusCode, equals(404)); | 137 expect(response.statusCode, equals(404)); |
| 138 }); | 138 }); |
| 139 }, "request $urlPath"); | 139 }, "request $urlPath"); |
| 140 } | 140 } |
| 141 | 141 |
| 142 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies | 142 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies |
| 143 /// that it responds with a 405. | 143 /// that it responds with a 405. |
| 144 void postShould405(String urlPath) { | 144 void postShould405(String urlPath) { |
| 145 schedule(() { | 145 schedule(() { |
| 146 return http.post("http://127.0.0.1:$_port/$urlPath").then((response) { | 146 return http.post("http://localhost:$_port/$urlPath").then((response) { |
| 147 expect(response.statusCode, equals(405)); | 147 expect(response.statusCode, equals(405)); |
| 148 }); | 148 }); |
| 149 }, "request $urlPath"); | 149 }, "request $urlPath"); |
| 150 } | 150 } |
| 151 | 151 |
| 152 /// Reads lines from pub serve's stdout until it prints the build success | 152 /// Reads lines from pub serve's stdout until it prints the build success |
| 153 /// message. | 153 /// message. |
| 154 /// | 154 /// |
| 155 /// The schedule will not proceed until the output is found. If not found, it | 155 /// The schedule will not proceed until the output is found. If not found, it |
| 156 /// will eventually time out. | 156 /// will eventually time out. |
| 157 void waitForBuildSuccess() { | 157 void waitForBuildSuccess() { |
| 158 nextLine() { | 158 nextLine() { |
| 159 return _pubServer.nextLine().then((line) { | 159 return _pubServer.nextLine().then((line) { |
| 160 if (line.contains("successfully")) return; | 160 if (line.contains("successfully")) return; |
| 161 | 161 |
| 162 // This line wasn't it, so ignore it and keep trying. | 162 // This line wasn't it, so ignore it and keep trying. |
| 163 return nextLine(); | 163 return nextLine(); |
| 164 }); | 164 }); |
| 165 } | 165 } |
| 166 | 166 |
| 167 schedule(nextLine); | 167 schedule(nextLine); |
| 168 } | 168 } |
| OLD | NEW |