OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Test: | |
6 // *) Launching 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:expect/expect.dart"; | |
15 | |
16 | |
17 String pathToExecutable = new Options().executable; | |
18 String pathOfData = new File(new Options().script).directory.path + | |
19 '/http_launch_data'; | |
20 int port; | |
21 | |
22 _sendNotFound(HttpResponse response) { | |
23 response.statusCode = HttpStatus.NOT_FOUND; | |
24 response.close(); | |
25 } | |
26 | |
27 handleRequest(HttpRequest request) { | |
28 final String path = request.uri.path; | |
29 final String requestPath = '$pathOfData$path'; | |
30 final File file = new File(requestPath); | |
31 file.exists().then((bool found) { | |
32 if (found) { | |
33 file.fullPath().then((String fullPath) { | |
Søren Gjesse
2013/05/30 09:13:03
Why are you calling fullPath here? Seems that you
Cutch
2013/05/30 13:44:09
Good catch. I had copied this from an article on u
| |
34 file.openRead() | |
35 .pipe(request.response) | |
36 .catchError((e) { _sendNotFound(request.response); }); | |
37 }); | |
38 } else { | |
39 _sendNotFound(request.response); | |
40 } | |
41 }); | |
42 } | |
43 | |
44 serverRunning(HttpServer server) { | |
45 port = server.port; | |
46 server.listen(handleRequest); | |
47 Future<ProcessResult> no_http_run = | |
48 Process.run(pathToExecutable, ['${pathOfData}/http_launch_main.dart']); | |
49 Future<ProcessResult> http_run = | |
50 Process.run(pathToExecutable, | |
51 ['http://127.0.0.1:$port/http_launch_main.dart']); | |
52 Future<ProcessResult> http_pkg_root_run = | |
53 Process.run(pathToExecutable, | |
54 ['--package-root=http://127.0.0.1:$port/packages/', | |
55 'http://127.0.0.1:$port/http_launch_main.dart']); | |
56 Future<List<ProcessResult>> results = Future.wait([no_http_run, http_run, | |
57 http_pkg_root_run]); | |
58 results.then((results) { | |
59 // Close server. | |
60 server.close(); | |
61 // Check results. | |
62 checkResults(results); | |
63 }); | |
64 } | |
65 | |
66 checkResults(List<ProcessResult> results) { | |
67 Expect.equals(3, results.length); | |
68 // Exited cleanly. | |
69 for (int i = 0; i < results.length; i++) { | |
70 ProcessResult result = results[i]; | |
71 Expect.equals(0, result.exitCode); | |
72 } | |
73 String stdout = results[0].stdout; | |
74 // Output is the string 'hello'. Use startsWith to avoid new line differences. | |
75 Expect.isTrue(stdout.startsWith('hello')); | |
76 // Same output from all three process runs. | |
77 for (int i = 0; i < results.length; i++) { | |
78 Expect.equals(stdout, results[i].stdout); | |
79 } | |
80 } | |
81 | |
82 main() { | |
83 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0).then(serverRunning); | |
84 } | |
OLD | NEW |