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 // | |
Søren Gjesse
2013/06/03 09:23:49
With dart:io being used for the HTTP client we sho
Cutch
2013/06/03 17:55:37
Done.
| |
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 // *) Spawning a URI over HTTP. | |
10 | |
11 library http_launch_test; | |
12 | |
13 import 'dart:async'; | |
14 import 'dart:io'; | |
15 import 'package:expect/expect.dart'; | |
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.openRead() | |
34 .pipe(request.response) | |
35 .catchError((e) { _sendNotFound(request.response); }); | |
36 } else { | |
37 _sendNotFound(request.response); | |
38 } | |
39 }); | |
40 } | |
41 | |
42 serverRunning(HttpServer server) { | |
43 port = server.port; | |
44 server.listen(handleRequest); | |
45 Future<ProcessResult> no_http_run = | |
46 Process.run(pathToExecutable, ['${pathOfData}/http_launch_main.dart']); | |
47 Future<ProcessResult> http_run = | |
48 Process.run(pathToExecutable, | |
49 ['http://127.0.0.1:$port/http_launch_main.dart']); | |
50 Future<ProcessResult> http_pkg_root_run = | |
51 Process.run(pathToExecutable, | |
52 ['--package-root=http://127.0.0.1:$port/packages/', | |
53 'http://127.0.0.1:$port/http_launch_main.dart']); | |
54 Future<ProcessResult> isolate_run = | |
55 Process.run(pathToExecutable, | |
56 ['http://127.0.0.1:$port/http_spawn_main.dart', '$port']); | |
57 Future<List<ProcessResult>> results = Future.wait([no_http_run, http_run, | |
58 http_pkg_root_run, | |
59 isolate_run]); | |
60 results.then((results) { | |
61 // Close server. | |
62 server.close(); | |
63 // Check results. | |
64 checkResults(results); | |
65 }); | |
66 } | |
67 | |
68 checkResults(List<ProcessResult> results) { | |
69 Expect.equals(4, results.length); | |
70 // Exited cleanly. | |
71 for (int i = 0; i < results.length; i++) { | |
72 ProcessResult result = results[i]; | |
73 Expect.equals(0, result.exitCode); | |
74 } | |
75 String stdout = results[0].stdout; | |
76 // Output is the string 'hello'. Use startsWith to avoid new line differences. | |
77 Expect.isTrue(stdout.startsWith('hello')); | |
78 // Same output from all three process runs. | |
79 for (int i = 0; i < results.length; i++) { | |
80 Expect.equals(stdout, results[i].stdout); | |
81 } | |
82 } | |
83 | |
84 main() { | |
85 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 0).then(serverRunning); | |
86 } | |
OLD | NEW |