| Index: pkg/http_server/test/utils.dart
|
| diff --git a/pkg/http_server/test/utils.dart b/pkg/http_server/test/utils.dart
|
| index 898cf31e1ecea9cb1b4529c918ccf3b189177257..c0b3c5ca49e154a1d138b8f0178f230f31401838 100644
|
| --- a/pkg/http_server/test/utils.dart
|
| +++ b/pkg/http_server/test/utils.dart
|
| @@ -11,8 +11,27 @@ import "package:unittest/unittest.dart";
|
|
|
| import 'package:http_server/http_server.dart';
|
|
|
| +import 'http_mock.dart';
|
| +
|
| +/**
|
| + * Used to flag a given test case as being a mock or not.
|
| + */
|
| +final _isMockTestExpando = new Expando<bool>('isMockTest');
|
| +
|
| void testVirtualDir(String name, Future func(Directory dir)) {
|
| + _testVirtualDir(name, false, func);
|
| + _testVirtualDir(name, true, func);
|
| +}
|
| +
|
| +void _testVirtualDir(String name, bool useMocks, Future func(Directory dir)) {
|
| + if(useMocks) {
|
| + name = '$name, with mocks';
|
| + }
|
| +
|
| test(name, () {
|
| + // see subsequent access to this expando below
|
| + _isMockTestExpando[currentTestCase] = useMocks;
|
| +
|
| var dir = Directory.systemTemp.createTempSync('http_server_virtual_');
|
|
|
| return func(dir)
|
| @@ -29,13 +48,27 @@ Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir,
|
| DateTime ifModifiedSince,
|
| bool rawPath: false,
|
| bool followRedirects: true}) {
|
| - return _withServer((server) {
|
|
|
| - virtualDir.serve(server);
|
| - return getStatusCode(server.port, path, host: host, secure: secure,
|
| + // if this is a mock test, then run the mock code path
|
| + if(_isMockTestExpando[currentTestCase]) {
|
| + var uri = _getUri(0, path, secure: secure, rawPath: rawPath);
|
| +
|
| + var request = new MockHttpRequest(uri, followRedirects: followRedirects,
|
| + ifModifiedSince: ifModifiedSince);
|
| +
|
| + return _withMockRequest(virtualDir, request)
|
| + .then((response) {
|
| + return response.statusCode;
|
| + });
|
| + };
|
| +
|
| + assert(_isMockTestExpando[currentTestCase] == false);
|
| +
|
| + return _withServer(virtualDir, (port) {
|
| + return getStatusCode(port, path, host: host, secure: secure,
|
| ifModifiedSince: ifModifiedSince, rawPath: rawPath,
|
| followRedirects: followRedirects);
|
| - });
|
| + });
|
| }
|
|
|
| Future<int> getStatusCode(int port,
|
| @@ -45,17 +78,7 @@ Future<int> getStatusCode(int port,
|
| DateTime ifModifiedSince,
|
| bool rawPath: false,
|
| bool followRedirects: true}) {
|
| - Uri uri;
|
| - if (rawPath) {
|
| - uri = new Uri(scheme: secure ? 'https' : 'http',
|
| - host: 'localhost',
|
| - port: port,
|
| - path: path);
|
| - } else {
|
| - uri = (secure ?
|
| - new Uri.https('localhost:$port', path) :
|
| - new Uri.http('localhost:$port', path));
|
| - }
|
| + var uri = _getUri(port, path, secure: secure, rawPath: rawPath);
|
|
|
| return new HttpClient().getUrl(uri)
|
| .then((request) {
|
| @@ -71,37 +94,107 @@ Future<int> getStatusCode(int port,
|
| }
|
|
|
| Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) {
|
| - return _withServer((server) {
|
| - virDir.serve(server);
|
|
|
| - return new HttpClient()
|
| - .get('localhost', server.port, path)
|
| - .then((request) => request.close())
|
| - .then((response) => response.drain().then((_) => response.headers));
|
| + // if this is a mock test, then run the mock code path
|
| + if(_isMockTestExpando[currentTestCase]) {
|
| + var uri = _getUri(0, path);
|
| +
|
| + var request = new MockHttpRequest(uri);
|
| +
|
| + return _withMockRequest(virDir, request)
|
| + .then((response) {
|
| + return response.headers;
|
| + });
|
| + }
|
| +
|
| + assert(_isMockTestExpando[currentTestCase] == false);
|
| +
|
| + return _withServer(virDir, (port) {
|
| + return _getHeaders(port, path);
|
| });
|
| }
|
|
|
| Future<String> getAsString(VirtualDirectory virtualDir, String path) {
|
| - return _withServer((server) {
|
| - virtualDir.serve(server);
|
|
|
| - return new HttpClient()
|
| - .get('localhost', server.port, path)
|
| - .then((request) => request.close())
|
| - .then((response) => UTF8.decodeStream(response));
|
| + // if this is a mock test, then run the mock code path
|
| + if(_isMockTestExpando[currentTestCase]) {
|
| + var uri = _getUri(0, path);
|
| +
|
| + var request = new MockHttpRequest(uri);
|
| +
|
| + return _withMockRequest(virtualDir, request)
|
| + .then((response) {
|
| + return response.mockContent;
|
| + });
|
| + };
|
| +
|
| + assert(_isMockTestExpando[currentTestCase] == false);
|
| +
|
| + return _withServer(virtualDir, (int port) {
|
| + return _getAsString(port, path);
|
| });
|
| }
|
|
|
| -Future _withServer(Future func(HttpServer server)) {
|
| +Future<MockHttpResponse> _withMockRequest(VirtualDirectory virDir,
|
| + MockHttpRequest request) {
|
| + return virDir.serveRequest(request).then((value) {
|
| + expect(value, isNull);
|
| + expect(request.response.mockDone, isTrue);
|
| + return request.response;
|
| + })
|
| + .then((HttpResponse response) {
|
| + if(response.statusCode == HttpStatus.MOVED_PERMANENTLY ||
|
| + response.statusCode == HttpStatus.MOVED_TEMPORARILY) {
|
| + if(request.followRedirects == true) {
|
| + var uri = Uri.parse(response.headers.value(HttpHeaders.LOCATION));
|
| + var newMock = new MockHttpRequest(uri, followRedirects: true);
|
| +
|
| + return _withMockRequest(virDir, newMock);
|
| + }
|
| + }
|
| + return response;
|
| + });
|
| +}
|
| +
|
| +Future _withServer(VirtualDirectory virDir, Future func(int port)) {
|
| HttpServer server;
|
| return HttpServer.bind('localhost', 0)
|
| .then((value) {
|
| server = value;
|
| - return func(server);
|
| + virDir.serve(server);
|
| + return func(server.port);
|
| })
|
| .whenComplete(() => server.close());
|
| }
|
|
|
| +Future<HttpHeaders> _getHeaders(int port, String path) =>
|
| + new HttpClient()
|
| + .get('localhost', port, path)
|
| + .then((request) => request.close())
|
| + .then((response) => response.drain().then(
|
| + (_) => response.headers));
|
| +
|
| +Future<String> _getAsString(int port, String path) =>
|
| + new HttpClient()
|
| + .get('localhost', port, path)
|
| + .then((request) => request.close())
|
| + .then((response) => UTF8.decodeStream(response));
|
| +
|
| +Uri _getUri(int port,
|
| + String path,
|
| + {bool secure: false,
|
| + bool rawPath: false}) {
|
| + if (rawPath) {
|
| + return new Uri(scheme: secure ? 'https' : 'http',
|
| + host: 'localhost',
|
| + port: port,
|
| + path: path);
|
| + } else {
|
| + return (secure ?
|
| + new Uri.https('localhost:$port', path) :
|
| + new Uri.http('localhost:$port', path));
|
| + }
|
| +}
|
|
|
| const CERTIFICATE = "localhost_cert";
|
|
|
|
|