Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
b2a27c10
Unverified
Commit
b2a27c10
authored
Jan 31, 2020
by
Dan Field
Committed by
GitHub
Jan 31, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Be clearer about when and why we override HttpClient in tests (#49844)
parent
effc0190
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
0 deletions
+73
-0
test.dart
dev/bots/test.dart
+24
-0
_binding_io.dart
packages/flutter_test/lib/src/_binding_io.dart
+16
-0
binding.dart
packages/flutter_test/lib/src/binding.dart
+7
-0
bindings_test_failure.dart
packages/flutter_test/test/bindings_test_failure.dart
+26
-0
No files found.
dev/bots/test.dart
View file @
b2a27c10
...
...
@@ -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
>{
...
...
packages/flutter_test/lib/src/_binding_io.dart
View file @
b2a27c10
...
...
@@ -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
();
}
}
...
...
packages/flutter_test/lib/src/binding.dart
View file @
b2a27c10
...
...
@@ -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
,
...
...
packages/flutter_test/test/bindings_test_failure.dart
0 → 100644
View file @
b2a27c10
// 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'
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment