Chromium Code Reviews| 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/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 _server.defaultRequestHandler = (request, response) { | |
| 28 consumeInputStream(request.inputStream).then((requestBodyBytes) { | |
| 29 response.statusCode = 200; | |
| 30 response.headers.contentType = new ContentType("application", "json"); | |
| 31 | |
| 32 var requestBody; | |
| 33 if (requestBodyBytes.isEmpty) { | |
| 34 requestBody = null; | |
| 35 } else if (request.headers.contentType.charset != null) { | |
| 36 var encoding = requiredEncodingForCharset( | |
| 37 request.headers.contentType.charset); | |
| 38 requestBody = decodeString(requestBodyBytes, encoding); | |
| 39 } else { | |
| 40 requestBody = requestBodyBytes; | |
| 41 } | |
| 42 | |
| 43 var content = { | |
| 44 'method': request.method, | |
| 45 'path': request.path, | |
| 46 'headers': <String>{} | |
| 47 }; | |
| 48 if (requestBody != null) content['body'] = requestBody; | |
| 49 request.headers.forEach((name, values) { | |
| 50 // These headers are automatically generated by dart:io, so we don't | |
| 51 // want to test them here. | |
| 52 if (name == 'cookie' || name == 'host') return; | |
| 53 | |
| 54 content['headers'][name] = values; | |
| 55 }); | |
| 56 | |
| 57 var outputEncoding; | |
| 58 var encodingName = request.queryParameters['response-encoding']; | |
| 59 if (encodingName != null) { | |
| 60 outputEncoding = requiredEncodingForCharset(encodingName); | |
| 61 } else { | |
| 62 outputEncoding = Encoding.ASCII; | |
| 63 } | |
| 64 | |
| 65 var body = JSON.stringify(content); | |
| 66 response.contentLength = body.length; | |
| 67 response.outputStream.writeString(body, outputEncoding); | |
| 68 response.outputStream.close(); | |
| 69 }); | |
| 70 }; | |
| 71 _server.listen("127.0.0.1", 0); | |
| 72 } | |
| 73 | |
| 74 /// Stops the current HTTP server. | |
| 75 void stopServer() { | |
| 76 _server.close(); | |
| 77 _server = null; | |
| 78 } | |
| 79 | |
| 80 /// A matcher that matches JSON that parses to a value that matches the inner | |
| 81 /// matcher. | |
| 82 Matcher parse(matcher) => new _Parse(matcher); | |
| 83 | |
| 84 class _Parse extends BaseMatcher { | |
| 85 final Matcher _matcher; | |
| 86 | |
| 87 _Parse(this._matcher); | |
| 88 | |
| 89 bool matches(item, MatchState matchState) { | |
| 90 if (item is! String) return false; | |
| 91 | |
| 92 var parsed; | |
| 93 try { | |
| 94 parsed = JSON.parse(item); | |
| 95 } catch (e) { | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 return _matcher.matches(parsed, matchState); | |
| 100 } | |
| 101 | |
| 102 Description describe(Description description) { | |
| 103 return description.add('parses to a value that ') | |
| 104 .addDescriptionOf(_matcher); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 /// A matcher for StateErrors. | |
| 109 const isStateError = const _StateError(); | |
|
Bob Nystrom
2012/10/31 01:17:44
These should go away once unittest is updated to t
nweiz
2012/10/31 18:20:59
Done.
| |
| 110 | |
| 111 /// A matcher for functions that throw StateError. | |
| 112 const Matcher throwsStateError = | |
| 113 const Throws(isStateError); | |
| 114 | |
| 115 class _StateError extends TypeMatcher { | |
| 116 const _StateError() : super("StateError"); | |
| 117 bool matches(item, MatchState matchState) => item is StateError; | |
| 118 } | |
| OLD | NEW |