diff --git a/plugins/http_compatibility_layer/pubspec.yaml b/plugins/http_compatibility_layer/pubspec.yaml index e10e81af8..79a196134 100644 --- a/plugins/http_compatibility_layer/pubspec.yaml +++ b/plugins/http_compatibility_layer/pubspec.yaml @@ -21,3 +21,4 @@ dependencies: dev_dependencies: lints: any + test: any diff --git a/plugins/http_compatibility_layer/test/client_mock.dart b/plugins/http_compatibility_layer/test/client_mock.dart new file mode 100644 index 000000000..e06b54563 --- /dev/null +++ b/plugins/http_compatibility_layer/test/client_mock.dart @@ -0,0 +1,32 @@ +import 'package:http/http.dart'; + +class CloseClientMock implements Client { + bool closeWasCalled = false; + + @override + void close() { + closeWasCalled = true; + } + + @override + void noSuchMethod(Invocation invocation) { + throw UnimplementedError(); + } +} + +class ClientMock implements Client { + StreamedResponse? response; + + BaseRequest? request; + + @override + Future send(BaseRequest request) async { + this.request = request; + return response!; + } + + @override + void noSuchMethod(Invocation invocation) { + throw UnimplementedError(); + } +} diff --git a/plugins/http_compatibility_layer/test/conversion_layer_adapter_test.dart b/plugins/http_compatibility_layer/test/conversion_layer_adapter_test.dart new file mode 100644 index 000000000..86f30d01f --- /dev/null +++ b/plugins/http_compatibility_layer/test/conversion_layer_adapter_test.dart @@ -0,0 +1,63 @@ +import 'dart:typed_data'; + +import 'package:dio/dio.dart'; +import 'package:dio_http_compatibility_layer/dio_http_compatibility_layer.dart'; +import 'package:http/http.dart'; +import 'package:test/test.dart'; + +import 'client_mock.dart'; + +void main() { + test('close', () { + final mock = CloseClientMock(); + final cla = ConversionLayerAdapter(mock); + + cla.close(); + + expect(mock.closeWasCalled, true); + }); + + test('close with force', () { + final mock = CloseClientMock(); + final cla = ConversionLayerAdapter(mock); + + cla.close(force: true); + + expect(mock.closeWasCalled, true); + }); + + test('headers', () async { + final mock = ClientMock()..response = StreamedResponse(Stream.empty(), 200); + final cla = ConversionLayerAdapter(mock); + + await cla.fetch( + RequestOptions(path: '', headers: {'foo': 'bar'}), + Stream.empty(), + null, + ); + + expect(mock.request?.headers, {'foo': 'bar'}); + }); + + test('download stream', () async { + final mock = ClientMock() + ..response = StreamedResponse( + Stream.fromIterable([ + Uint8List.fromList([10, 1]), + Uint8List.fromList([1, 4]), + Uint8List.fromList([5, 1]), + Uint8List.fromList([1, 1]), + Uint8List.fromList([2, 4]), + ]), + 200); + final cla = ConversionLayerAdapter(mock); + + final resp = await cla.fetch( + RequestOptions(path: ''), + null, + null, + ); + + expect(await resp.stream.length, 5); + }); +}