-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from pedrox-hs/improvement/shic/coverage
Increase code coverage
- Loading branch information
Showing
4 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
); | ||
}); | ||
} |