Unverified Commit b2a27c10 authored by Dan Field's avatar Dan Field Committed by GitHub

Be clearer about when and why we override HttpClient in tests (#49844)

parent effc0190
......@@ -466,6 +466,30 @@ Future<void> _runFrameworkTests() async {
'FLUTTER_EXPERIMENTAL_BUILD': 'true',
},
);
const String httpClientWarning =
'Warning: At least one test in this suite creates an HttpClient. When\n'
'running a test suite that uses TestWidgetsFlutterBinding, all HTTP\n'
'requests will return status code 400, and no network request will\n'
'actually be made. Any test expecting an real network connection and\n'
'status code will fail.\n'
'To test code that needs an HttpClient, provide your own HttpClient\n'
'implementation to the code under test, so that your test can\n'
'consistently provide a testable response to the code under test.';
await _runFlutterTest(
path.join(flutterRoot, 'packages', 'flutter_test'),
script: path.join('test', 'bindings_test_failure.dart'),
expectFailure: true,
printOutput: false,
outputChecker: (CapturedOutput output) {
final Iterable<Match> matches = httpClientWarning.allMatches(output.stdout);
if (matches == null || matches.isEmpty || matches.length > 1) {
return 'Failed to print warning about HttpClientUsage, or printed it too many times.\n'
'stdout:\n${output.stdout}';
}
return null;
},
tableData: bigqueryApi?.tabledata,
);
}
await selectSubshard(<String, ShardRunner>{
......
......@@ -11,6 +11,9 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
// ignore: deprecated_member_use
import 'package:test_api/test_api.dart' as test_package;
import 'binding.dart';
......@@ -73,8 +76,21 @@ void mockFlutterAssets() {
/// If another [HttpClient] is provided using [HttpOverrides.runZoned], that will
/// take precedence over this provider.
class _MockHttpOverrides extends HttpOverrides {
bool warningPrinted = false;
@override
HttpClient createHttpClient(SecurityContext _) {
if (!warningPrinted) {
test_package.printOnFailure(
'Warning: At least one test in this suite creates an HttpClient. When\n'
'running a test suite that uses TestWidgetsFlutterBinding, all HTTP\n'
'requests will return status code 400, and no network request will\n'
'actually be made. Any test expecting an real network connection and\n'
'status code will fail.\n'
'To test code that needs an HttpClient, provide your own HttpClient\n'
'implementation to the code under test, so that your test can\n'
'consistently provide a testable response to the code under test.');
warningPrinted = true;
}
return _MockHttpClient();
}
}
......
......@@ -147,6 +147,13 @@ class TestDefaultBinaryMessenger extends BinaryMessenger {
///
/// When using these bindings, certain features are disabled. For
/// example, [timeDilation] is reset to 1.0 on initialization.
///
/// In non-browser tests, the binding overrides `HttpClient` creation with a
/// fake client that always returns a status code of 400. This is to prevent
/// tests from making network calls, which could introduce flakiness. A test
/// that actually needs to make a network call should provide its own
/// `HttpClient` to the code making the call, so that it can appropriately mock
/// or fake responses.
abstract class TestWidgetsFlutterBinding extends BindingBase
with ServicesBinding,
SchedulerBinding,
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
// This test checks that we output something on test failure where the test
// creates an HttpClient.
// It should not be run as part of a normal test suite, as it is expected to
// fail. See dev/bots/test.dart, which runs this test, checks that it fails,
// and prints the expected warning about HttpClient usage.
// We don't run this for browser tests, since we don't override the behavior
// in browser tests.
void main() {
test('Http Warning Message', () {
TestWidgetsFlutterBinding.ensureInitialized();
HttpClient();
// Make sure we only add the message once.
HttpClient();
HttpClient();
fail('Intentional');
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment