 Chromium Code Reviews
 Chromium Code Reviews Issue 11810004:
  Make browser tests all run from a server instead of the local filesystem.  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 11810004:
  Make browser tests all run from a server instead of the local filesystem.  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a | 
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. | 
| 4 | 4 | 
| 5 library http_server; | 5 library http_server; | 
| 6 | 6 | 
| 7 import 'dart:io'; | 7 import 'dart:io'; | 
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; | 
| 9 import 'test_suite.dart'; // For TestUtils. | 9 import 'test_suite.dart'; // For TestUtils. | 
| 10 import '../../../pkg/args/lib/args.dart'; | |
| 
ahe
2013/01/16 13:30:37
This breaks test.dart in a subtle, but severe way.
 | |
| 10 | 11 | 
| 11 HttpServer _httpServer; | 12 main() { | 
| 13 /** Convenience method for local testing. */ | |
| 14 var parser = new ArgParser(); | |
| 15 parser.addOption('port', abbr: 'p', | |
| 16 help: 'The main server port we wish to respond to requests.', | |
| 17 defaultsTo: '0'); | |
| 18 parser.addOption('crossOriginPort', abbr: 'c', | |
| 19 help: 'A different port that accepts request from the main server port.', | |
| 20 defaultsTo: '0'); | |
| 21 parser.addOption('mode', abbr: 'm', help: 'Testing mode.', | |
| 22 defaultsTo: 'release'); | |
| 23 parser.addOption('arch', abbr: 'a', help: 'Testing architecture.', | |
| 24 defaultsTo: 'ia32'); | |
| 25 parser.addFlag('help', abbr: 'h', negatable: false, | |
| 26 help: 'Print this usage information.'); | |
| 27 var args = parser.parse(new Options().arguments); | |
| 28 if (args['help']) { | |
| 29 print(parser.getUsage()); | |
| 30 } else { | |
| 31 // Pretend we're running test.dart so that TestUtils doesn't get confused | |
| 32 // about the "current directory." This is only used if we're trying to run | |
| 33 // this file independently for local testing. | |
| 34 TestUtils.testScriptPath = new Path.fromNative( | |
| 35 new Options().script).directoryPath.join( | |
| 36 new Path('../../test.dart')).canonicalize().toNativePath(); | |
| 37 TestingServerRunner.setPackageRootDir({'mode': args['mode'], | |
| 38 'arch': args['arch'], 'system': Platform.operatingSystem, | |
| 39 'build_directory': ''}); | |
| 12 | 40 | 
| 13 void startHttpServer(String host, int port) { | 41 TestingServerRunner.startHttpServer('127.0.0.1', | 
| 14 var basePath = TestUtils.dartDir(); | 42 port: int.parse(args['port'])); | 
| 15 _httpServer = new HttpServer(); | 43 print('Server listening on port ' | 
| 16 _httpServer.onError = (e) { | 44 '${TestingServerRunner.serverList[0].port}.'); | 
| 17 // Consider errors in the builtin http server fatal. | 45 TestingServerRunner.startHttpServer('127.0.0.1', | 
| 18 // Intead of just throwing the exception we print | 46 allowedPort: TestingServerRunner.serverList[0].port, port: | 
| 19 // a message that makes it clearer what happened. | 47 int.parse(args['crossOriginPort'])); | 
| 20 print('Test http server error: $e'); | 48 print( | 
| 21 exit(1); | 49 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); | 
| 22 }; | 50 } | 
| 23 _httpServer.defaultRequestHandler = (request, resp) { | 51 } | 
| 24 var requestPath = new Path(request.path).canonicalize(); | 52 /** | 
| 25 if (!requestPath.isAbsolute) { | 53 * Runs a set of servers that are initialized specifically for the needs of our | 
| 26 resp.statusCode = HttpStatus.NOT_FOUND; | 54 * test framework, such as dealing with package-root. | 
| 27 resp.outputStream.close(); | 55 */ | 
| 28 } else { | 56 class TestingServerRunner { | 
| 29 var path = basePath; | 57 static List serverList = []; | 
| 30 requestPath.segments().forEach((s) => path = path.append(s)); | 58 static Path _packageRootDir = null; | 
| 59 | |
| 60 // Added as a getter so that the function will be called again each time the | |
| 61 // default request handler closure is executed. | |
| 62 static Path get packageRootDir => _packageRootDir; | |
| 63 | |
| 64 static setPackageRootDir(Map configuration) { | |
| 65 _packageRootDir = TestUtils.currentWorkingDirectory.join( | |
| 66 new Path(TestUtils.buildDir(configuration))); | |
| 67 } | |
| 68 | |
| 69 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { | |
| 70 var basePath = TestUtils.dartDir(); | |
| 71 var httpServer = new HttpServer(); | |
| 72 var packagesDirName = 'packages'; | |
| 73 httpServer.onError = (e) { | |
| 74 // Consider errors in the builtin http server fatal. | |
| 75 // Intead of just throwing the exception we print | |
| 76 // a message that makes it clearer what happened. | |
| 77 print('Test http server error: $e'); | |
| 78 exit(1); | |
| 79 }; | |
| 80 httpServer.defaultRequestHandler = (request, resp) { | |
| 81 var requestPath = new Path(request.path.substring(1)).canonicalize(); | |
| 82 var path = basePath.join(requestPath); | |
| 31 var file = new File(path.toNativePath()); | 83 var file = new File(path.toNativePath()); | 
| 84 | |
| 85 if (requestPath.segments().contains(packagesDirName)) { | |
| 86 // Essentially implement the packages path rewriting, so we don't have | |
| 87 // to pass environment variables to the browsers. | |
| 88 var requestPathStr = requestPath.toNativePath().substring( | |
| 89 requestPath.toNativePath().indexOf(packagesDirName)); | |
| 90 path = packageRootDir.append(requestPathStr); | |
| 91 file = new File(path.toNativePath()); | |
| 92 } | |
| 32 file.exists().then((exists) { | 93 file.exists().then((exists) { | 
| 33 if (exists) { | 94 if (exists) { | 
| 34 // Allow loading from localhost in browsers. | 95 if (allowedPort != -1) { | 
| 35 resp.headers.set("Access-Control-Allow-Origin", "*"); | 96 // Allow loading from localhost:$allowedPort in browsers. | 
| 97 resp.headers.set("Access-Control-Allow-Origin", | |
| 98 "http://127.0.0.1:${allowedPort+1}"); | |
| 99 resp.headers.set('Access-Control-Allow-Credentials', 'true'); | |
| 100 } else { | |
| 101 // No allowedPort specified. Allow from anywhere (but cross-origin | |
| 102 // requests *with credentials* will fail because you can't use "*"). | |
| 103 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
| 104 } | |
| 105 if (path.toNativePath().endsWith('.html')) { | |
| 106 resp.headers.set('Content-Type', 'text/html'); | |
| 107 } else if (path.toNativePath().endsWith('.js')) { | |
| 108 resp.headers.set('Content-Type', 'application/javascript'); | |
| 109 } else if (path.toNativePath().endsWith('.dart')) { | |
| 110 resp.headers.set('Content-Type', 'application/dart'); | |
| 111 } | |
| 36 file.openInputStream().pipe(resp.outputStream); | 112 file.openInputStream().pipe(resp.outputStream); | 
| 37 } else { | 113 } else { | 
| 38 resp.statusCode = HttpStatus.NOT_FOUND; | 114 resp.statusCode = HttpStatus.NOT_FOUND; | 
| 39 resp.outputStream.close(); | 115 try { | 
| 116 resp.outputStream.close(); | |
| 117 } catch (e) { | |
| 118 if (e is StreamException) { | |
| 119 print('Test http_server error closing the response stream: $e'); | |
| 120 } else { | |
| 121 throw e; | |
| 122 } | |
| 123 } | |
| 40 } | 124 } | 
| 41 }); | 125 }); | 
| 42 } | |
| 43 }; | |
| 44 | 126 | 
| 45 // Echos back the contents of the request as the response data. | 127 }; | 
| 46 _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | |
| 47 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
| 48 | 128 | 
| 49 request.inputStream.pipe(resp.outputStream); | 129 // Echos back the contents of the request as the response data. | 
| 50 }); | 130 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 
| 131 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
| 51 | 132 | 
| 52 _httpServer.listen(host, port); | 133 request.inputStream.pipe(resp.outputStream); | 
| 134 }); | |
| 135 | |
| 136 httpServer.listen(host, port); | |
| 137 serverList.add(httpServer); | |
| 138 } | |
| 139 | |
| 140 static terminateHttpServers() { | |
| 141 for (var server in serverList) server.close(); | |
| 142 } | |
| 53 } | 143 } | 
| 54 | |
| 55 terminateHttpServer() { | |
| 56 if (_httpServer != null) _httpServer.close(); | |
| 57 } | |
| OLD | NEW |