| 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:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'package:http/http.dart' as http; | 11 import 'package:http/http.dart' as http; |
| 12 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
| 13 import 'package:scheduled_test/scheduled_process.dart'; | 13 import 'package:scheduled_test/scheduled_process.dart'; |
| 14 import 'package:scheduled_test/scheduled_stream.dart'; | 14 import 'package:scheduled_test/scheduled_stream.dart'; |
| 15 import 'package:scheduled_test/scheduled_test.dart'; | 15 import 'package:scheduled_test/scheduled_test.dart'; |
| 16 | 16 |
| 17 import '../descriptor.dart' as d; |
| 17 import '../test_pub.dart'; | 18 import '../test_pub.dart'; |
| 18 | 19 |
| 19 /// The pub process running "pub serve". | 20 /// The pub process running "pub serve". |
| 20 ScheduledProcess _pubServer; | 21 ScheduledProcess _pubServer; |
| 21 | 22 |
| 22 /// The ephemeral port assigned to the running server. | 23 /// The ephemeral ports assigned to the running servers, associated with the |
| 23 int _port; | 24 /// directories they're serving. |
| 25 final _ports = new Map<String, int>(); |
| 24 | 26 |
| 25 /// The web socket connection to the running pub process, or `null` if no | 27 /// The web socket connection to the running pub process, or `null` if no |
| 26 /// connection has been made. | 28 /// connection has been made. |
| 27 WebSocket _webSocket; | 29 WebSocket _webSocket; |
| 28 Stream _webSocketBroadcastStream; | 30 Stream _webSocketBroadcastStream; |
| 29 | 31 |
| 30 /// The code for a transformer that renames ".txt" files to ".out" and adds a | 32 /// The code for a transformer that renames ".txt" files to ".out" and adds a |
| 31 /// ".out" suffix. | 33 /// ".out" suffix. |
| 32 const REWRITE_TRANSFORMER = """ | 34 const REWRITE_TRANSFORMER = """ |
| 33 import 'dart:async'; | 35 import 'dart:async'; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 95 } |
| 94 """; | 96 """; |
| 95 } | 97 } |
| 96 | 98 |
| 97 /// Schedules starting the `pub serve` process. | 99 /// Schedules starting the `pub serve` process. |
| 98 /// | 100 /// |
| 99 /// Unlike [pubServe], this doesn't determine the port number of the server, and | 101 /// Unlike [pubServe], this doesn't determine the port number of the server, and |
| 100 /// so may be used to test for errors in the initialization process. | 102 /// so may be used to test for errors in the initialization process. |
| 101 /// | 103 /// |
| 102 /// Returns the `pub serve` process. | 104 /// Returns the `pub serve` process. |
| 103 ScheduledProcess startPubServe([Iterable<String> args]) { | 105 ScheduledProcess startPubServe({Iterable<String> args, |
| 106 bool createWebDir: true}) { |
| 104 // Use port 0 to get an ephemeral port. | 107 // Use port 0 to get an ephemeral port. |
| 105 var pubArgs = ["serve", "--port=0", "--hostname=127.0.0.1", "--force-poll"]; | 108 var pubArgs = ["serve", "--port=0", "--hostname=127.0.0.1", "--force-poll"]; |
| 106 | 109 |
| 107 if (args != null) pubArgs.addAll(args); | 110 if (args != null) pubArgs.addAll(args); |
| 108 | 111 |
| 109 // Dart2js can take a long time to compile dart code, so we increase the | 112 // Dart2js can take a long time to compile dart code, so we increase the |
| 110 // timeout to cope with that. | 113 // timeout to cope with that. |
| 111 currentSchedule.timeout *= 1.5; | 114 currentSchedule.timeout *= 1.5; |
| 112 | 115 |
| 116 if (createWebDir) d.dir(appPath, [d.dir("web")]).create(); |
| 113 return startPub(args: pubArgs); | 117 return startPub(args: pubArgs); |
| 114 } | 118 } |
| 115 | 119 |
| 116 /// Schedules starting the "pub serve" process and records its port number for | 120 /// Schedules starting the "pub serve" process and records its port number for |
| 117 /// future requests. | 121 /// future requests. |
| 118 /// | 122 /// |
| 119 /// If [shouldGetFirst] is `true`, validates that pub get is run first. | 123 /// If [shouldGetFirst] is `true`, validates that pub get is run first. |
| 120 /// | 124 /// |
| 125 /// If [createWebDir] is `true`, creates a `web/` directory if one doesn't exist |
| 126 /// so pub doesn't complain about having nothing to serve. |
| 127 /// |
| 121 /// Returns the `pub serve` process. | 128 /// Returns the `pub serve` process. |
| 122 ScheduledProcess pubServe({bool shouldGetFirst: false, Iterable<String> args}) { | 129 ScheduledProcess pubServe({bool shouldGetFirst: false, bool createWebDir: true, |
| 123 _pubServer = startPubServe(args); | 130 Iterable<String> args}) { |
| 131 _pubServer = startPubServe(args: args, createWebDir: createWebDir); |
| 124 | 132 |
| 125 currentSchedule.onComplete.schedule(() { | 133 currentSchedule.onComplete.schedule(() { |
| 134 _ports.clear(); |
| 135 |
| 126 if (_webSocket != null) { | 136 if (_webSocket != null) { |
| 127 _webSocket.close(); | 137 _webSocket.close(); |
| 128 _webSocket = null; | 138 _webSocket = null; |
| 129 _webSocketBroadcastStream = null; | 139 _webSocketBroadcastStream = null; |
| 130 } | 140 } |
| 131 }); | 141 }); |
| 132 | 142 |
| 133 if (shouldGetFirst) { | 143 if (shouldGetFirst) { |
| 134 _pubServer.stdout.expect(consumeThrough("Got dependencies!")); | 144 _pubServer.stdout.expect(consumeThrough("Got dependencies!")); |
| 135 } | 145 } |
| 136 | 146 |
| 137 expect(schedule(() => _pubServer.stdout.next()).then(_parsePort), completes); | 147 // The server should emit one or more ports. |
| 148 _pubServer.stdout.expect( |
| 149 consumeWhile(predicate(_parsePort, 'emits server url'))); |
| 150 schedule(() => expect(_ports, isNot(isEmpty))); |
| 151 |
| 138 return _pubServer; | 152 return _pubServer; |
| 139 } | 153 } |
| 140 | 154 |
| 155 /// The regular expression for parsing pub's output line describing the URL for |
| 156 /// the server. |
| 157 final _parsePortRegExp = new RegExp(r"([^ ]+) +on http://127\.0\.0\.1:(\d+)"); |
| 158 |
| 141 /// Parses the port number from the "Serving blah on 127.0.0.1:1234" line | 159 /// Parses the port number from the "Serving blah on 127.0.0.1:1234" line |
| 142 /// printed by pub serve. | 160 /// printed by pub serve. |
| 143 void _parsePort(String line) { | 161 bool _parsePort(String line) { |
| 144 var match = new RegExp(r"127\.0\.0\.1:(\d+)").firstMatch(line); | 162 var match = _parsePortRegExp.firstMatch(line); |
| 145 assert(match != null); | 163 if (match == null) return false; |
| 146 _port = int.parse(match[1]); | 164 _ports[match[1]] = int.parse(match[2]); |
| 165 return true; |
| 147 } | 166 } |
| 148 | 167 |
| 149 void endPubServe() { | 168 void endPubServe() { |
| 150 _pubServer.kill(); | 169 _pubServer.kill(); |
| 151 } | 170 } |
| 152 | 171 |
| 153 /// Schedules an HTTP request to the running pub server with [urlPath] and | 172 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 154 /// verifies that it responds with a body that matches [expectation]. | 173 /// verifies that it responds with a body that matches [expectation]. |
| 155 /// | 174 /// |
| 156 /// [expectation] may either be a [Matcher] or a string to match an exact body. | 175 /// [expectation] may either be a [Matcher] or a string to match an exact body. |
| 176 /// [root] indicates which server should be accessed, and defaults to "web". |
| 157 /// [headers] may be either a [Matcher] or a map to match an exact headers map. | 177 /// [headers] may be either a [Matcher] or a map to match an exact headers map. |
| 158 void requestShouldSucceed(String urlPath, expectation, {headers}) { | 178 void requestShouldSucceed(String urlPath, expectation, {String root, headers}) { |
| 159 schedule(() { | 179 schedule(() { |
| 160 return http.get("http://127.0.0.1:$_port/$urlPath").then((response) { | 180 return http.get("${_serverUrl(root)}/$urlPath").then((response) { |
| 161 if (expectation != null) expect(response.body, expectation); | 181 if (expectation != null) expect(response.body, expectation); |
| 162 if (headers != null) expect(response.headers, headers); | 182 if (headers != null) expect(response.headers, headers); |
| 163 }); | 183 }); |
| 164 }, "request $urlPath"); | 184 }, "request $urlPath"); |
| 165 } | 185 } |
| 166 | 186 |
| 167 /// Schedules an HTTP request to the running pub server with [urlPath] and | 187 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 168 /// verifies that it responds with a 404. | 188 /// verifies that it responds with a 404. |
| 169 void requestShould404(String urlPath) { | 189 /// |
| 190 /// [root] indicates which server should be accessed, and defaults to "web". |
| 191 void requestShould404(String urlPath, {String root}) { |
| 170 schedule(() { | 192 schedule(() { |
| 171 return http.get("http://127.0.0.1:$_port/$urlPath").then((response) { | 193 return http.get("${_serverUrl(root)}/$urlPath").then((response) { |
| 172 expect(response.statusCode, equals(404)); | 194 expect(response.statusCode, equals(404)); |
| 173 }); | 195 }); |
| 174 }, "request $urlPath"); | 196 }, "request $urlPath"); |
| 175 } | 197 } |
| 176 | 198 |
| 177 /// Schedules an HTTP request to the running pub server with [urlPath] and | 199 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 178 /// verifies that it responds with a redirect to the given [redirectTarget]. | 200 /// verifies that it responds with a redirect to the given [redirectTarget]. |
| 179 /// | 201 /// |
| 180 /// [redirectTarget] may be either a [Matcher] or a string to match an exact | 202 /// [redirectTarget] may be either a [Matcher] or a string to match an exact |
| 181 /// URL. | 203 /// URL. [root] indicates which server should be accessed, and defaults to |
| 182 void requestShouldRedirect(String urlPath, redirectTarget) { | 204 /// "web". |
| 205 void requestShouldRedirect(String urlPath, redirectTarget, {String root}) { |
| 183 schedule(() { | 206 schedule(() { |
| 184 var request = new http.Request("GET", | 207 var request = new http.Request("GET", |
| 185 Uri.parse("http://127.0.0.1:$_port/$urlPath")); | 208 Uri.parse("${_serverUrl(root)}/$urlPath")); |
| 186 request.followRedirects = false; | 209 request.followRedirects = false; |
| 187 return request.send().then((response) { | 210 return request.send().then((response) { |
| 188 expect(response.statusCode ~/ 100, equals(3)); | 211 expect(response.statusCode ~/ 100, equals(3)); |
| 189 | 212 |
| 190 expect(response.headers, containsPair('location', redirectTarget)); | 213 expect(response.headers, containsPair('location', redirectTarget)); |
| 191 }); | 214 }); |
| 192 }, "request $urlPath"); | 215 }, "request $urlPath"); |
| 193 } | 216 } |
| 194 | 217 |
| 195 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies | 218 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies |
| 196 /// that it responds with a 405. | 219 /// that it responds with a 405. |
| 197 void postShould405(String urlPath) { | 220 /// |
| 221 /// [root] indicates which server should be accessed, and defaults to "web". |
| 222 void postShould405(String urlPath, {String root}) { |
| 198 schedule(() { | 223 schedule(() { |
| 199 return http.post("http://127.0.0.1:$_port/$urlPath").then((response) { | 224 return http.post("${_serverUrl(root)}/$urlPath").then((response) { |
| 200 expect(response.statusCode, equals(405)); | 225 expect(response.statusCode, equals(405)); |
| 201 }); | 226 }); |
| 202 }, "request $urlPath"); | 227 }, "request $urlPath"); |
| 203 } | 228 } |
| 204 | 229 |
| 205 /// Reads lines from pub serve's stdout until it prints the build success | 230 /// Reads lines from pub serve's stdout until it prints the build success |
| 206 /// message. | 231 /// message. |
| 207 /// | 232 /// |
| 208 /// The schedule will not proceed until the output is found. If not found, it | 233 /// The schedule will not proceed until the output is found. If not found, it |
| 209 /// will eventually time out. | 234 /// will eventually time out. |
| 210 void waitForBuildSuccess() => | 235 void waitForBuildSuccess() => |
| 211 _pubServer.stdout.expect(consumeThrough(contains("successfully"))); | 236 _pubServer.stdout.expect(consumeThrough(contains("successfully"))); |
| 212 | 237 |
| 213 /// Schedules opening a web socket connection to the currently running pub | 238 /// Schedules opening a web socket connection to the currently running pub |
| 214 /// serve. | 239 /// serve. |
| 215 Future _ensureWebSocket() { | 240 Future _ensureWebSocket() { |
| 216 // Use the existing one if already connected. | 241 // Use the existing one if already connected. |
| 217 if (_webSocket != null) return new Future.value(); | 242 if (_webSocket != null) return new Future.value(); |
| 218 | 243 |
| 219 // Server should already be running. | 244 // Server should already be running. |
| 220 assert(_pubServer != null); | 245 expect(_pubServer, isNotNull); |
| 221 assert(_port != null); | 246 expect(_ports, isNot(isEmpty)); |
| 222 | 247 |
| 223 return WebSocket.connect("ws://127.0.0.1:$_port").then((socket) { | 248 // TODO(nweiz): once we have a separate port for a web interface into the |
| 249 // server, use that port for the websocket interface. |
| 250 var port = _ports.values.first; |
| 251 return WebSocket.connect("ws://127.0.0.1:$port").then((socket) { |
| 224 _webSocket = socket; | 252 _webSocket = socket; |
| 225 // TODO(rnystrom): Works around #13913. | 253 // TODO(rnystrom): Works around #13913. |
| 226 _webSocketBroadcastStream = _webSocket.asBroadcastStream(); | 254 _webSocketBroadcastStream = _webSocket.asBroadcastStream(); |
| 227 }); | 255 }); |
| 228 } | 256 } |
| 229 | 257 |
| 230 /// Sends [request] (an arbitrary JSON object) to the running pub serve's web | 258 /// Sends [request] (an arbitrary JSON object) to the running pub serve's web |
| 231 /// socket connection, waits for a reply, then verifies that the reply matches | 259 /// socket connection, waits for a reply, then verifies that the reply matches |
| 232 /// [expectation]. | 260 /// [expectation]. |
| 233 /// | 261 /// |
| 234 /// If [encodeRequest] is `false`, then [request] will be sent as-is over the | 262 /// If [encodeRequest] is `false`, then [request] will be sent as-is over the |
| 235 /// socket. It omitted, request is JSON encoded to a string first. | 263 /// socket. It omitted, request is JSON encoded to a string first. |
| 236 void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) { | 264 void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) { |
| 237 schedule(() => _ensureWebSocket().then((_) { | 265 schedule(() => _ensureWebSocket().then((_) { |
| 238 if (encodeRequest) request = JSON.encode(request); | 266 if (encodeRequest) request = JSON.encode(request); |
| 239 _webSocket.add(request); | 267 _webSocket.add(request); |
| 240 return _webSocketBroadcastStream.first.then((value) { | 268 return _webSocketBroadcastStream.first.then((value) { |
| 241 expect(JSON.decode(value), expectation); | 269 expect(JSON.decode(value), expectation); |
| 242 }); | 270 }); |
| 243 }), "send $request to web socket and expect reply that $expectation"); | 271 }), "send $request to web socket and expect reply that $expectation"); |
| 272 } |
| 273 |
| 274 /// Returns the URL for the server serving from [root]. |
| 275 String _serverUrl([String root]) { |
| 276 if (root == null) root = 'web'; |
| 277 expect(_ports, contains(root)); |
| 278 return "http://127.0.0.1:${_ports[root]}"; |
| 244 } | 279 } |
| OLD | NEW |