void main() {
testWidgets('using HttpOverrides', (WidgetTester tester) async {
HttpOverrides.runZoned(() async {
// All code inside here will use the HttpClient returned below.
await tester.pumpWidget(new Image.network('http://www.foobar.com/baz.png'));
}, createHttpClient: createMockImageHttpClient);
});
}
// Returns a mock HTTP client that responds with a blank image to all requests.
MockHttpClient createMockImageHttpClient(SecurityContext _) {
final MockHttpClient client = new MockHttpClient();
final MockHttpClientRequest request = new MockHttpClientRequest();
final MockHttpClientResponse response = new MockHttpClientResponse();
final MockHttpHeaders headers = new MockHttpHeaders();
when(client.getUrl(typed(any))).thenReturn(new Future<HttpClientRequest>.value(request));
when(request.headers).thenReturn(headers);
when(request.close()).thenReturn(new Future<HttpClientResponse>.value(response));
when(response.contentLength).thenReturn(kTransparentImage.length);
when(response.statusCode).thenReturn(HttpStatus.OK);
when(response.listen(typed(any))).thenAnswer((Invocation invocation) {
final void Function(List<int>) onData = invocation.positionalArguments[0];
final void Function() onDone = invocation.namedArguments[#onDone];
final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError];
final bool cancelOnError = invocation.namedArguments[#cancelOnError];
return new Stream<List<int>>.fromIterable(<List<int>>[kTransparentImage])
.listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
});
return client;
}
class MockHttpClient extends Mock implements HttpClient {}
class MockHttpClientRequest extends Mock implements HttpClientRequest {}
class MockHttpClientResponse extends Mock implements HttpClientResponse {}
class MockHttpHeaders extends Mock implements HttpHeaders {}
--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Ian Hickson
😸