|
| 1 | +// Copyright (c) 2025, 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 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:collection/collection.dart'; |
| 8 | +import 'package:logging/logging.dart'; |
| 9 | +import 'package:path/path.dart' as p; |
| 10 | +import 'package:test/test.dart'; |
| 11 | +import 'package:test_descriptor/test_descriptor.dart' as d; |
| 12 | +import 'package:test_process/test_process.dart'; |
| 13 | + |
| 14 | +import 'package:webdev/src/logging.dart'; |
| 15 | +import 'package:webdev/src/serve/utils.dart'; |
| 16 | + |
| 17 | +import 'test_utils.dart'; |
| 18 | + |
| 19 | +void main() { |
| 20 | + group('serve app with TLS options', () { |
| 21 | + // Change to true for debugging. |
| 22 | + final debug = false; |
| 23 | + |
| 24 | + final testRunner = TestRunner(); |
| 25 | + late String exampleDirectory; |
| 26 | + |
| 27 | + setUpAll(() async { |
| 28 | + configureLogWriter(debug); |
| 29 | + await testRunner.setUpAll(); |
| 30 | + exampleDirectory = |
| 31 | + p.absolute(p.join(p.current, '..', 'fixtures', '_experimentSound')); |
| 32 | + |
| 33 | + final process = await TestProcess.start( |
| 34 | + 'dart', |
| 35 | + ['pub', 'upgrade'], |
| 36 | + workingDirectory: exampleDirectory, |
| 37 | + environment: getPubEnvironment(), |
| 38 | + ); |
| 39 | + |
| 40 | + await process.shouldExit(0); |
| 41 | + |
| 42 | + await d |
| 43 | + .file('.dart_tool/package_config.json', isNotEmpty) |
| 44 | + .validate(exampleDirectory); |
| 45 | + await d.file('pubspec.lock', isNotEmpty).validate(exampleDirectory); |
| 46 | + }); |
| 47 | + |
| 48 | + test('listens on a loopback interface', () async { |
| 49 | + final port = await findUnusedPort(); |
| 50 | + final args = [ |
| 51 | + 'serve', |
| 52 | + 'web:$port', |
| 53 | + '--hostname=0.0.0.0', |
| 54 | + '--tls-cert-chain=localhost+2.pem', |
| 55 | + '--tls-cert-key=localhost+2-key.pem', |
| 56 | + ]; |
| 57 | + |
| 58 | + final process = |
| 59 | + await testRunner.runWebDev(args, workingDirectory: exampleDirectory); |
| 60 | + await expectLater(process.stdout, emitsThrough(contains('Succeeded'))); |
| 61 | + |
| 62 | + final client = HttpClient() |
| 63 | + ..badCertificateCallback = (_, __, ___) => true; |
| 64 | + try { |
| 65 | + final request = |
| 66 | + await client.getUrl(Uri.parse('https://localhost:$port')); |
| 67 | + final response = await request.close(); |
| 68 | + expect(response.statusCode, equals(200)); |
| 69 | + } finally { |
| 70 | + client.close(force: true); |
| 71 | + } |
| 72 | + |
| 73 | + await process.kill(); |
| 74 | + await process.shouldExit(); |
| 75 | + }); |
| 76 | + |
| 77 | + test('listens on a non-loopback interface', () async { |
| 78 | + final port = await findUnusedPort(); |
| 79 | + final args = [ |
| 80 | + 'serve', |
| 81 | + 'web:$port', |
| 82 | + '--hostname=0.0.0.0', |
| 83 | + '--tls-cert-chain=localhost+2.pem', |
| 84 | + '--tls-cert-key=localhost+2-key.pem', |
| 85 | + ]; |
| 86 | + |
| 87 | + final process = |
| 88 | + await testRunner.runWebDev(args, workingDirectory: exampleDirectory); |
| 89 | + await expectLater(process.stdout, emitsThrough(contains('Succeeded'))); |
| 90 | + |
| 91 | + final interfaces = await NetworkInterface.list( |
| 92 | + type: InternetAddressType.IPv4, |
| 93 | + includeLoopback: false, |
| 94 | + ); |
| 95 | + final nonLoopback = interfaces.expand((i) => i.addresses).firstOrNull; |
| 96 | + |
| 97 | + if (nonLoopback == null) { |
| 98 | + Logger.root.info( |
| 99 | + 'No non-loopback IPv4 address available, skipping hostname test.'); |
| 100 | + } else { |
| 101 | + final client = HttpClient() |
| 102 | + ..badCertificateCallback = (_, __, ___) => true; |
| 103 | + try { |
| 104 | + final request = await client |
| 105 | + .getUrl(Uri.parse('https://${nonLoopback.address}:$port')); |
| 106 | + final response = await request.close(); |
| 107 | + expect(response.statusCode, equals(200)); |
| 108 | + } finally { |
| 109 | + client.close(force: true); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + await process.kill(); |
| 114 | + await process.shouldExit(); |
| 115 | + }); |
| 116 | + }); |
| 117 | +} |
0 commit comments