// Copyright 2013 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:async'; import 'dart:io'; import 'package:mockito/mockito.dart'; class TestHttpOverrides extends HttpOverrides { @override HttpClient createHttpClient(SecurityContext? context) { return createMockImageHttpClient(context); } } MockHttpClient createMockImageHttpClient(SecurityContext? _) { final MockHttpClient client = MockHttpClient(); final MockHttpClientRequest request = MockHttpClientRequest(); final MockHttpClientResponse response = MockHttpClientResponse(); final MockHttpHeaders headers = MockHttpHeaders(); final List transparentImage = getTestImageData(); when(client.getUrl(any)).thenAnswer((_) => Future.value(request)); when(request.headers).thenReturn(headers); when(request.close()).thenAnswer((_) => Future.value(response)); when(client.autoUncompress = any).thenAnswer((_) => null); when(response.contentLength).thenReturn(transparentImage.length); when(response.statusCode).thenReturn(HttpStatus.ok); when(response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); // Define an image stream that streams the mock test image for all // image tests that request an image. StreamSubscription> imageStream(Invocation invocation) { final void Function(List)? onData = invocation.positionalArguments[0] as void Function(List)?; final void Function()? onDone = invocation.namedArguments[#onDone] as void Function()?; final void Function(Object, [StackTrace?])? onError = invocation.namedArguments[#onError] as void Function(Object, [StackTrace?])?; final bool? cancelOnError = invocation.namedArguments[#cancelOnError] as bool?; return Stream>.fromIterable(>[transparentImage]).listen( onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError, ); } when(response.listen(any, onError: anyNamed('onError'), onDone: anyNamed('onDone'), cancelOnError: anyNamed('cancelOnError'))) .thenAnswer(imageStream); return client; } // A list of integers that can be consumed as image data in a stream. final List _transparentImage = [ // Image bytes. 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, ]; List getTestImageData() { return _transparentImage; } /// Define the "fake" data types to be used in mock data type definitions. These /// fake data types are important in the definition of the return values of the /// properties and methods of the mock data types for null safety. // ignore: avoid_implementing_value_types class _FakeDuration extends Fake implements Duration {} class _FakeHttpClientRequest extends Fake implements HttpClientRequest {} class _FakeUri extends Fake implements Uri {} class _FakeHttpHeaders extends Fake implements HttpHeaders {} class _FakeHttpClientResponse extends Fake implements HttpClientResponse {} class _FakeSocket extends Fake implements Socket {} class _FakeStreamSubscription extends Fake implements StreamSubscription {} /// A class which mocks [HttpClient]. /// /// See the documentation for Mockito's code generation for more information. class MockHttpClient extends Mock implements HttpClient { MockHttpClient() { throwOnMissingStub(this); } @override Duration get idleTimeout => super.noSuchMethod(Invocation.getter(#idleTimeout), returnValue: _FakeDuration()) as Duration; @override set idleTimeout(Duration? idleTimeout) => super.noSuchMethod(Invocation.setter(#idleTimeout, idleTimeout)); @override bool get autoUncompress => super.noSuchMethod(Invocation.getter(#autoUncompress), returnValue: false) as bool; @override set autoUncompress(bool? autoUncompress) => super.noSuchMethod(Invocation.setter(#autoUncompress, autoUncompress)); @override Future open(String? method, String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#open, [method, host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future openUrl(String? method, Uri? url) => super.noSuchMethod(Invocation.method(#openUrl, [method, url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future get(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#get, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future getUrl(Uri? url) => super.noSuchMethod(Invocation.method(#getUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future post(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#post, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future postUrl(Uri? url) => super.noSuchMethod(Invocation.method(#postUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future put(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#put, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future putUrl(Uri? url) => super.noSuchMethod(Invocation.method(#putUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future delete(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#delete, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future deleteUrl(Uri? url) => super.noSuchMethod(Invocation.method(#deleteUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future patch(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#patch, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future patchUrl(Uri? url) => super.noSuchMethod(Invocation.method(#patchUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future head(String? host, int? port, String? path) => super.noSuchMethod(Invocation.method(#head, [host, port, path]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override Future headUrl(Uri? url) => super.noSuchMethod(Invocation.method(#headUrl, [url]), returnValue: Future<_FakeHttpClientRequest>.value(_FakeHttpClientRequest())) as Future; @override void addCredentials(Uri? url, String? realm, HttpClientCredentials? credentials) => super.noSuchMethod(Invocation.method(#addCredentials, [url, realm, credentials])); @override void addProxyCredentials(String? host, int? port, String? realm, HttpClientCredentials? credentials) => super.noSuchMethod(Invocation.method(#addProxyCredentials, [host, port, realm, credentials])); @override void close({bool? force = false}) => super.noSuchMethod(Invocation.method(#close, [], {#force: force})); } /// A class which mocks [HttpClientRequest]. /// /// See the documentation for Mockito's code generation for more information. class MockHttpClientRequest extends Mock implements HttpClientRequest { MockHttpClientRequest() { throwOnMissingStub(this); } @override bool get persistentConnection => super.noSuchMethod(Invocation.getter(#persistentConnection), returnValue: false) as bool; @override set persistentConnection(bool? persistentConnection) => super.noSuchMethod(Invocation.setter(#persistentConnection, persistentConnection)); @override bool get followRedirects => super.noSuchMethod(Invocation.getter(#followRedirects), returnValue: false) as bool; @override set followRedirects(bool? followRedirects) => super.noSuchMethod(Invocation.setter(#followRedirects, followRedirects)); @override int get maxRedirects => super.noSuchMethod(Invocation.getter(#maxRedirects), returnValue: 0) as int; @override set maxRedirects(int? maxRedirects) => super.noSuchMethod(Invocation.setter(#maxRedirects, maxRedirects)); @override int get contentLength => super.noSuchMethod(Invocation.getter(#contentLength), returnValue: 0) as int; @override set contentLength(int? contentLength) => super.noSuchMethod(Invocation.setter(#contentLength, contentLength)); @override bool get bufferOutput => super.noSuchMethod(Invocation.getter(#bufferOutput), returnValue: false) as bool; @override set bufferOutput(bool? bufferOutput) => super.noSuchMethod(Invocation.setter(#bufferOutput, bufferOutput)); @override String get method => super.noSuchMethod(Invocation.getter(#method), returnValue: '') as String; @override Uri get uri => super.noSuchMethod(Invocation.getter(#uri), returnValue: _FakeUri()) as Uri; @override HttpHeaders get headers => super.noSuchMethod(Invocation.getter(#headers), returnValue: _FakeHttpHeaders()) as HttpHeaders; @override List get cookies => super.noSuchMethod(Invocation.getter(#cookies), returnValue: []) as List; @override Future get done => super.noSuchMethod(Invocation.getter(#done), returnValue: Future<_FakeHttpClientResponse>.value(_FakeHttpClientResponse())) as Future; @override Future close() => super.noSuchMethod(Invocation.method(#close, []), returnValue: Future<_FakeHttpClientResponse>.value(_FakeHttpClientResponse())) as Future; } /// A class which mocks [HttpClientResponse]. /// /// See the documentation for Mockito's code generation for more information. class MockHttpClientResponse extends Mock implements HttpClientResponse { MockHttpClientResponse() { throwOnMissingStub(this); } // Include an override method for the inherited listen method. This method // intercepts HttpClientResponse listen calls to return a mock image. @override StreamSubscription> listen(void Function(List event)? onData, {Function? onError, void Function()? onDone, bool? cancelOnError}) => super.noSuchMethod( Invocation.method( #listen, [onData], {#onError: onError, #onDone: onDone, #cancelOnError: cancelOnError}, ), returnValue: _FakeStreamSubscription>()) as StreamSubscription>; @override int get statusCode => super.noSuchMethod(Invocation.getter(#statusCode), returnValue: 0) as int; @override String get reasonPhrase => super.noSuchMethod(Invocation.getter(#reasonPhrase), returnValue: '') as String; @override int get contentLength => super.noSuchMethod(Invocation.getter(#contentLength), returnValue: 0) as int; @override HttpClientResponseCompressionState get compressionState => super.noSuchMethod(Invocation.getter(#compressionState), returnValue: HttpClientResponseCompressionState.notCompressed) as HttpClientResponseCompressionState; @override bool get persistentConnection => super.noSuchMethod(Invocation.getter(#persistentConnection), returnValue: false) as bool; @override bool get isRedirect => super.noSuchMethod(Invocation.getter(#isRedirect), returnValue: false) as bool; @override List get redirects => super.noSuchMethod(Invocation.getter(#redirects), returnValue: []) as List; @override HttpHeaders get headers => super.noSuchMethod(Invocation.getter(#headers), returnValue: _FakeHttpHeaders()) as HttpHeaders; @override List get cookies => super.noSuchMethod(Invocation.getter(#cookies), returnValue: []) as List; @override Future redirect([String? method, Uri? url, bool? followLoops]) => super.noSuchMethod(Invocation.method(#redirect, [method, url, followLoops]), returnValue: Future<_FakeHttpClientResponse>.value(_FakeHttpClientResponse())) as Future; @override Future detachSocket() => super.noSuchMethod(Invocation.method(#detachSocket, []), returnValue: Future<_FakeSocket>.value(_FakeSocket())) as Future; } /// A class which mocks [HttpHeaders]. /// /// See the documentation for Mockito's code generation for more information. class MockHttpHeaders extends Mock implements HttpHeaders { MockHttpHeaders() { throwOnMissingStub(this); } @override int get contentLength => super.noSuchMethod(Invocation.getter(#contentLength), returnValue: 0) as int; @override set contentLength(int? contentLength) => super.noSuchMethod(Invocation.setter(#contentLength, contentLength)); @override bool get persistentConnection => super.noSuchMethod(Invocation.getter(#persistentConnection), returnValue: false) as bool; @override set persistentConnection(bool? persistentConnection) => super.noSuchMethod(Invocation.setter(#persistentConnection, persistentConnection)); @override bool get chunkedTransferEncoding => super.noSuchMethod(Invocation.getter(#chunkedTransferEncoding), returnValue: false) as bool; @override set chunkedTransferEncoding(bool? chunkedTransferEncoding) => super.noSuchMethod(Invocation.setter(#chunkedTransferEncoding, chunkedTransferEncoding)); @override List? operator [](String? name) => super.noSuchMethod(Invocation.method(#[], [name])) as List?; @override String? value(String? name) => super.noSuchMethod(Invocation.method(#value, [name])) as String?; @override void add(String? name, Object? value, {bool? preserveHeaderCase = false}) => super.noSuchMethod( Invocation.method(#add, [name, value], {#preserveHeaderCase: preserveHeaderCase})); @override void set(String? name, Object? value, {bool? preserveHeaderCase = false}) => super.noSuchMethod( Invocation.method(#set, [name, value], {#preserveHeaderCase: preserveHeaderCase})); @override void remove(String? name, Object? value) => super.noSuchMethod(Invocation.method(#remove, [name, value])); @override void removeAll(String? name) => super.noSuchMethod(Invocation.method(#removeAll, [name])); @override void forEach(void Function(String, List)? action) => super.noSuchMethod(Invocation.method(#forEach, [action])); @override void noFolding(String? name) => super.noSuchMethod(Invocation.method(#noFolding, [name])); }