Skip to content

Commit

Permalink
add tests, and conditionally await FutureOr<String?>
Browse files Browse the repository at this point in the history
  • Loading branch information
Reprevise committed Oct 31, 2023
1 parent 59b69b9 commit 2228b04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dio/lib/src/transformers/sync_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,17 @@ class SyncTransformer extends Transformer {
);
final String? response;
if (options.responseDecoder != null) {
response = await options.responseDecoder!(
final decodeResponse = options.responseDecoder!(
responseBytes,
options,
responseBody..stream = Stream.empty(),
);

if (decodeResponse is Future) {
response = await decodeResponse;
} else {
response = decodeResponse;
}
} else if (!isJsonContent || responseBytes.isNotEmpty) {
response = utf8.decode(responseBytes, allowMalformed: true);
} else {
Expand Down
22 changes: 22 additions & 0 deletions dio/test/options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,28 @@ void main() {
expect(response.data, null);
});

test('responseDecoder can return Future<String?>', () async {
final dio = Dio();
dio.options.responseDecoder = (_, __, ___) => Future.value('example');
dio.options.baseUrl = EchoAdapter.mockBase;
dio.httpClientAdapter = EchoAdapter();

final Response response = await dio.get('');

expect(response.data, 'example');
});

test('responseDecoder can return String?', () async {
final dio = Dio();
dio.options.responseDecoder = (_, __, ___) => 'example';
dio.options.baseUrl = EchoAdapter.mockBase;
dio.httpClientAdapter = EchoAdapter();

final Response response = await dio.get('');

expect(response.data, 'example');
});

test('invalid response type throws exceptions', () async {
final dio = Dio(
BaseOptions(
Expand Down

0 comments on commit 2228b04

Please sign in to comment.