| 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 // TODO(efortuna): Rewrite to not use the args library and simply take an | 10 // TODO(efortuna): Rewrite to not use the args library and simply take an |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 print(parser.getUsage()); | 32 print(parser.getUsage()); |
| 33 } else { | 33 } else { |
| 34 // Pretend we're running test.dart so that TestUtils doesn't get confused | 34 // Pretend we're running test.dart so that TestUtils doesn't get confused |
| 35 // about the "current directory." This is only used if we're trying to run | 35 // about the "current directory." This is only used if we're trying to run |
| 36 // this file independently for local testing. | 36 // this file independently for local testing. |
| 37 TestUtils.testScriptPath = new Path(new Options().script) | 37 TestUtils.testScriptPath = new Path(new Options().script) |
| 38 .directoryPath | 38 .directoryPath |
| 39 .join(new Path('../../test.dart')) | 39 .join(new Path('../../test.dart')) |
| 40 .canonicalize() | 40 .canonicalize() |
| 41 .toNativePath(); | 41 .toNativePath(); |
| 42 var conf = {'mode': args['mode'], | 42 TestingServerRunner.setPackageRootDir({'mode': args['mode'], |
| 43 'arch': args['arch'], 'system': Platform.operatingSystem, | 43 'arch': args['arch'], 'system': Platform.operatingSystem, |
| 44 'build_directory': ''}; | 44 'build_directory': ''}); |
| 45 TestingServerRunner.setPackageRootDir(conf); | 45 |
| 46 TestingServerRunner.setBuildDir(conf); | |
| 47 TestingServerRunner.startHttpServer('127.0.0.1', | 46 TestingServerRunner.startHttpServer('127.0.0.1', |
| 48 port: int.parse(args['port'])); | 47 port: int.parse(args['port'])); |
| 49 print('Server listening on port ' | 48 print('Server listening on port ' |
| 50 '${TestingServerRunner.serverList[0].port}.'); | 49 '${TestingServerRunner.serverList[0].port}.'); |
| 51 TestingServerRunner.startHttpServer('127.0.0.1', | 50 TestingServerRunner.startHttpServer('127.0.0.1', |
| 52 allowedPort: TestingServerRunner.serverList[0].port, port: | 51 allowedPort: TestingServerRunner.serverList[0].port, port: |
| 53 int.parse(args['crossOriginPort'])); | 52 int.parse(args['crossOriginPort'])); |
| 54 print( | 53 print( |
| 55 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); | 54 'Server listening on port ${TestingServerRunner.serverList[1].port}.'); |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 /** | 57 /** |
| 59 * Runs a set of servers that are initialized specifically for the needs of our | 58 * Runs a set of servers that are initialized specifically for the needs of our |
| 60 * test framework, such as dealing with package-root. | 59 * test framework, such as dealing with package-root. |
| 61 */ | 60 */ |
| 62 class TestingServerRunner { | 61 class TestingServerRunner { |
| 63 static List serverList = []; | 62 static List serverList = []; |
| 64 static Path _packageRootDir = null; | 63 static Path _packageRootDir = null; |
| 65 static Path _buildDirectory = null; | |
| 66 | 64 |
| 67 // Added as a getter so that the function will be called again each time the | 65 // Added as a getter so that the function will be called again each time the |
| 68 // default request handler closure is executed. | 66 // default request handler closure is executed. |
| 69 static Path get packageRootDir => _packageRootDir; | 67 static Path get packageRootDir => _packageRootDir; |
| 70 static Path get buildDirectory => _buildDirectory; | |
| 71 | 68 |
| 72 static setPackageRootDir(Map configuration) { | 69 static setPackageRootDir(Map configuration) { |
| 73 _packageRootDir = TestUtils.currentWorkingDirectory.join( | 70 _packageRootDir = TestUtils.currentWorkingDirectory.join( |
| 74 new Path(TestUtils.buildDir(configuration))); | 71 new Path(TestUtils.buildDir(configuration))); |
| 75 } | 72 } |
| 76 | 73 |
| 77 static setBuildDir(Map configuration) { | |
| 78 _buildDirectory = new Path(TestUtils.buildDir(configuration)); | |
| 79 } | |
| 80 | |
| 81 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { | 74 static startHttpServer(String host, {int allowedPort:-1, int port: 0}) { |
| 82 var basePath = TestUtils.dartDir(); | 75 var basePath = TestUtils.dartDir(); |
| 83 var httpServer = new HttpServer(); | 76 var httpServer = new HttpServer(); |
| 84 var packagesDirName = 'packages'; | 77 var packagesDirName = 'packages'; |
| 85 httpServer.onError = (e) { | 78 httpServer.onError = (e) { |
| 86 // TODO(ricow): Once we have a debug log we should write this out there. | 79 // TODO(ricow): Once we have a debug log we should write this out there. |
| 87 print('Test http server error: $e'); | 80 print('Test http server error: $e'); |
| 88 }; | 81 }; |
| 89 httpServer.defaultRequestHandler = (request, resp) { | 82 httpServer.defaultRequestHandler = (request, resp) { |
| 90 var requestPath = new Path(request.path.substring(1)).canonicalize(); | 83 var requestPath = new Path(request.path.substring(1)).canonicalize(); |
| 91 var path = basePath.join(requestPath); | 84 var path = basePath.join(requestPath); |
| 92 var file = new File(path.toNativePath()); | 85 var file = new File(path.toNativePath()); |
| 93 // Since the build directory may not be located directly beneath the dart | 86 |
| 94 // root directory (if we pass it in, e.g., for dartium testing) we serve | |
| 95 // files from the build directory explicitly. Please note that if | |
| 96 // buildDirectory has the same name as a directory inside the dart repo | |
| 97 // we will server files from the buildDirectory. | |
| 98 if (path.toString().startsWith(buildDirectory.toString())) { | |
| 99 file = new File(path.toNativePath()); | |
| 100 } | |
| 101 if (requestPath.segments().contains(packagesDirName)) { | 87 if (requestPath.segments().contains(packagesDirName)) { |
| 102 // Essentially implement the packages path rewriting, so we don't have | 88 // Essentially implement the packages path rewriting, so we don't have |
| 103 // to pass environment variables to the browsers. | 89 // to pass environment variables to the browsers. |
| 104 var requestPathStr = requestPath.toNativePath().substring( | 90 var requestPathStr = requestPath.toNativePath().substring( |
| 105 requestPath.toNativePath().indexOf(packagesDirName)); | 91 requestPath.toNativePath().indexOf(packagesDirName)); |
| 106 path = packageRootDir.append(requestPathStr); | 92 path = packageRootDir.append(requestPathStr); |
| 107 file = new File(path.toNativePath()); | 93 file = new File(path.toNativePath()); |
| 108 } | 94 } |
| 109 file.exists().then((exists) { | 95 file.exists().then((exists) { |
| 110 if (exists) { | 96 if (exists) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 }); | 136 }); |
| 151 | 137 |
| 152 httpServer.listen(host, port); | 138 httpServer.listen(host, port); |
| 153 serverList.add(httpServer); | 139 serverList.add(httpServer); |
| 154 } | 140 } |
| 155 | 141 |
| 156 static terminateHttpServers() { | 142 static terminateHttpServers() { |
| 157 for (var server in serverList) server.close(); | 143 for (var server in serverList) server.close(); |
| 158 } | 144 } |
| 159 } | 145 } |
| OLD | NEW |