| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 test_utils; | 5 library http.test.io_utils; |
| 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'; | 11 import 'package:http/http.dart'; |
| 12 import 'package:http/src/utils.dart'; | 12 import 'package:http/src/utils.dart'; |
| 13 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 14 | 14 |
| 15 export '../utils.dart'; |
| 16 |
| 15 /// The current server instance. | 17 /// The current server instance. |
| 16 HttpServer _server; | 18 HttpServer _server; |
| 17 | 19 |
| 18 /// The URL for the current server instance. | 20 /// The URL for the current server instance. |
| 19 Uri get serverUrl => Uri.parse('http://localhost:${_server.port}'); | 21 Uri get serverUrl => Uri.parse('http://localhost:${_server.port}'); |
| 20 | 22 |
| 21 /// A dummy URL for constructing requests that won't be sent. | |
| 22 Uri get dummyUrl => Uri.parse('http://dartlang.org/'); | |
| 23 | |
| 24 /// Starts a new HTTP server. | 23 /// Starts a new HTTP server. |
| 25 Future startServer() { | 24 Future startServer() { |
| 26 return HttpServer.bind("localhost", 0).then((s) { | 25 return HttpServer.bind("localhost", 0).then((s) { |
| 27 _server = s; | 26 _server = s; |
| 28 s.listen((request) { | 27 s.listen((request) { |
| 29 var path = request.uri.path; | 28 var path = request.uri.path; |
| 30 var response = request.response; | 29 var response = request.response; |
| 31 | 30 |
| 32 if (path == '/error') { | 31 if (path == '/error') { |
| 33 response.statusCode = 400; | 32 response.statusCode = 400; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 } | 111 } |
| 113 | 112 |
| 114 /// Stops the current HTTP server. | 113 /// Stops the current HTTP server. |
| 115 void stopServer() { | 114 void stopServer() { |
| 116 if (_server != null) { | 115 if (_server != null) { |
| 117 _server.close(); | 116 _server.close(); |
| 118 _server = null; | 117 _server = null; |
| 119 } | 118 } |
| 120 } | 119 } |
| 121 | 120 |
| 122 /// Removes eight spaces of leading indentation from a multiline string. | |
| 123 /// | |
| 124 /// Note that this is very sensitive to how the literals are styled. They should | |
| 125 /// be: | |
| 126 /// ''' | |
| 127 /// Text starts on own line. Lines up with subsequent lines. | |
| 128 /// Lines are indented exactly 8 characters from the left margin. | |
| 129 /// Close is on the same line.''' | |
| 130 /// | |
| 131 /// This does nothing if text is only a single line. | |
| 132 // TODO(nweiz): Make this auto-detect the indentation level from the first | |
| 133 // non-whitespace line. | |
| 134 String cleanUpLiteral(String text) { | |
| 135 var lines = text.split('\n'); | |
| 136 if (lines.length <= 1) return text; | |
| 137 | |
| 138 for (var j = 0; j < lines.length; j++) { | |
| 139 if (lines[j].length > 8) { | |
| 140 lines[j] = lines[j].substring(8, lines[j].length); | |
| 141 } else { | |
| 142 lines[j] = ''; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 return lines.join('\n'); | |
| 147 } | |
| 148 | |
| 149 /// A matcher that matches JSON that parses to a value that matches the inner | |
| 150 /// matcher. | |
| 151 Matcher parse(matcher) => new _Parse(matcher); | |
| 152 | |
| 153 class _Parse extends Matcher { | |
| 154 final Matcher _matcher; | |
| 155 | |
| 156 _Parse(this._matcher); | |
| 157 | |
| 158 bool matches(item, Map matchState) { | |
| 159 if (item is! String) return false; | |
| 160 | |
| 161 var parsed; | |
| 162 try { | |
| 163 parsed = JSON.decode(item); | |
| 164 } catch (e) { | |
| 165 return false; | |
| 166 } | |
| 167 | |
| 168 return _matcher.matches(parsed, matchState); | |
| 169 } | |
| 170 | |
| 171 Description describe(Description description) { | |
| 172 return description.add('parses to a value that ') | |
| 173 .addDescriptionOf(_matcher); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 /// A matcher for functions that throw HttpException. | 121 /// A matcher for functions that throw HttpException. |
| 178 Matcher get throwsClientException => | 122 Matcher get throwsClientException => |
| 179 throwsA(new isInstanceOf<ClientException>()); | 123 throwsA(new isInstanceOf<ClientException>()); |
| 180 | 124 |
| 181 /// A matcher for RedirectLimitExceededExceptions. | 125 /// A matcher for RedirectLimitExceededExceptions. |
| 182 const isRedirectLimitExceededException = | 126 const isRedirectLimitExceededException = |
| 183 const _RedirectLimitExceededException(); | 127 const _RedirectLimitExceededException(); |
| 184 | 128 |
| 185 /// A matcher for functions that throw RedirectLimitExceededException. | 129 /// A matcher for functions that throw RedirectLimitExceededException. |
| 186 const Matcher throwsRedirectLimitExceededException = | 130 const Matcher throwsRedirectLimitExceededException = |
| (...skipping 11 matching lines...) Expand all Loading... |
| 198 const isSocketException = const _SocketException(); | 142 const isSocketException = const _SocketException(); |
| 199 | 143 |
| 200 /// A matcher for functions that throw SocketException. | 144 /// A matcher for functions that throw SocketException. |
| 201 const Matcher throwsSocketException = | 145 const Matcher throwsSocketException = |
| 202 const Throws(isSocketException); | 146 const Throws(isSocketException); |
| 203 | 147 |
| 204 class _SocketException extends TypeMatcher { | 148 class _SocketException extends TypeMatcher { |
| 205 const _SocketException() : super("SocketException"); | 149 const _SocketException() : super("SocketException"); |
| 206 bool matches(item, Map matchState) => item is SocketException; | 150 bool matches(item, Map matchState) => item is SocketException; |
| 207 } | 151 } |
| OLD | NEW |