| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 test_utils; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 import 'dart:json'; | 8 import 'dart:json' as json; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 | 10 |
| 11 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 12 import 'package:http/http.dart' as http; | 12 import 'package:http/http.dart' as http; |
| 13 import 'package:http/src/utils.dart'; | 13 import 'package:http/src/utils.dart'; |
| 14 | 14 |
| 15 /// The current server instance. | 15 /// The current server instance. |
| 16 HttpServer _server; | 16 HttpServer _server; |
| 17 | 17 |
| 18 /// The URL for the current server instance. | 18 /// The URL for the current server instance. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 }); | 81 }); |
| 82 | 82 |
| 83 var outputEncoding; | 83 var outputEncoding; |
| 84 var encodingName = request.queryParameters['response-encoding']; | 84 var encodingName = request.queryParameters['response-encoding']; |
| 85 if (encodingName != null) { | 85 if (encodingName != null) { |
| 86 outputEncoding = requiredEncodingForCharset(encodingName); | 86 outputEncoding = requiredEncodingForCharset(encodingName); |
| 87 } else { | 87 } else { |
| 88 outputEncoding = Encoding.ASCII; | 88 outputEncoding = Encoding.ASCII; |
| 89 } | 89 } |
| 90 | 90 |
| 91 var body = JSON.stringify(content); | 91 var body = json.stringify(content); |
| 92 response.contentLength = body.length; | 92 response.contentLength = body.length; |
| 93 response.outputStream.writeString(body, outputEncoding); | 93 response.outputStream.writeString(body, outputEncoding); |
| 94 response.outputStream.close(); | 94 response.outputStream.close(); |
| 95 }); | 95 }); |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 _server.listen("127.0.0.1", 0); | 98 _server.listen("127.0.0.1", 0); |
| 99 } | 99 } |
| 100 | 100 |
| 101 /// Stops the current HTTP server. | 101 /// Stops the current HTTP server. |
| 102 void stopServer() { | 102 void stopServer() { |
| 103 _server.close(); | 103 _server.close(); |
| 104 _server = null; | 104 _server = null; |
| 105 } | 105 } |
| 106 | 106 |
| 107 /// A matcher that matches JSON that parses to a value that matches the inner | 107 /// A matcher that matches JSON that parses to a value that matches the inner |
| 108 /// matcher. | 108 /// matcher. |
| 109 Matcher parse(matcher) => new _Parse(matcher); | 109 Matcher parse(matcher) => new _Parse(matcher); |
| 110 | 110 |
| 111 class _Parse extends BaseMatcher { | 111 class _Parse extends BaseMatcher { |
| 112 final Matcher _matcher; | 112 final Matcher _matcher; |
| 113 | 113 |
| 114 _Parse(this._matcher); | 114 _Parse(this._matcher); |
| 115 | 115 |
| 116 bool matches(item, MatchState matchState) { | 116 bool matches(item, MatchState matchState) { |
| 117 if (item is! String) return false; | 117 if (item is! String) return false; |
| 118 | 118 |
| 119 var parsed; | 119 var parsed; |
| 120 try { | 120 try { |
| 121 parsed = JSON.parse(item); | 121 parsed = json.parse(item); |
| 122 } catch (e) { | 122 } catch (e) { |
| 123 return false; | 123 return false; |
| 124 } | 124 } |
| 125 | 125 |
| 126 return _matcher.matches(parsed, matchState); | 126 return _matcher.matches(parsed, matchState); |
| 127 } | 127 } |
| 128 | 128 |
| 129 Description describe(Description description) { | 129 Description describe(Description description) { |
| 130 return description.add('parses to a value that ') | 130 return description.add('parses to a value that ') |
| 131 .addDescriptionOf(_matcher); | 131 .addDescriptionOf(_matcher); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 const Matcher throwsRedirectLimitExceededException = | 165 const Matcher throwsRedirectLimitExceededException = |
| 166 const Throws(isRedirectLimitExceededException); | 166 const Throws(isRedirectLimitExceededException); |
| 167 | 167 |
| 168 class _RedirectLimitExceededException extends TypeMatcher { | 168 class _RedirectLimitExceededException extends TypeMatcher { |
| 169 const _RedirectLimitExceededException() : | 169 const _RedirectLimitExceededException() : |
| 170 super("RedirectLimitExceededException"); | 170 super("RedirectLimitExceededException"); |
| 171 | 171 |
| 172 bool matches(item, MatchState matchState) => | 172 bool matches(item, MatchState matchState) => |
| 173 item is RedirectLimitExceededException; | 173 item is RedirectLimitExceededException; |
| 174 } | 174 } |
| OLD | NEW |