| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library utils; |
| 6 |
| 7 import 'dart:io'; |
| 8 import 'dart:json'; |
| 9 import 'dart:uri'; |
| 10 |
| 11 import '../../unittest/lib/unittest.dart'; |
| 12 import '../lib/http.dart' as http; |
| 13 import '../lib/src/utils.dart'; |
| 14 |
| 15 /// The current server instance. |
| 16 HttpServer _server; |
| 17 |
| 18 /// The URL for the current server instance. |
| 19 Uri get serverUrl => new Uri.fromString('http://localhost:${_server.port}'); |
| 20 |
| 21 /// A dummy URL for constructing requests that won't be sent. |
| 22 Uri get dummyUrl => new Uri.fromString('http://dartlang.org/'); |
| 23 |
| 24 /// Starts a new HTTP server. |
| 25 void startServer() { |
| 26 _server = new HttpServer(); |
| 27 |
| 28 _server.addRequestHandler((request) => request.path == '/error', |
| 29 (request, response) { |
| 30 response.statusCode = 400; |
| 31 response.outputStream.close(); |
| 32 }); |
| 33 |
| 34 _server.defaultRequestHandler = (request, response) { |
| 35 consumeInputStream(request.inputStream).then((requestBodyBytes) { |
| 36 response.statusCode = 200; |
| 37 response.headers.contentType = new ContentType("application", "json"); |
| 38 |
| 39 var requestBody; |
| 40 if (requestBodyBytes.isEmpty) { |
| 41 requestBody = null; |
| 42 } else if (request.headers.contentType.charset != null) { |
| 43 var encoding = requiredEncodingForCharset( |
| 44 request.headers.contentType.charset); |
| 45 requestBody = decodeString(requestBodyBytes, encoding); |
| 46 } else { |
| 47 requestBody = requestBodyBytes; |
| 48 } |
| 49 |
| 50 var content = { |
| 51 'method': request.method, |
| 52 'path': request.path, |
| 53 'headers': <String>{} |
| 54 }; |
| 55 if (requestBody != null) content['body'] = requestBody; |
| 56 request.headers.forEach((name, values) { |
| 57 // These headers are automatically generated by dart:io, so we don't |
| 58 // want to test them here. |
| 59 if (name == 'cookie' || name == 'host') return; |
| 60 |
| 61 content['headers'][name] = values; |
| 62 }); |
| 63 |
| 64 var outputEncoding; |
| 65 var encodingName = request.queryParameters['response-encoding']; |
| 66 if (encodingName != null) { |
| 67 outputEncoding = requiredEncodingForCharset(encodingName); |
| 68 } else { |
| 69 outputEncoding = Encoding.ASCII; |
| 70 } |
| 71 |
| 72 var body = JSON.stringify(content); |
| 73 response.contentLength = body.length; |
| 74 response.outputStream.writeString(body, outputEncoding); |
| 75 response.outputStream.close(); |
| 76 }); |
| 77 }; |
| 78 |
| 79 _server.listen("127.0.0.1", 0); |
| 80 } |
| 81 |
| 82 /// Stops the current HTTP server. |
| 83 void stopServer() { |
| 84 _server.close(); |
| 85 _server = null; |
| 86 } |
| 87 |
| 88 /// A matcher that matches JSON that parses to a value that matches the inner |
| 89 /// matcher. |
| 90 Matcher parse(matcher) => new _Parse(matcher); |
| 91 |
| 92 class _Parse extends BaseMatcher { |
| 93 final Matcher _matcher; |
| 94 |
| 95 _Parse(this._matcher); |
| 96 |
| 97 bool matches(item, MatchState matchState) { |
| 98 if (item is! String) return false; |
| 99 |
| 100 var parsed; |
| 101 try { |
| 102 parsed = JSON.parse(item); |
| 103 } catch (e) { |
| 104 return false; |
| 105 } |
| 106 |
| 107 return _matcher.matches(parsed, matchState); |
| 108 } |
| 109 |
| 110 Description describe(Description description) { |
| 111 return description.add('parses to a value that ') |
| 112 .addDescriptionOf(_matcher); |
| 113 } |
| 114 } |
| 115 |
| 116 // TODO(nweiz): remove this once it's built in to unittest |
| 117 /// A matcher for StateErrors. |
| 118 const isStateError = const _StateError(); |
| 119 |
| 120 /// A matcher for functions that throw StateError. |
| 121 const Matcher throwsStateError = |
| 122 const Throws(isStateError); |
| 123 |
| 124 class _StateError extends TypeMatcher { |
| 125 const _StateError() : super("StateError"); |
| 126 bool matches(item, MatchState matchState) => item is StateError; |
| 127 } |
| 128 |
| 129 /// A matcher for HttpExceptions. |
| 130 const isHttpException = const _HttpException(); |
| 131 |
| 132 /// A matcher for functions that throw HttpException. |
| 133 const Matcher throwsHttpException = |
| 134 const Throws(isHttpException); |
| 135 |
| 136 class _HttpException extends TypeMatcher { |
| 137 const _HttpException() : super("HttpException"); |
| 138 bool matches(item, MatchState matchState) => item is HttpException; |
| 139 } |
| OLD | NEW |