OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:convert'; |
| 7 import 'dart:io'; |
| 8 |
| 9 import "package:args/args.dart"; |
| 10 import "package:path/path.dart" as path; |
| 11 import "package:test/test.dart"; |
| 12 |
| 13 void main(List<String> args) { |
| 14 final ArgParser parser = new ArgParser(); |
| 15 parser.addOption('mojo-shell', |
| 16 help: 'Path to the Mojo shell.'); |
| 17 final ArgResults results = parser.parse(args); |
| 18 final String shellPath = results['mojo-shell']; |
| 19 final List<String> devtoolsFlags = results.rest; |
| 20 |
| 21 HttpServer testServer; |
| 22 String testServerUrl; |
| 23 setUp(() async { |
| 24 testServer = await HttpServer.bind('127.0.0.1', 0); |
| 25 testServer.listen((HttpRequest request) { |
| 26 request.response.write('Hello, world!'); |
| 27 request.response.close(); |
| 28 }); |
| 29 testServerUrl = 'http://127.0.0.1:${testServer.port}/'; |
| 30 }); |
| 31 |
| 32 tearDown(() async { |
| 33 testServer.close(); |
| 34 }); |
| 35 |
| 36 test("Wget .mojo", () async { |
| 37 final String mojoUrl = 'http://core.mojoapps.io/dart_wget.mojo'; |
| 38 final List<String> args = new List.from(devtoolsFlags)..add(mojoUrl); |
| 39 args.add('--args-for=$mojoUrl $testServerUrl'); |
| 40 |
| 41 final ProcessResult result = await Process.run(shellPath, args); |
| 42 expect(result.exitCode, equals(0)); |
| 43 expect(result.stdout.contains('Hello, world!'), isTrue); |
| 44 }); |
| 45 |
| 46 test("Wget .dart", () async { |
| 47 final HttpServer server = await HttpServer.bind('127.0.0.1', 0); |
| 48 final String scriptOrigin = path.normalize( |
| 49 path.join(path.dirname(Platform.script.path), '..', '..')); |
| 50 final String packageOrigin = path.normalize( |
| 51 path.join(path.fromUri(Platform.packageRoot), '..')); |
| 52 |
| 53 server.listen((HttpRequest request) async { |
| 54 final String requestPath = request.uri.toFilePath(); |
| 55 |
| 56 String origin; |
| 57 if (requestPath.startsWith('/packages')) { |
| 58 origin = packageOrigin; |
| 59 } else { |
| 60 origin = scriptOrigin; |
| 61 } |
| 62 |
| 63 final String filePath = |
| 64 path.join(origin, path.relative(requestPath, from: '/'));; |
| 65 final File file = new File(filePath); |
| 66 if (await file.exists()) { |
| 67 try { |
| 68 await file.openRead().pipe(request.response); |
| 69 } catch (e) { |
| 70 print(e); |
| 71 } |
| 72 } else { |
| 73 request.response.statusCode = HttpStatus.NOT_FOUND; |
| 74 request.response.close(); |
| 75 } |
| 76 }); |
| 77 |
| 78 final String dartUrl = |
| 79 'http://127.0.0.1:${server.port}/wget/lib/main.dart'; |
| 80 |
| 81 final List<String> args = new List.from(devtoolsFlags)..add(dartUrl); |
| 82 args.add('--args-for=$dartUrl $testServerUrl'); |
| 83 |
| 84 final ProcessResult result = await Process.run(shellPath, args); |
| 85 expect(result.exitCode, equals(0)); |
| 86 expect(result.stdout.contains('Hello, world!'), isTrue); |
| 87 |
| 88 server.close(); |
| 89 }); |
| 90 } |
OLD | NEW |