OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, 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 // VMOptions= | |
5 // VMOptions=--short_socket_read | |
6 // VMOptions=--short_socket_write | |
7 // VMOptions=--short_socket_read --short_socket_write | |
8 // OtherResources=http_launch_data/http_isolate_main.dart | |
9 // OtherResources=http_launch_data/http_launch_main.dart | |
10 // OtherResources=http_launch_data/http_spawn_main.dart | |
11 // OtherResources=http_launch_data/packages/simple/simple.dart | |
12 // | |
13 // Test: | |
14 // *) Launching a script fetched over HTTP. | |
15 // *) Importing a library fetched over HTTP. | |
16 // *) Automatically resolving package_root when script is fetched over HTTP. | |
17 // *) Spawning a URI over HTTP. | |
18 | |
19 library http_launch_test; | |
20 | |
21 import 'dart:async'; | |
22 import 'dart:io'; | |
23 import 'package:expect/expect.dart'; | |
24 | |
25 String pathToExecutable = Platform.executable; | |
26 Uri pathOfData = Platform.script.resolve('http_launch_data/'); | |
27 int port; | |
28 | |
29 _sendNotFound(HttpResponse response) { | |
30 response.statusCode = HttpStatus.NOT_FOUND; | |
31 response.close(); | |
32 } | |
33 | |
34 handleRequest(HttpRequest request) { | |
35 final String path = request.uri.path.substring(1); | |
36 final Uri requestPath = pathOfData.resolve(path); | |
37 final File file = new File(requestPath.toFilePath()); | |
38 file.exists().then((bool found) { | |
39 if (found) { | |
40 file.openRead().pipe(request.response).catchError((e) { | |
41 _sendNotFound(request.response); | |
42 }); | |
43 } else { | |
44 _sendNotFound(request.response); | |
45 } | |
46 }); | |
47 } | |
48 | |
49 serverRunning(HttpServer server) { | |
50 port = server.port; | |
51 server.listen(handleRequest); | |
52 Future<ProcessResult> no_http_run = Process.run(pathToExecutable, | |
53 [pathOfData.resolve('http_launch_main.dart').toFilePath()]); | |
54 Future<ProcessResult> http_run = Process | |
55 .run(pathToExecutable, ['http://127.0.0.1:$port/http_launch_main.dart']); | |
56 Future<ProcessResult> http_pkg_root_run = Process.run(pathToExecutable, [ | |
57 '--package-root=http://127.0.0.1:$port/packages/', | |
58 'http://127.0.0.1:$port/http_launch_main.dart' | |
59 ]); | |
60 Future<ProcessResult> isolate_run = Process.run(pathToExecutable, | |
61 ['http://127.0.0.1:$port/http_spawn_main.dart', '$port']); | |
62 Future<List<ProcessResult>> results = | |
63 Future.wait([no_http_run, http_run, http_pkg_root_run, isolate_run]); | |
64 results.then((results) { | |
65 // Close server. | |
66 server.close(); | |
67 // Check results. | |
68 checkResults(results); | |
69 }); | |
70 } | |
71 | |
72 checkResults(List<ProcessResult> results) { | |
73 Expect.equals(4, results.length); | |
74 // Exited cleanly. | |
75 for (int i = 0; i < results.length; i++) { | |
76 ProcessResult result = results[i]; | |
77 if (result.exitCode != 0) { | |
78 print("Exit code for process $i = ${result.exitCode}"); | |
79 print("---stdout:---\n${result.stdout}"); | |
80 print("---stderr:---\n${result.stderr}\n---"); | |
81 } | |
82 Expect.equals(0, result.exitCode); | |
83 } | |
84 String stdout = results[0].stdout; | |
85 // Output is the string 'hello'. Use startsWith to avoid new line differences. | |
86 if (!stdout.startsWith('hello')) { | |
87 print("---- stdout of remote process:\n$stdout\n----"); | |
88 } | |
89 Expect.isTrue(stdout.startsWith('hello')); | |
90 // Same output from all three process runs. | |
91 for (int i = 0; i < results.length; i++) { | |
92 Expect.equals(stdout, results[i].stdout); | |
93 } | |
94 } | |
95 | |
96 main() { | |
97 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0).then(serverRunning); | |
98 } | |
OLD | NEW |