| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 // | |
| 5 // Test: | |
| 6 // *) Compiling a script fetched over HTTP. | |
| 7 // *) Importing a library fetched over HTTP. | |
| 8 // *) Automatically resolving package_root when script is fetched over HTTP. | |
| 9 | |
| 10 library http_launch_test; | |
| 11 | |
| 12 import 'dart:async'; | |
| 13 import 'dart:io'; | |
| 14 import 'package:async_helper/async_helper.dart'; | |
| 15 import 'package:expect/expect.dart'; | |
| 16 import 'package:path/path.dart' as path; | |
| 17 | |
| 18 Uri pathOfData = Platform.script.resolve('http_launch_data/'); | |
| 19 Directory tempDir; | |
| 20 String outFilePath; | |
| 21 | |
| 22 _sendNotFound(HttpResponse response) { | |
| 23 response.statusCode = HttpStatus.NOT_FOUND; | |
| 24 response.close(); | |
| 25 } | |
| 26 | |
| 27 Future handleRequest(HttpRequest request) { | |
| 28 final String path = request.uri.path.substring(1); | |
| 29 final Uri requestPath = pathOfData.resolve(path); | |
| 30 final File file = new File(requestPath.toFilePath()); | |
| 31 return file.exists().then((bool found) { | |
| 32 if (found) { | |
| 33 file.openRead() | |
| 34 .pipe(request.response) | |
| 35 .catchError((e) { _sendNotFound(request.response); }); | |
| 36 } else { | |
| 37 _sendNotFound(request.response); | |
| 38 } | |
| 39 }); | |
| 40 } | |
| 41 | |
| 42 Future launchDart2Js(args) { | |
| 43 String ext = Platform.isWindows ? '.bat' : ''; | |
| 44 String command = | |
| 45 path.normalize(path.join(path.fromUri(Platform.script), | |
| 46 '../../../../sdk/bin/dart2js${ext}')); | |
| 47 return Process.run(command, args); | |
| 48 } | |
| 49 | |
| 50 void check(ProcessResult result) { | |
| 51 Expect.equals(0, result.exitCode); | |
| 52 File outFile = new File(outFilePath); | |
| 53 Expect.isTrue(outFile.existsSync()); | |
| 54 Expect.isTrue(outFile.readAsStringSync().contains("hello http tester")); | |
| 55 } | |
| 56 | |
| 57 void checkNotFound(ProcessResult result, String filename) { | |
| 58 Expect.notEquals(0, result.exitCode); | |
| 59 File outFile = new File(outFilePath); | |
| 60 Expect.isTrue(result.stdout.contains("404")); | |
| 61 Expect.isTrue(result.stdout.contains(filename)); | |
| 62 } | |
| 63 | |
| 64 cleanup() { | |
| 65 File outFile = new File(outFilePath); | |
| 66 if (outFile.existsSync()) { | |
| 67 outFile.deleteSync(); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 Future testNonHttp() { | |
| 72 String inFilePath = pathOfData.resolve('http_launch_main.dart').toFilePath(); | |
| 73 List<String> args = [inFilePath, "--out=" + outFilePath]; | |
| 74 return launchDart2Js(args) | |
| 75 .then(check) | |
| 76 .then((_) { cleanup(); }); | |
| 77 } | |
| 78 | |
| 79 Future testHttpMain(String serverUrl) { | |
| 80 String inFilePath = '$serverUrl/http_launch_main.dart'; | |
| 81 List<String> args = [inFilePath, "--out=" + outFilePath]; | |
| 82 return launchDart2Js(args) | |
| 83 .then(check) | |
| 84 .then((_) { cleanup(); }); | |
| 85 } | |
| 86 | |
| 87 Future testHttpLib(String serverUrl) { | |
| 88 File file = new File(path.join(tempDir.path, "in.dart")); | |
| 89 file.writeAsStringSync(""" | |
| 90 import '$serverUrl/lib1.dart'; | |
| 91 main() { print(foo()); } | |
| 92 """); | |
| 93 String inFilePath = file.path; | |
| 94 List<String> args = [inFilePath, "--out=" + outFilePath]; | |
| 95 return launchDart2Js(args) | |
| 96 .then(check) | |
| 97 .whenComplete(file.deleteSync) | |
| 98 .then((_) { cleanup(); }); | |
| 99 } | |
| 100 | |
| 101 Future testHttpPackage(String serverUrl) { | |
| 102 String inFilePath = | |
| 103 pathOfData.resolve('http_launch_main_package.dart').toFilePath(); | |
| 104 String packageRoot = '$serverUrl/packages/'; | |
| 105 List<String> args = [inFilePath, | |
| 106 "--out=" + outFilePath, | |
| 107 "--package-root=" + packageRoot]; | |
| 108 return launchDart2Js(args) | |
| 109 .then(check) | |
| 110 .then((_) { cleanup(); }); | |
| 111 } | |
| 112 | |
| 113 Future testBadHttp(String serverUrl) { | |
| 114 File file = new File(path.join(tempDir.path, "in_bad.dart")); | |
| 115 file.writeAsStringSync(""" | |
| 116 import '$serverUrl/not_existing.dart'; | |
| 117 main() { print(foo()); } | |
| 118 """); | |
| 119 String inFilePath = file.path; | |
| 120 List<String> args = [inFilePath, "--out=" + outFilePath]; | |
| 121 return launchDart2Js(args) | |
| 122 .then((pr) => checkNotFound(pr, "not_existing.dart")) | |
| 123 .whenComplete(file.deleteSync) | |
| 124 .then((_) { cleanup(); }); | |
| 125 } | |
| 126 | |
| 127 Future testBadHttp2(String serverUrl) { | |
| 128 String inFilePath = '$serverUrl/not_found.dart'; | |
| 129 List<String> args = [inFilePath, "--out=" + outFilePath]; | |
| 130 return launchDart2Js(args) | |
| 131 .then((processResult) => checkNotFound(processResult, "not_found.dart")) | |
| 132 .then((_) { cleanup(); }); | |
| 133 } | |
| 134 | |
| 135 serverRunning(HttpServer server, String scheme) { | |
| 136 tempDir = Directory.systemTemp.createTempSync('directory_test'); | |
| 137 outFilePath = path.join(tempDir.path, "out.js"); | |
| 138 int port = server.port; | |
| 139 String serverUrl = "$scheme://127.0.0.1:$port"; | |
| 140 | |
| 141 asyncStart(); | |
| 142 server.listen(handleRequest); | |
| 143 return new Future.value() | |
| 144 .then((_) => cleanup()) // Make sure we start fresh. | |
| 145 .then((_) => testNonHttp()) | |
| 146 .then((_) => testHttpMain(serverUrl)) | |
| 147 .then((_) => testHttpLib(serverUrl)) | |
| 148 .then((_) => testHttpPackage(serverUrl)) | |
| 149 .then((_) => testBadHttp(serverUrl)) | |
| 150 .then((_) => testBadHttp2(serverUrl)) | |
| 151 .whenComplete(() => tempDir.delete(recursive: true)) | |
| 152 .whenComplete(server.close) | |
| 153 .then((_) => asyncEnd()); | |
| 154 } | |
| 155 | |
| 156 Future testHttp() { | |
| 157 return HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0) | |
| 158 .then((HttpServer server) => serverRunning(server, "http")); | |
| 159 } | |
| 160 | |
| 161 void initializeSSL() { | |
| 162 Uri pathOfPkcert = pathOfData.resolve('pkcert'); | |
| 163 String testPkcertDatabase = pathOfPkcert.toFilePath(); | |
| 164 SecureSocket.initialize(database: testPkcertDatabase, | |
| 165 password: 'dartdart'); | |
| 166 } | |
| 167 | |
| 168 Future testHttps() { | |
| 169 initializeSSL(); | |
| 170 return HttpServer.bindSecure(InternetAddress.LOOPBACK_IP_V4, | |
| 171 0, | |
| 172 certificateName: 'localhost_cert') | |
| 173 .then((HttpServer server) => serverRunning(server, "https")); | |
| 174 } | |
| 175 | |
| 176 main() { | |
| 177 asyncStart(); | |
| 178 testHttp() | |
| 179 .then((_) => testHttps) | |
| 180 .whenComplete(asyncEnd); | |
| 181 } | |
| OLD | NEW |