Skip to content

Commit

Permalink
Add missing client.openUrl mock based on mocktail implementation
Browse files Browse the repository at this point in the history
gmeral committed Nov 20, 2024

Unverified

This user has not yet uploaded their public signing key.
1 parent 8b65a14 commit 836ef64
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/apptive_grid_heinzelmen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ dependencies:
apptive_grid_theme: ^1.3.0
flutter:
sdk: flutter
flutter_svg: ^2.0.7
flutter_svg: ^2.0.14
http: '>=0.13.0 <2.0.0'
intl: ^0.19.0
provider: ^6.0.5
Original file line number Diff line number Diff line change
@@ -170,6 +170,7 @@ class _MockHttpClient extends Mock implements HttpClient {
_MockHttpClient() {
registerFallbackValue((List<int> _) {});
registerFallbackValue(Uri());
registerFallbackValue(Stream<List<int>>.fromIterable([]));
}

@override
@@ -221,5 +222,71 @@ HttpClient _createHttpClient(Map<Uri, Uint8List>? images) {

return request;
});

HttpClientResponse _createResponse(Uri uri) {
final response = _MockHttpClientResponse();
final headers = _MockHttpHeaders();
final data = images?[uri] ?? fallbackImage;

when(() => response.headers).thenReturn(headers);
when(() => response.contentLength).thenReturn(data.length);
when(() => response.statusCode).thenReturn(HttpStatus.ok);
when(() => response.isRedirect).thenReturn(false);
when(() => response.redirects).thenReturn([]);
when(() => response.persistentConnection).thenReturn(false);
when(() => response.reasonPhrase).thenReturn('OK');
when(
() => response.compressionState,
).thenReturn(HttpClientResponseCompressionState.notCompressed);
when(
() => response.handleError(any(), test: any(named: 'test')),
).thenAnswer((_) => Stream<List<int>>.value(data));
when(
() => response.listen(
any(),
onDone: any(named: 'onDone'),
onError: any(named: 'onError'),
cancelOnError: any(named: 'cancelOnError'),
),
).thenAnswer((invocation) {
final onData =
invocation.positionalArguments.first as void Function(List<int>);
final onDone = invocation.namedArguments[#onDone] as void Function()?;
return Stream<List<int>>.fromIterable(
<List<int>>[data],
).listen(onData, onDone: onDone);
});
return response;
}

HttpClientRequest _createRequest(Uri uri) {
final request = _MockHttpClientRequest();
final headers = _MockHttpHeaders();

when(() => request.headers).thenReturn(headers);
when(
() => request.addStream(any()),
).thenAnswer((invocation) {
final stream = invocation.positionalArguments.first as Stream<List<int>>;
return stream.fold<List<int>>(
<int>[],
(previous, element) => previous..addAll(element),
);
});
when(
request.close,
).thenAnswer((_) async => _createResponse(uri));

return request;
}



when(() => client.openUrl(any(), any())).thenAnswer(
(invokation) async => _createRequest(
invokation.positionalArguments.last as Uri,
),
);

return client;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 836ef64

Please sign in to comment.