|
| 1 | +// Copyright 2021 The Flutter 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 | +// @dart = 2.9 |
| 6 | + |
| 7 | +// FlutterTesterOptions=--disallow-insecure-connections |
| 8 | + |
| 9 | +import 'dart:async'; |
| 10 | +import 'dart:io'; |
| 11 | + |
| 12 | +import 'package:litetest/litetest.dart'; |
| 13 | + |
| 14 | +/// Asserts that `callback` throws an exception of type `T`. |
| 15 | +Future<void> asyncExpectThrows<T>(Function callback) async { |
| 16 | + bool threw = false; |
| 17 | + try { |
| 18 | + await callback(); |
| 19 | + } catch (e) { |
| 20 | + expect(e is T, true); |
| 21 | + threw = true; |
| 22 | + } |
| 23 | + expect(threw, true); |
| 24 | +} |
| 25 | + |
| 26 | +Future<String> getLocalHostIP() async { |
| 27 | + final List<NetworkInterface> interfaces = await NetworkInterface.list( |
| 28 | + includeLoopback: false, type: InternetAddressType.IPv4); |
| 29 | + return interfaces.first.addresses.first.address; |
| 30 | +} |
| 31 | + |
| 32 | +Future<void> bindServerAndTest(String serverHost, |
| 33 | + Future<void> Function(HttpClient client, Uri uri) testCode) async { |
| 34 | + final HttpClient httpClient = HttpClient(); |
| 35 | + final HttpServer server = await HttpServer.bind(serverHost, 0); |
| 36 | + final Uri uri = Uri(scheme: 'http', host: serverHost, port: server.port); |
| 37 | + try { |
| 38 | + await testCode(httpClient, uri); |
| 39 | + } finally { |
| 40 | + httpClient.close(force: true); |
| 41 | + await server.close(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Answers the question whether this computer supports binding to IPv6 addresses. |
| 46 | +Future<bool> _supportsIPv6() async { |
| 47 | + try { |
| 48 | + final ServerSocket socket = await ServerSocket.bind(InternetAddress.loopbackIPv6, 0); |
| 49 | + await socket.close(); |
| 50 | + return true; |
| 51 | + } on SocketException catch (_) { |
| 52 | + return false; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void main() { |
| 57 | + test('testWithHostname', () async { |
| 58 | + await bindServerAndTest(await getLocalHostIP(), (HttpClient httpClient, Uri httpUri) async { |
| 59 | + asyncExpectThrows<UnsupportedError>( |
| 60 | + () async => httpClient.getUrl(httpUri)); |
| 61 | + asyncExpectThrows<UnsupportedError>( |
| 62 | + () async => runZoned(() => httpClient.getUrl(httpUri), |
| 63 | + zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: 'foo'})); |
| 64 | + asyncExpectThrows<UnsupportedError>( |
| 65 | + () async => runZoned(() => httpClient.getUrl(httpUri), |
| 66 | + zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: false})); |
| 67 | + await runZoned(() => httpClient.getUrl(httpUri), |
| 68 | + zoneValues: <dynamic, dynamic>{#flutter.io.allow_http: true}); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + test('testWithLoopback', () async { |
| 73 | + await bindServerAndTest('127.0.0.1', (HttpClient httpClient, Uri uri) async { |
| 74 | + await httpClient.getUrl(Uri.parse('http://localhost:${uri.port}')); |
| 75 | + await httpClient.getUrl(Uri.parse('http://127.0.0.1:${uri.port}')); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + test('testWithIPV6', () async { |
| 80 | + if (await _supportsIPv6()) { |
| 81 | + await bindServerAndTest('::1', (HttpClient httpClient, Uri uri) async { |
| 82 | + await httpClient.getUrl(uri); |
| 83 | + }); |
| 84 | + } |
| 85 | + }); |
| 86 | +} |
0 commit comments