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