Index: pkg/http/test/utils.dart |
diff --git a/pkg/http/test/utils.dart b/pkg/http/test/utils.dart |
index 10067d2c5f7b47684aca1c18938eba22bad4dce3..606bb77cae4e4f0a3e03ccf927c0dca82b9c9b09 100644 |
--- a/pkg/http/test/utils.dart |
+++ b/pkg/http/test/utils.dart |
@@ -10,6 +10,7 @@ import 'dart:json' as json; |
import 'dart:uri'; |
import 'package:unittest/unittest.dart'; |
+import 'package:http/src/byte_stream.dart'; |
import 'package:http/http.dart' as http; |
import 'package:http/src/utils.dart'; |
@@ -23,81 +24,83 @@ Uri get serverUrl => Uri.parse('http://localhost:${_server.port}'); |
Uri get dummyUrl => Uri.parse('http://dartlang.org/'); |
/// Starts a new HTTP server. |
-void startServer() { |
- _server = new HttpServer(); |
- |
- _server.addRequestHandler((request) => request.path == '/error', |
- (request, response) { |
- response.statusCode = 400; |
- response.contentLength = 0; |
- response.outputStream.close(); |
- }); |
+Future startServer() { |
+ return HttpServer.bind("127.0.0.1", 0).then((s) { |
+ _server = s; |
+ s.listen((request) { |
+ var path = request.uri.path; |
+ var response = request.response; |
+ |
+ if (path == '/error') { |
+ response.statusCode = 400; |
+ response.contentLength = 0; |
+ response.close(); |
+ return; |
+ } |
- _server.addRequestHandler((request) => request.path == '/loop', |
- (request, response) { |
- var n = int.parse(Uri.parse(request.uri).query); |
- response.statusCode = 302; |
- response.headers.set('location', |
- serverUrl.resolve('/loop?${n + 1}').toString()); |
- response.contentLength = 0; |
- response.outputStream.close(); |
- }); |
+ if (path == '/loop') { |
+ var n = int.parse(request.uri.query); |
+ response.statusCode = 302; |
+ response.headers.set('location', |
+ serverUrl.resolve('/loop?${n + 1}').toString()); |
+ response.contentLength = 0; |
+ response.close(); |
+ return; |
+ } |
- _server.addRequestHandler((request) => request.path == '/redirect', |
- (request, response) { |
- response.statusCode = 302; |
- response.headers.set('location', serverUrl.resolve('/').toString()); |
- response.contentLength = 0; |
- response.outputStream.close(); |
- }); |
+ if (path == '/redirect') { |
+ response.statusCode = 302; |
+ response.headers.set('location', serverUrl.resolve('/').toString()); |
+ response.contentLength = 0; |
+ response.close(); |
+ return; |
+ } |
- _server.defaultRequestHandler = (request, response) { |
- consumeInputStream(request.inputStream).then((requestBodyBytes) { |
- response.statusCode = 200; |
- response.headers.contentType = new ContentType("application", "json"); |
+ new ByteStream(request).toBytes().then((requestBodyBytes) { |
+ response.statusCode = 200; |
+ response.headers.contentType = new ContentType("application", "json"); |
response.headers.set('single', 'value'); |
- var requestBody; |
- if (requestBodyBytes.isEmpty) { |
- requestBody = null; |
- } else if (request.headers.contentType.charset != null) { |
- var encoding = requiredEncodingForCharset( |
- request.headers.contentType.charset); |
- requestBody = decodeString(requestBodyBytes, encoding); |
- } else { |
- requestBody = requestBodyBytes; |
- } |
- |
- var content = { |
- 'method': request.method, |
- 'path': request.path, |
- 'headers': {} |
- }; |
- if (requestBody != null) content['body'] = requestBody; |
- request.headers.forEach((name, values) { |
- // These headers are automatically generated by dart:io, so we don't |
- // want to test them here. |
- if (name == 'cookie' || name == 'host') return; |
- |
- content['headers'][name] = values; |
+ var requestBody; |
+ if (requestBodyBytes.isEmpty) { |
+ requestBody = null; |
+ } else if (request.headers.contentType.charset != null) { |
+ var encoding = requiredEncodingForCharset( |
+ request.headers.contentType.charset); |
+ requestBody = decodeString(requestBodyBytes, encoding); |
+ } else { |
+ requestBody = requestBodyBytes; |
+ } |
+ |
+ var content = { |
+ 'method': request.method, |
+ 'path': request.uri.path, |
+ 'headers': {} |
+ }; |
+ if (requestBody != null) content['body'] = requestBody; |
+ request.headers.forEach((name, values) { |
+ // These headers are automatically generated by dart:io, so we don't |
+ // want to test them here. |
+ if (name == 'cookie' || name == 'host') return; |
+ |
+ content['headers'][name] = values; |
+ }); |
+ |
+ var outputEncoding; |
+ var encodingName = request.queryParameters['response-encoding']; |
+ if (encodingName != null) { |
+ outputEncoding = requiredEncodingForCharset(encodingName); |
+ } else { |
+ outputEncoding = Encoding.ASCII; |
+ } |
+ |
+ var body = json.stringify(content); |
+ response.contentLength = body.length; |
+ response.addString(body, outputEncoding); |
+ response.close(); |
}); |
- |
- var outputEncoding; |
- var encodingName = request.queryParameters['response-encoding']; |
- if (encodingName != null) { |
- outputEncoding = requiredEncodingForCharset(encodingName); |
- } else { |
- outputEncoding = Encoding.ASCII; |
- } |
- |
- var body = json.stringify(content); |
- response.contentLength = body.length; |
- response.outputStream.writeString(body, outputEncoding); |
- response.outputStream.close(); |
}); |
- }; |
- |
- _server.listen("127.0.0.1", 0); |
+ }); |
} |
/// Stops the current HTTP server. |
@@ -106,11 +109,6 @@ void stopServer() { |
_server = null; |
} |
-// TODO(nweiz): remove this once issue 7785 is fixed. |
-/// Buffers all input from an InputStream and returns it as a future. |
-Future<List<int>> consumeInputStream(InputStream stream) => |
- new http.ByteStream(wrapInputStream(stream)).toBytes(); |
- |
/// A matcher that matches JSON that parses to a value that matches the inner |
/// matcher. |
Matcher parse(matcher) => new _Parse(matcher); |