| Index: pkg/http/test/utils.dart
|
| diff --git a/pkg/http/test/utils.dart b/pkg/http/test/utils.dart
|
| index 051cbd4f0936d56265c336d4613b63981ee6c58e..f89a0c58ca447e278b579e6a66b22021ad52ea72 100644
|
| --- a/pkg/http/test/utils.dart
|
| +++ b/pkg/http/test/utils.dart
|
| @@ -4,121 +4,15 @@
|
|
|
| library test_utils;
|
|
|
| -import 'dart:async';
|
| import 'dart:convert';
|
| -import 'dart:io';
|
|
|
| -import 'package:http/http.dart';
|
| -import 'package:http/src/utils.dart';
|
| +import 'package:http/http.dart' as http;
|
| +import 'package:http_parser/http_parser.dart';
|
| import 'package:unittest/unittest.dart';
|
|
|
| -/// The current server instance.
|
| -HttpServer _server;
|
| -
|
| -/// The URL for the current server instance.
|
| -Uri get serverUrl => Uri.parse('http://localhost:${_server.port}');
|
| -
|
| /// A dummy URL for constructing requests that won't be sent.
|
| Uri get dummyUrl => Uri.parse('http://dartlang.org/');
|
|
|
| -/// Starts a new HTTP server.
|
| -Future startServer() {
|
| - return HttpServer.bind("localhost", 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;
|
| - }
|
| -
|
| - 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;
|
| - }
|
| -
|
| - if (path == '/redirect') {
|
| - response.statusCode = 302;
|
| - response.headers.set('location', serverUrl.resolve('/').toString());
|
| - response.contentLength = 0;
|
| - response.close();
|
| - return;
|
| - }
|
| -
|
| - if (path == '/no-content-length') {
|
| - response.statusCode = 200;
|
| - response.contentLength = -1;
|
| - response.write('body');
|
| - response.close();
|
| - return;
|
| - }
|
| -
|
| - new ByteStream(request).toBytes().then((requestBodyBytes) {
|
| - var outputEncoding;
|
| - var encodingName = request.uri.queryParameters['response-encoding'];
|
| - if (encodingName != null) {
|
| - outputEncoding = requiredEncodingForCharset(encodingName);
|
| - } else {
|
| - outputEncoding = ASCII;
|
| - }
|
| -
|
| - response.headers.contentType =
|
| - new ContentType(
|
| - "application", "json", charset: outputEncoding.name);
|
| - response.headers.set('single', 'value');
|
| -
|
| - var requestBody;
|
| - if (requestBodyBytes.isEmpty) {
|
| - requestBody = null;
|
| - } else if (request.headers.contentType != null &&
|
| - request.headers.contentType.charset != null) {
|
| - var encoding = requiredEncodingForCharset(
|
| - request.headers.contentType.charset);
|
| - requestBody = encoding.decode(requestBodyBytes);
|
| - } 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 body = JSON.encode(content);
|
| - response.contentLength = body.length;
|
| - response.write(body);
|
| - response.close();
|
| - });
|
| - });
|
| - });
|
| -}
|
| -
|
| -/// Stops the current HTTP server.
|
| -void stopServer() {
|
| - if (_server != null) {
|
| - _server.close();
|
| - _server = null;
|
| - }
|
| -}
|
| -
|
| /// Removes eight spaces of leading indentation from a multiline string.
|
| ///
|
| /// Note that this is very sensitive to how the literals are styled. They should
|
| @@ -174,34 +68,51 @@ class _Parse extends Matcher {
|
| }
|
| }
|
|
|
| -/// A matcher for functions that throw HttpException.
|
| -Matcher get throwsClientException =>
|
| - throwsA(new isInstanceOf<ClientException>());
|
| +/// A matcher that validates the body of a multipart request after finalization.
|
| +/// The string "{{boundary}}" in [pattern] will be replaced by the boundary
|
| +/// string for the request, and LF newlines will be replaced with CRLF.
|
| +/// Indentation will be normalized.
|
| +Matcher bodyMatches(String pattern) => new _BodyMatches(pattern);
|
|
|
| -/// A matcher for RedirectLimitExceededExceptions.
|
| -const isRedirectLimitExceededException =
|
| - const _RedirectLimitExceededException();
|
| +class _BodyMatches extends Matcher {
|
| + final String _pattern;
|
|
|
| -/// A matcher for functions that throw RedirectLimitExceededException.
|
| -const Matcher throwsRedirectLimitExceededException =
|
| - const Throws(isRedirectLimitExceededException);
|
| + _BodyMatches(this._pattern);
|
|
|
| -class _RedirectLimitExceededException extends TypeMatcher {
|
| - const _RedirectLimitExceededException() :
|
| - super("RedirectLimitExceededException");
|
| -
|
| - bool matches(item, Map matchState) =>
|
| - item is RedirectException && item.message == "Redirect limit exceeded";
|
| -}
|
| -
|
| -/// A matcher for SocketExceptions.
|
| -const isSocketException = const _SocketException();
|
| + bool matches(item, Map matchState) {
|
| + if (item is! http.MultipartRequest) return false;
|
| +
|
| + var future = item.finalize().toBytes().then((bodyBytes) {
|
| + var body = UTF8.decode(bodyBytes);
|
| + var contentType = new MediaType.parse(item.headers['content-type']);
|
| + var boundary = contentType.parameters['boundary'];
|
| + var expected = cleanUpLiteral(_pattern)
|
| + .replaceAll("\n", "\r\n")
|
| + .replaceAll("{{boundary}}", boundary);
|
| +
|
| + expect(body, equals(expected));
|
| + expect(item.contentLength, equals(bodyBytes.length));
|
| + });
|
|
|
| -/// A matcher for functions that throw SocketException.
|
| -const Matcher throwsSocketException =
|
| - const Throws(isSocketException);
|
| + return completes.matches(future, matchState);
|
| + }
|
|
|
| -class _SocketException extends TypeMatcher {
|
| - const _SocketException() : super("SocketException");
|
| - bool matches(item, Map matchState) => item is SocketException;
|
| + Description describe(Description description) {
|
| + return description.add('has a body that matches "$_pattern"');
|
| + }
|
| }
|
| +
|
| +/// A matcher that matches a [http.ClientException] with the given [message].
|
| +///
|
| +/// [message] can be a String or a [Matcher].
|
| +Matcher isClientException(message) => predicate((error) {
|
| + expect(error, new isInstanceOf<http.ClientException>());
|
| + expect(error.message, message);
|
| + return true;
|
| +});
|
| +
|
| +/// A matcher that matches function or future that throws a
|
| +/// [http.ClientException] with the given [message].
|
| +///
|
| +/// [message] can be a String or a [Matcher].
|
| +Matcher throwsClientException(message) => throwsA(isClientException(message));
|
|
|