| 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 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if (match == null) return false; | 197 if (match == null) return false; |
| 198 _ports[match[1]] = int.parse(match[2]); | 198 _ports[match[1]] = int.parse(match[2]); |
| 199 return true; | 199 return true; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void endPubServe() { | 202 void endPubServe() { |
| 203 _pubServer.kill(); | 203 _pubServer.kill(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 /// Schedules an HTTP request to the running pub server with [urlPath] and | 206 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 207 /// invokes [callback] with the response. |
| 208 /// |
| 209 /// [root] indicates which server should be accessed, and defaults to "web". |
| 210 Future<http.Response> scheduleRequest(String urlPath, {String root}) { |
| 211 return schedule(() { |
| 212 return http.get(_getServerUrlSync(root, urlPath)); |
| 213 }, "request $urlPath"); |
| 214 } |
| 215 |
| 216 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 207 /// verifies that it responds with a body that matches [expectation]. | 217 /// verifies that it responds with a body that matches [expectation]. |
| 208 /// | 218 /// |
| 209 /// [expectation] may either be a [Matcher] or a string to match an exact body. | 219 /// [expectation] may either be a [Matcher] or a string to match an exact body. |
| 210 /// [root] indicates which server should be accessed, and defaults to "web". | 220 /// [root] indicates which server should be accessed, and defaults to "web". |
| 211 /// [headers] may be either a [Matcher] or a map to match an exact headers map. | 221 /// [headers] may be either a [Matcher] or a map to match an exact headers map. |
| 212 void requestShouldSucceed(String urlPath, expectation, {String root, headers}) { | 222 void requestShouldSucceed(String urlPath, expectation, {String root, headers}) { |
| 213 schedule(() { | 223 scheduleRequest(urlPath, root: root).then((response) { |
| 214 return http.get(_getServerUrlSync(root, urlPath)).then((response) { | 224 if (expectation != null) expect(response.body, expectation); |
| 215 if (expectation != null) expect(response.body, expectation); | 225 if (headers != null) expect(response.headers, headers); |
| 216 if (headers != null) expect(response.headers, headers); | 226 }); |
| 217 }); | |
| 218 }, "request $urlPath"); | |
| 219 } | 227 } |
| 220 | 228 |
| 221 /// Schedules an HTTP request to the running pub server with [urlPath] and | 229 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 222 /// verifies that it responds with a 404. | 230 /// verifies that it responds with a 404. |
| 223 /// | 231 /// |
| 224 /// [root] indicates which server should be accessed, and defaults to "web". | 232 /// [root] indicates which server should be accessed, and defaults to "web". |
| 225 void requestShould404(String urlPath, {String root}) { | 233 void requestShould404(String urlPath, {String root}) { |
| 226 schedule(() { | 234 scheduleRequest(urlPath, root: root).then((response) { |
| 227 return http.get(_getServerUrlSync(root, urlPath)).then((response) { | 235 expect(response.statusCode, equals(404)); |
| 228 expect(response.statusCode, equals(404)); | 236 }); |
| 229 }); | |
| 230 }, "request $urlPath"); | |
| 231 } | 237 } |
| 232 | 238 |
| 233 /// Schedules an HTTP request to the running pub server with [urlPath] and | 239 /// Schedules an HTTP request to the running pub server with [urlPath] and |
| 234 /// verifies that it responds with a redirect to the given [redirectTarget]. | 240 /// verifies that it responds with a redirect to the given [redirectTarget]. |
| 235 /// | 241 /// |
| 236 /// [redirectTarget] may be either a [Matcher] or a string to match an exact | 242 /// [redirectTarget] may be either a [Matcher] or a string to match an exact |
| 237 /// URL. [root] indicates which server should be accessed, and defaults to | 243 /// URL. [root] indicates which server should be accessed, and defaults to |
| 238 /// "web". | 244 /// "web". |
| 239 void requestShouldRedirect(String urlPath, redirectTarget, {String root}) { | 245 void requestShouldRedirect(String urlPath, redirectTarget, {String root}) { |
| 240 schedule(() { | 246 schedule(() { |
| 241 var request = new http.Request("GET", | 247 var request = new http.Request("GET", |
| 242 Uri.parse(_getServerUrlSync(root, urlPath))); | 248 Uri.parse(_getServerUrlSync(root, urlPath))); |
| 243 request.followRedirects = false; | 249 request.followRedirects = false; |
| 244 return request.send().then((response) { | 250 return request.send().then((response) { |
| 245 expect(response.statusCode ~/ 100, equals(3)); | 251 expect(response.statusCode ~/ 100, equals(3)); |
| 246 | |
| 247 expect(response.headers, containsPair('location', redirectTarget)); | 252 expect(response.headers, containsPair('location', redirectTarget)); |
| 248 }); | 253 }); |
| 249 }, "request $urlPath"); | 254 }, "request $urlPath"); |
| 250 } | 255 } |
| 251 | 256 |
| 252 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies | 257 /// Schedules an HTTP POST to the running pub server with [urlPath] and verifies |
| 253 /// that it responds with a 405. | 258 /// that it responds with a 405. |
| 254 /// | 259 /// |
| 255 /// [root] indicates which server should be accessed, and defaults to "web". | 260 /// [root] indicates which server should be accessed, and defaults to "web". |
| 256 void postShould405(String urlPath, {String root}) { | 261 void postShould405(String urlPath, {String root}) { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 /// included. Unlike [getServerUrl], this should only be called after the ports | 419 /// included. Unlike [getServerUrl], this should only be called after the ports |
| 415 /// are known. | 420 /// are known. |
| 416 String _getServerUrlSync([String root, String path]) { | 421 String _getServerUrlSync([String root, String path]) { |
| 417 if (root == null) root = 'web'; | 422 if (root == null) root = 'web'; |
| 418 expect(_ports, contains(root)); | 423 expect(_ports, contains(root)); |
| 419 var url = "http://127.0.0.1:${_ports[root]}"; | 424 var url = "http://127.0.0.1:${_ports[root]}"; |
| 420 if (path != null) url = "$url/$path"; | 425 if (path != null) url = "$url/$path"; |
| 421 return url; | 426 return url; |
| 422 } | 427 } |
| 423 | 428 |
| OLD | NEW |