Index: tools/testing/dart/http_server.dart |
diff --git a/tools/testing/dart/http_server.dart b/tools/testing/dart/http_server.dart |
index 8d233a83a0000ed525aa73635bb8b3b26d4d3194..028bbcbb6ec4cb342bfe703cca71ff94dcee8e21 100644 |
--- a/tools/testing/dart/http_server.dart |
+++ b/tools/testing/dart/http_server.dart |
@@ -7,51 +7,133 @@ library http_server; |
import 'dart:io'; |
import 'dart:isolate'; |
import 'test_suite.dart'; // For TestUtils. |
+import '../../../pkg/args/lib/args.dart'; |
+import '../../../pkg/path/lib/path.dart' as pathLib; |
-HttpServer _httpServer; |
- |
-void startHttpServer(String host, int port) { |
- var basePath = TestUtils.dartDir(); |
- _httpServer = new HttpServer(); |
- _httpServer.onError = (e) { |
- // Consider errors in the builtin http server fatal. |
- // Intead of just throwing the exception we print |
- // a message that makes it clearer what happened. |
- print('Test http server error: $e'); |
- exit(1); |
- }; |
- _httpServer.defaultRequestHandler = (request, resp) { |
- var requestPath = new Path(request.path).canonicalize(); |
- if (!requestPath.isAbsolute) { |
- resp.statusCode = HttpStatus.NOT_FOUND; |
- resp.outputStream.close(); |
- } else { |
- var path = basePath; |
- requestPath.segments().forEach((s) => path = path.append(s)); |
- var file = new File(path.toNativePath()); |
+main() { |
+ /** Convenience method for local testing. */ |
+ var parser = new ArgParser(); |
+ parser.addOption('port', abbr: 'p', |
+ help: 'The main server port we wish to respond to requests.', |
+ defaultsTo: '0'); |
+ parser.addOption('crossOriginPort', abbr: 'c', |
+ help: 'A different port that accepts request from the main server port.', |
+ defaultsTo: '0'); |
+ parser.addFlag('help', abbr: 'h', negatable: false, |
+ help: 'Print this usage information.'); |
+ var args = parser.parse(new Options().arguments); |
+ if (args['help']) { |
+ print(parser.getUsage()); |
+ } else { |
+ // Pretend we're running test.dart so that TestUtils doesn't get confused |
+ // about the "current directory." This is only used if we're trying to run |
+ // this file independently for local testing. |
+ TestUtils.testScriptPath = 'tools/test.dart'; |
+ TestingServerRunner.setPackageRootDir({'mode': 'release'}); |
+ |
+ TestingServerRunner.startHttpServer('127.0.0.1', |
+ port: int.parse(args['port'])); |
+ print( |
+ 'Server listening on port ${TestingServerRunner.serverList[0].port}.'); |
Mads Ager (google)
2013/01/11 10:11:03
Nit: consider splitting the string in the middle i
|
+ TestingServerRunner.startHttpServer('127.0.0.1', |
+ allowedPort: TestingServerRunner.serverList[0].port, port: |
+ int.parse(args['crossOriginPort'])); |
+ print( |
+ 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
+ } |
+} |
+/** |
+ * Runs a set of servers that are initialized specifically the needs for our |
Mads Ager (google)
2013/01/11 10:11:03
Reformulate: specifically the needs for
|
+ * test framework, such as dealing with package-root. |
+ */ |
+class TestingServerRunner { |
+ static List serverList = []; |
+ static String _packageRootDir = null; |
+ |
+ // Added as a getter so that the function will be called again each time the |
+ // default request handler closure is executed. |
+ static String get packageRootDir => _packageRootDir; |
+ |
+ static setPackageRootDir(Map configuration) { |
+ _packageRootDir = pathLib.join(TestUtils.currentWorkingDirectory.toString(), |
+ TestUtils.buildDir(configuration)); |
+ } |
+ |
+ static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { |
+ var basePath = TestUtils.dartDir().toNativePath(); |
+ var httpServer = new HttpServer(); |
+ var packagesDirName = 'packages'; |
+ httpServer.onError = (e) { |
+ // Consider errors in the builtin http server fatal. |
+ // Intead of just throwing the exception we print |
+ // a message that makes it clearer what happened. |
+ print('Test http server error: $e'); |
+ exit(1); |
+ }; |
+ httpServer.defaultRequestHandler = (request, resp) { |
+ var builder = new pathLib.Builder(style: pathLib.Style.posix); |
+ // URLs will have a starting slash, but in this case we don't want to |
+ // interpret it as the root directory. |
+ var requestPath = builder.normalize(request.path).substring(1); |
+ var path = pathLib.join(basePath, requestPath); |
+ var file = new File(path); |
+ |
+ if (pathLib.split(requestPath).contains(packagesDirName)) { |
+ // Essentially implement the packages path rewriting, so we don't have |
+ // to pass environment variables to the browsers. |
+ requestPath = requestPath.substring( |
+ requestPath.indexOf(packagesDirName)); |
+ path = pathLib.join(packageRootDir, requestPath); |
+ file = new File(path); |
+ } |
file.exists().then((exists) { |
if (exists) { |
- // Allow loading from localhost in browsers. |
- resp.headers.set("Access-Control-Allow-Origin", "*"); |
+ if (allowedPort != -1) { |
+ // Allow loading from localhost:$allowedPort in browsers. |
+ resp.headers.set("Access-Control-Allow-Origin", |
+ "http://127.0.0.1:$allowedPort"); |
+ resp.headers.set('Access-Control-Allow-Credentials', 'true'); |
+ } else { |
+ // No allowedPort specified. Allow from anywhere (but cross-origin |
+ // requests *with credentials* will fail because you can't use "*"). |
+ resp.headers.set("Access-Control-Allow-Origin", "*"); |
+ } |
+ if (path.endsWith('.html')) { |
+ resp.headers.set('Content-Type', 'text/html'); |
+ } else if (path.endsWith('.js')) { |
+ resp.headers.set('Content-Type', 'application/javascript'); |
+ } else if (path.endsWith('.dart')) { |
+ resp.headers.set('Content-Type', 'application/dart'); |
+ } |
file.openInputStream().pipe(resp.outputStream); |
} else { |
resp.statusCode = HttpStatus.NOT_FOUND; |
- resp.outputStream.close(); |
+ try { |
+ resp.outputStream.close(); |
+ } catch (e) { |
+ if (e is StreamException) { |
+ print('Test http_server error closing the response stream: $e'); |
+ } else { |
+ throw e; |
+ } |
+ } |
} |
}); |
- } |
- }; |
- // Echos back the contents of the request as the response data. |
- _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
- resp.headers.set("Access-Control-Allow-Origin", "*"); |
+ }; |
- request.inputStream.pipe(resp.outputStream); |
- }); |
+ // Echos back the contents of the request as the response data. |
+ httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { |
+ resp.headers.set("Access-Control-Allow-Origin", "*"); |
- _httpServer.listen(host, port); |
-} |
+ request.inputStream.pipe(resp.outputStream); |
+ }); |
+ |
+ httpServer.listen(host, port); |
+ serverList.add(httpServer); |
+ } |
-terminateHttpServer() { |
- if (_httpServer != null) _httpServer.close(); |
+ static terminateHttpServers() { |
+ for (var server in serverList) server.close(); |
+ } |
} |