| 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 utils; | 5 library utils; |
| 6 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 import 'dart:json'; | 8 import 'dart:json'; |
| 9 import 'dart:uri'; | 9 import 'dart:uri'; |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 /// A dummy URL for constructing requests that won't be sent. | 21 /// A dummy URL for constructing requests that won't be sent. |
| 22 Uri get dummyUrl => new Uri.fromString('http://dartlang.org/'); | 22 Uri get dummyUrl => new Uri.fromString('http://dartlang.org/'); |
| 23 | 23 |
| 24 /// Starts a new HTTP server. | 24 /// Starts a new HTTP server. |
| 25 void startServer() { | 25 void startServer() { |
| 26 _server = new HttpServer(); | 26 _server = new HttpServer(); |
| 27 | 27 |
| 28 _server.addRequestHandler((request) => request.path == '/error', | 28 _server.addRequestHandler((request) => request.path == '/error', |
| 29 (request, response) { | 29 (request, response) { |
| 30 response.statusCode = 400; | 30 response.statusCode = 400; |
| 31 response.contentLength = 0; |
| 31 response.outputStream.close(); | 32 response.outputStream.close(); |
| 32 }); | 33 }); |
| 33 | 34 |
| 35 _server.addRequestHandler((request) => request.path == '/loop', |
| 36 (request, response) { |
| 37 var n = int.parse(new Uri.fromString(request.uri).query); |
| 38 response.statusCode = 302; |
| 39 response.headers.set('location', '/loop?${n + 1}'); |
| 40 response.contentLength = 0; |
| 41 response.outputStream.close(); |
| 42 }); |
| 43 |
| 44 _server.addRequestHandler((request) => request.path == '/redirect', |
| 45 (request, response) { |
| 46 response.statusCode = 302; |
| 47 response.headers.set('location', '/'); |
| 48 response.contentLength = 0; |
| 49 response.outputStream.close(); |
| 50 }); |
| 51 |
| 34 _server.defaultRequestHandler = (request, response) { | 52 _server.defaultRequestHandler = (request, response) { |
| 35 consumeInputStream(request.inputStream).then((requestBodyBytes) { | 53 consumeInputStream(request.inputStream).then((requestBodyBytes) { |
| 36 response.statusCode = 200; | 54 response.statusCode = 200; |
| 37 response.headers.contentType = new ContentType("application", "json"); | 55 response.headers.contentType = new ContentType("application", "json"); |
| 38 | 56 |
| 39 var requestBody; | 57 var requestBody; |
| 40 if (requestBodyBytes.isEmpty) { | 58 if (requestBodyBytes.isEmpty) { |
| 41 requestBody = null; | 59 requestBody = null; |
| 42 } else if (request.headers.contentType.charset != null) { | 60 } else if (request.headers.contentType.charset != null) { |
| 43 var encoding = requiredEncodingForCharset( | 61 var encoding = requiredEncodingForCharset( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 /// A matcher that matches JSON that parses to a value that matches the inner | 106 /// A matcher that matches JSON that parses to a value that matches the inner |
| 89 /// matcher. | 107 /// matcher. |
| 90 Matcher parse(matcher) => new _Parse(matcher); | 108 Matcher parse(matcher) => new _Parse(matcher); |
| 91 | 109 |
| 92 class _Parse extends BaseMatcher { | 110 class _Parse extends BaseMatcher { |
| 93 final Matcher _matcher; | 111 final Matcher _matcher; |
| 94 | 112 |
| 95 _Parse(this._matcher); | 113 _Parse(this._matcher); |
| 96 | 114 |
| 97 bool matches(item, MatchState matchState) { | 115 bool matches(item, MatchState matchState) { |
| 98 print("_Parse.matches [$item]"); | |
| 99 if (item is! String) return false; | 116 if (item is! String) return false; |
| 100 | 117 |
| 101 var parsed; | 118 var parsed; |
| 102 try { | 119 try { |
| 103 parsed = JSON.parse(item); | 120 parsed = JSON.parse(item); |
| 104 } catch (e) { | 121 } catch (e) { |
| 105 return false; | 122 return false; |
| 106 } | 123 } |
| 107 | 124 |
| 108 return _matcher.matches(parsed, matchState); | 125 return _matcher.matches(parsed, matchState); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 131 const isHttpException = const _HttpException(); | 148 const isHttpException = const _HttpException(); |
| 132 | 149 |
| 133 /// A matcher for functions that throw HttpException. | 150 /// A matcher for functions that throw HttpException. |
| 134 const Matcher throwsHttpException = | 151 const Matcher throwsHttpException = |
| 135 const Throws(isHttpException); | 152 const Throws(isHttpException); |
| 136 | 153 |
| 137 class _HttpException extends TypeMatcher { | 154 class _HttpException extends TypeMatcher { |
| 138 const _HttpException() : super("HttpException"); | 155 const _HttpException() : super("HttpException"); |
| 139 bool matches(item, MatchState matchState) => item is HttpException; | 156 bool matches(item, MatchState matchState) => item is HttpException; |
| 140 } | 157 } |
| 158 |
| 159 /// A matcher for RedirectLimitExceededExceptions. |
| 160 const isRedirectLimitExceededException = |
| 161 const _RedirectLimitExceededException(); |
| 162 |
| 163 /// A matcher for functions that throw RedirectLimitExceededException. |
| 164 const Matcher throwsRedirectLimitExceededException = |
| 165 const Throws(isRedirectLimitExceededException); |
| 166 |
| 167 class _RedirectLimitExceededException extends TypeMatcher { |
| 168 const _RedirectLimitExceededException() : |
| 169 super("RedirectLimitExceededException"); |
| 170 |
| 171 bool matches(item, MatchState matchState) => |
| 172 item is RedirectLimitExceededException; |
| 173 } |
| OLD | NEW |