Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Unified Diff: pkg/http/test/utils.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r18818 Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/http/test/request_test.dart ('k') | pkg/intl/tool/generate_locale_data_files.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« no previous file with comments | « pkg/http/test/request_test.dart ('k') | pkg/intl/tool/generate_locale_data_files.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698