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 f1159c50acaba11cbdc3ee0b6702720637b4c8d0..76211a55d17448722c63bd5ab6df8fc8c665a465 100644 |
| --- a/pkg/http_server/test/utils.dart |
| +++ b/pkg/http_server/test/utils.dart |
| @@ -7,6 +7,33 @@ library utils; |
| import 'dart:async'; |
| import 'dart:convert'; |
| import 'dart:io'; |
| +import "package:unittest/unittest.dart"; |
| + |
| +import 'package:http_server/http_server.dart'; |
| + |
| +void testVirtualDir(String name, Future func(Directory dir)) { |
| + test(name, () { |
| + var dir = Directory.systemTemp.createTempSync('http_server_virtual_'); |
| + |
| + return func(dir) |
| + .whenComplete(() { |
| + return dir.delete(recursive: true); |
| + }); |
| + }); |
| +} |
| + |
| +Future<int> getStatusCodeForVirtDir(VirtualDirectory virtualDir, |
| + String path, |
|
Anders Johnsen
2014/01/07 07:42:10
Indentation
kevmoo
2014/01/07 19:36:52
Done.
|
| + {String host, |
| + bool secure: false, |
| + DateTime ifModifiedSince, |
| + bool rawPath: false}) => |
| + _withServer((server) { |
| + |
| + virtualDir.serve(server); |
| + return getStatusCode(server.port, path, host: host, secure: secure, |
| + ifModifiedSince: ifModifiedSince, rawPath: rawPath); |
| + }); |
| Future<int> getStatusCode(int port, |
| String path, |
| @@ -38,21 +65,35 @@ Future<int> getStatusCode(int port, |
| (_) => response.statusCode)); |
| } |
| +Future<HttpHeaders> getHeaders(VirtualDirectory virDir, String path) => |
|
Anders Johnsen
2014/01/07 07:42:10
Please use { ... } for multiline body.
kevmoo
2014/01/07 19:36:52
Done.
|
| + _withServer((server) { |
| + virDir.serve(server); |
| -Future<HttpHeaders> getHeaders(int port, String path) => |
| - new HttpClient() |
| - .get('localhost', port, path) |
| - .then((request) => request.close()) |
| - .then((response) => response.drain().then( |
| - (_) => response.headers)); |
| + return new HttpClient() |
| + .get('localhost', server.port, path) |
| + .then((request) => request.close()) |
| + .then((response) => response.drain().then((_) => response.headers)); |
| + }); |
| +Future<String> getAsString(VirtualDirectory virtualDir, String path) => |
| + _withServer((server) { |
| + virtualDir.serve(server); |
| + return new HttpClient() |
| + .get('localhost', server.port, path) |
| + .then((request) => request.close()) |
| + .then((response) => UTF8.decodeStream(response)); |
| + }); |
| -Future<String> getAsString(int port, String path) => |
| - new HttpClient() |
| - .get('localhost', port, path) |
| - .then((request) => request.close()) |
| - .then((response) => UTF8.decodeStream(response)); |
| +Future _withServer(Future func(HttpServer server)) { |
|
Anders Johnsen
2014/01/07 07:42:10
Consider:
{
return HttpServer.bind('localhost',
kevmoo
2014/01/07 19:36:52
I'll argue against this.
If func throws an error
|
| + HttpServer server; |
| + return HttpServer.bind('localhost', 0) |
| + .then((value) { |
| + server = value; |
| + return func(server); |
| + }) |
| + .whenComplete(() => server.close()); |
| +} |
| const CERTIFICATE = "localhost_cert"; |