Chromium Code Reviews| Index: pkg/http_server/test/utils.dart |
| diff --git a/pkg/http_server/test/utils.dart b/pkg/http_server/test/utils.dart |
| index d9e67a8daefa79fd58b03605775775112ac55e99..9f343e36ecd9a36c45313cf5ae6b46588d5bed58 100644 |
| --- a/pkg/http_server/test/utils.dart |
| +++ b/pkg/http_server/test/utils.dart |
| @@ -11,8 +11,25 @@ import "package:unittest/unittest.dart"; |
| import 'package:http_server/http_server.dart'; |
| +import 'http_mock.dart'; |
| + |
| +final _testLogicRequestExpando = new Expando('foo'); |
| + |
| void testVirtualDir(String name, Future func(Directory dir)) { |
| test(name, () { |
| + _testLogicRequestExpando[currentTestCase] = false; |
| + |
| + var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); |
| + |
| + return func(dir) |
| + .whenComplete(() { |
| + return dir.delete(recursive: true); |
| + }); |
| + }); |
| + |
| + test('$name - logical', () { |
| + _testLogicRequestExpando[currentTestCase] = true; |
|
Anders Johnsen
2014/01/09 06:12:23
This stuff really needs some documentation. What i
kevmoo
2014/01/10 18:12:31
Done.
|
| + |
| var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); |
| return func(dir) |
| @@ -22,18 +39,36 @@ void testVirtualDir(String name, Future func(Directory dir)) { |
| }); |
| } |
| +Future<MockHttpResponse> _serveRequest(VirtualDirectory virDir, MockHttpRequest request) => |
| + virDir.serveRequest(request).then((value) { |
| + expect(value, isNull); |
| + expect(request.response.mockDone, isTrue); |
| + return request.response; |
| + }); |
| + |
| Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, |
| String path, |
| {String host, |
| bool secure: false, |
| DateTime ifModifiedSince, |
| bool rawPath: false}) { |
| - return _withServer((server) { |
| + if(_testLogicRequestExpando[currentTestCase] == true) { |
| + var uri = _getUri(0, path, secure: secure, rawPath: rawPath); |
| + |
| + var request = new MockHttpRequest(uri, ifModifiedSince: ifModifiedSince); |
| + |
| + return _serveRequest(virtualDir, request) |
| + .then((response) { |
| + return response.statusCode; |
| + }); |
| + }; |
| + |
| + assert(_testLogicRequestExpando[currentTestCase] == false); |
| - virtualDir.serve(server); |
| - return getStatusCode(server.port, path, host: host, secure: secure, |
| + return _withServer(virtualDir, (port) { |
| + return getStatusCode(port, path, host: host, secure: secure, |
| ifModifiedSince: ifModifiedSince, rawPath: rawPath); |
| - }); |
| + }); |
| } |
| Future<int> getStatusCode(int port, |
| @@ -42,17 +77,8 @@ Future<int> getStatusCode(int port, |
| bool secure: false, |
| DateTime ifModifiedSince, |
| bool rawPath: false}) { |
| - 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) { |
| @@ -67,37 +93,82 @@ Future<int> getStatusCode(int port, |
| } |
| Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) { |
| - return _withServer((server) { |
| - virDir.serve(server); |
| + if(_testLogicRequestExpando[currentTestCase] == true) { |
|
Anders Johnsen
2014/01/09 06:12:23
remove '== true'
kevmoo
2014/01/10 05:02:00
Done.
|
| + var uri = _getUri(0, path); |
| + |
| + var request = new MockHttpRequest(uri); |
| - return new HttpClient() |
| - .get('localhost', server.port, path) |
| - .then((request) => request.close()) |
| - .then((response) => response.drain().then((_) => response.headers)); |
| + return _serveRequest(virDir, request) |
| + .then((response) { |
| + return response.headers; |
| + }); |
| + }; |
| + |
| + assert(_testLogicRequestExpando[currentTestCase] == false); |
| + |
| + return _withServer(virDir, (port) { |
| + return _getHeaders(port, path); |
| }); |
| } |
| Future<String> getAsString(VirtualDirectory virtualDir, String path) { |
| - return _withServer((server) { |
| - virtualDir.serve(server); |
| + if(_testLogicRequestExpando[currentTestCase] == true) { |
|
Anders Johnsen
2014/01/09 06:12:23
remove '== true'.
kevmoo
2014/01/10 05:02:00
Done.
|
| + var uri = _getUri(0, path); |
| + |
| + var request = new MockHttpRequest(uri); |
| + |
| + return _serveRequest(virtualDir, request) |
| + .then((response) { |
| + return response.mockContent; |
| + }); |
| + }; |
| - return new HttpClient() |
| - .get('localhost', server.port, path) |
| - .then((request) => request.close()) |
| - .then((response) => UTF8.decodeStream(response)); |
| + assert(_testLogicRequestExpando[currentTestCase] == false); |
| + |
| + return _withServer(virtualDir, (int port) { |
| + return _getAsString(port, path); |
| }); |
| } |
| -Future _withServer(Future func(HttpServer server)) { |
| +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"; |