| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
| 10 import 'dart:uri'; | 10 import 'dart:uri'; |
| 11 | 11 |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 import 'package:http/src/byte_stream.dart'; |
| 13 import 'package:http/http.dart' as http; | 14 import 'package:http/http.dart' as http; |
| 14 import 'package:http/src/utils.dart'; | 15 import 'package:http/src/utils.dart'; |
| 15 | 16 |
| 16 /// The current server instance. | 17 /// The current server instance. |
| 17 HttpServer _server; | 18 HttpServer _server; |
| 18 | 19 |
| 19 /// The URL for the current server instance. | 20 /// The URL for the current server instance. |
| 20 Uri get serverUrl => Uri.parse('http://localhost:${_server.port}'); | 21 Uri get serverUrl => Uri.parse('http://localhost:${_server.port}'); |
| 21 | 22 |
| 22 /// A dummy URL for constructing requests that won't be sent. | 23 /// A dummy URL for constructing requests that won't be sent. |
| 23 Uri get dummyUrl => Uri.parse('http://dartlang.org/'); | 24 Uri get dummyUrl => Uri.parse('http://dartlang.org/'); |
| 24 | 25 |
| 25 /// Starts a new HTTP server. | 26 /// Starts a new HTTP server. |
| 26 void startServer() { | 27 Future startServer() { |
| 27 _server = new HttpServer(); | 28 return HttpServer.bind("127.0.0.1", 0).then((s) { |
| 29 _server = s; |
| 30 s.listen((request) { |
| 31 var path = request.uri.path; |
| 32 var response = request.response; |
| 28 | 33 |
| 29 _server.addRequestHandler((request) => request.path == '/error', | 34 if (path == '/error') { |
| 30 (request, response) { | 35 response.statusCode = 400; |
| 31 response.statusCode = 400; | 36 response.contentLength = 0; |
| 32 response.contentLength = 0; | 37 response.close(); |
| 33 response.outputStream.close(); | 38 return; |
| 34 }); | 39 } |
| 35 | 40 |
| 36 _server.addRequestHandler((request) => request.path == '/loop', | 41 if (path == '/loop') { |
| 37 (request, response) { | 42 var n = int.parse(request.uri.query); |
| 38 var n = int.parse(Uri.parse(request.uri).query); | 43 response.statusCode = 302; |
| 39 response.statusCode = 302; | 44 response.headers.set('location', |
| 40 response.headers.set('location', | 45 serverUrl.resolve('/loop?${n + 1}').toString()); |
| 41 serverUrl.resolve('/loop?${n + 1}').toString()); | 46 response.contentLength = 0; |
| 42 response.contentLength = 0; | 47 response.close(); |
| 43 response.outputStream.close(); | 48 return; |
| 44 }); | 49 } |
| 45 | 50 |
| 46 _server.addRequestHandler((request) => request.path == '/redirect', | 51 if (path == '/redirect') { |
| 47 (request, response) { | 52 response.statusCode = 302; |
| 48 response.statusCode = 302; | 53 response.headers.set('location', serverUrl.resolve('/').toString()); |
| 49 response.headers.set('location', serverUrl.resolve('/').toString()); | 54 response.contentLength = 0; |
| 50 response.contentLength = 0; | 55 response.close(); |
| 51 response.outputStream.close(); | 56 return; |
| 52 }); | 57 } |
| 53 | 58 |
| 54 _server.defaultRequestHandler = (request, response) { | 59 new ByteStream(request).toBytes().then((requestBodyBytes) { |
| 55 consumeInputStream(request.inputStream).then((requestBodyBytes) { | 60 response.statusCode = 200; |
| 56 response.statusCode = 200; | 61 response.headers.contentType = new ContentType("application", "json"); |
| 57 response.headers.contentType = new ContentType("application", "json"); | |
| 58 response.headers.set('single', 'value'); | 62 response.headers.set('single', 'value'); |
| 59 | 63 |
| 60 var requestBody; | 64 var requestBody; |
| 61 if (requestBodyBytes.isEmpty) { | 65 if (requestBodyBytes.isEmpty) { |
| 62 requestBody = null; | 66 requestBody = null; |
| 63 } else if (request.headers.contentType.charset != null) { | 67 } else if (request.headers.contentType.charset != null) { |
| 64 var encoding = requiredEncodingForCharset( | 68 var encoding = requiredEncodingForCharset( |
| 65 request.headers.contentType.charset); | 69 request.headers.contentType.charset); |
| 66 requestBody = decodeString(requestBodyBytes, encoding); | 70 requestBody = decodeString(requestBodyBytes, encoding); |
| 67 } else { | 71 } else { |
| 68 requestBody = requestBodyBytes; | 72 requestBody = requestBodyBytes; |
| 69 } | 73 } |
| 70 | 74 |
| 71 var content = { | 75 var content = { |
| 72 'method': request.method, | 76 'method': request.method, |
| 73 'path': request.path, | 77 'path': request.uri.path, |
| 74 'headers': {} | 78 'headers': {} |
| 75 }; | 79 }; |
| 76 if (requestBody != null) content['body'] = requestBody; | 80 if (requestBody != null) content['body'] = requestBody; |
| 77 request.headers.forEach((name, values) { | 81 request.headers.forEach((name, values) { |
| 78 // These headers are automatically generated by dart:io, so we don't | 82 // These headers are automatically generated by dart:io, so we don't |
| 79 // want to test them here. | 83 // want to test them here. |
| 80 if (name == 'cookie' || name == 'host') return; | 84 if (name == 'cookie' || name == 'host') return; |
| 81 | 85 |
| 82 content['headers'][name] = values; | 86 content['headers'][name] = values; |
| 87 }); |
| 88 |
| 89 var outputEncoding; |
| 90 var encodingName = request.queryParameters['response-encoding']; |
| 91 if (encodingName != null) { |
| 92 outputEncoding = requiredEncodingForCharset(encodingName); |
| 93 } else { |
| 94 outputEncoding = Encoding.ASCII; |
| 95 } |
| 96 |
| 97 var body = json.stringify(content); |
| 98 response.contentLength = body.length; |
| 99 response.addString(body, outputEncoding); |
| 100 response.close(); |
| 83 }); | 101 }); |
| 84 | |
| 85 var outputEncoding; | |
| 86 var encodingName = request.queryParameters['response-encoding']; | |
| 87 if (encodingName != null) { | |
| 88 outputEncoding = requiredEncodingForCharset(encodingName); | |
| 89 } else { | |
| 90 outputEncoding = Encoding.ASCII; | |
| 91 } | |
| 92 | |
| 93 var body = json.stringify(content); | |
| 94 response.contentLength = body.length; | |
| 95 response.outputStream.writeString(body, outputEncoding); | |
| 96 response.outputStream.close(); | |
| 97 }); | 102 }); |
| 98 }; | 103 }); |
| 99 | |
| 100 _server.listen("127.0.0.1", 0); | |
| 101 } | 104 } |
| 102 | 105 |
| 103 /// Stops the current HTTP server. | 106 /// Stops the current HTTP server. |
| 104 void stopServer() { | 107 void stopServer() { |
| 105 _server.close(); | 108 _server.close(); |
| 106 _server = null; | 109 _server = null; |
| 107 } | 110 } |
| 108 | 111 |
| 109 // TODO(nweiz): remove this once issue 7785 is fixed. | |
| 110 /// Buffers all input from an InputStream and returns it as a future. | |
| 111 Future<List<int>> consumeInputStream(InputStream stream) => | |
| 112 new http.ByteStream(wrapInputStream(stream)).toBytes(); | |
| 113 | |
| 114 /// A matcher that matches JSON that parses to a value that matches the inner | 112 /// A matcher that matches JSON that parses to a value that matches the inner |
| 115 /// matcher. | 113 /// matcher. |
| 116 Matcher parse(matcher) => new _Parse(matcher); | 114 Matcher parse(matcher) => new _Parse(matcher); |
| 117 | 115 |
| 118 class _Parse extends BaseMatcher { | 116 class _Parse extends BaseMatcher { |
| 119 final Matcher _matcher; | 117 final Matcher _matcher; |
| 120 | 118 |
| 121 _Parse(this._matcher); | 119 _Parse(this._matcher); |
| 122 | 120 |
| 123 bool matches(item, MatchState matchState) { | 121 bool matches(item, MatchState matchState) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 const isSocketIOException = const _SocketIOException(); | 169 const isSocketIOException = const _SocketIOException(); |
| 172 | 170 |
| 173 /// A matcher for functions that throw SocketIOException. | 171 /// A matcher for functions that throw SocketIOException. |
| 174 const Matcher throwsSocketIOException = | 172 const Matcher throwsSocketIOException = |
| 175 const Throws(isSocketIOException); | 173 const Throws(isSocketIOException); |
| 176 | 174 |
| 177 class _SocketIOException extends TypeMatcher { | 175 class _SocketIOException extends TypeMatcher { |
| 178 const _SocketIOException() : super("SocketIOException"); | 176 const _SocketIOException() : super("SocketIOException"); |
| 179 bool matches(item, MatchState matchState) => item is SocketIOException; | 177 bool matches(item, MatchState matchState) => item is SocketIOException; |
| 180 } | 178 } |
| OLD | NEW |