Be clearer about when and why we override HttpClient in tests (#49844)
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user