Skip to content

Commit

Permalink
Merge pull request #33 from pedrox-hs/improvement/shic/coverage
Browse files Browse the repository at this point in the history
Increase code coverage
  • Loading branch information
kodiakhq[bot] authored Jan 1, 2024
2 parents 5bd7e9c + 9aec468 commit ba3d486
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 3 deletions.
6 changes: 3 additions & 3 deletions shic/lib/src/ext/client.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:http/http.dart';

extension ClientExt on Client {
Future<StreamedResponse> upload(
Future<Response> upload(
Uri url,
Iterable<MultipartFile> files, {
String method = 'POST',
Map<String, String> fields = const <String, String>{},
}) {
}) async {
final request = MultipartRequest(method, url)
..files.addAll(files)
..fields.addAll(fields);

return send(request);
return Response.fromStream(await send(request));
}
}
8 changes: 8 additions & 0 deletions shic/lib/src/ext/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ extension ResponseExt on Response {
dynamic get bodyJson => json.decode(body);
}

extension StreamedResponseExt on StreamedResponse {
Future<String> get body => toResponse().then((value) => value.body);

Future<dynamic> get bodyJson async => json.decode(await body);

Future<Response> toResponse() => Response.fromStream(this);
}

extension FutureResponseExt on Future<Response> {
Future<Out> mapJsonWith<In, Out>(Out Function(In) fn) =>
then((response) => fn(response.bodyJson));
Expand Down
51 changes: 51 additions & 0 deletions shic/test/src/ext/client_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:mocktail/mocktail.dart';
import 'package:shic/shic.dart';

void main() {
group('ClientExt', () {
late Client sut;

setUpAll(() {
registerFallbackValue(_FakeRequest());
});

setUp(() async {
sut = _MockClient();
});

test('upload should send multipart request correctly', () async {
// arrange
final url = Uri.parse('https://example.com');
final files = [
MultipartFile.fromBytes('foo', [1, 2, 3]),
MultipartFile.fromBytes('bar', [4, 5, 6]),
];
final fields = {
'foo': 'bar',
'baz': 'qux',
};

when(() => sut.send(any())).thenAnswer(
(_) async => StreamedResponse(ByteStream.fromBytes([]), 201),
);

// act
await sut.upload(url, files, fields: fields);

// assert
final verifier = verify(() => sut.send(captureAny()))..called(1);
expect(verifier.captured.first, isA<MultipartRequest>());

final request = verifier.captured.first as MultipartRequest;
expect(request.url, url);
expect(request.files, files);
expect(request.fields, fields);
});
});
}

class _MockClient extends Mock implements Client {}

class _FakeRequest extends Fake implements BaseRequest {}
76 changes: 76 additions & 0 deletions shic/test/src/ext/response_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:shic/shic.dart';

void main() {
group('ResponseExt', () {
test(
'bodyJson should parse json correctly',
() {
// arrange
final expected = {'foo': 'bar'};
final response = Response('{"foo": "bar"}', 200);

// act
final actual = response.bodyJson;

// assert
expect(actual, expected);
},
);
});

group('StreamedResponseExt', () {
test('body should return body as string', () async {
// arrange
final expected = 'Hello, world!';
final response = StreamedResponse(
Stream.value(expected.codeUnits),
200,
);

// act
final actual = await response.body;

// assert
expect(actual, expected);
});

test(
'bodyJson should parse json correctly',
() async {
// arrange
final expected = {'foo': 'bar'};
final response = StreamedResponse(
Stream.value('{"foo": "bar"}'.codeUnits),
200,
);

// act
final actual = await response.bodyJson;

// assert
expect(actual, expected);
},
);
});

group('FutureResponseExt', () {
test(
'mapJsonWith should map json correctly',
() async {
// arrange
final expected = {'foo': 'bar', 'baz': 'qux'};
final response = Future.value(Response('{"foo": "bar"}', 200));

// act
final actual = await response.mapJsonWith(
(Map<String, dynamic> json) => {...json, 'baz': 'qux'},
);

// assert
expect(actual, expected);
},
);
});
}

0 comments on commit ba3d486

Please sign in to comment.