Skip to content

Commit

Permalink
✅ Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Dec 2, 2023
1 parent 37d0d70 commit 7b8af43
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/http_compatibility_layer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ dependencies:

dev_dependencies:
lints: any
test: any
32 changes: 32 additions & 0 deletions plugins/http_compatibility_layer/test/client_mock.dart
Original file line number Diff line number Diff line change
@@ -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<StreamedResponse> send(BaseRequest request) async {
this.request = request;
return response!;
}

@override
void noSuchMethod(Invocation invocation) {
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
@@ -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>[
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);
});
}

0 comments on commit 7b8af43

Please sign in to comment.