Skip to content

Commit

Permalink
Optimize SyncTransformer.transformResponse (#1924)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->

- Optimize the `SyncTransformer.transformResponse` method and it‘s test.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [ ] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [ ] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

<!-- Provide more context and info about the PR. -->
  • Loading branch information
hgraceb authored Aug 7, 2023
1 parent b5b0d43 commit 8039ca3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dio/lib/src/transformers/sync_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SyncTransformer extends Transformer {
) async {
final responseType = options.responseType;
// Do not handled the body for streams.
if (options.responseType == ResponseType.stream) {
if (responseType == ResponseType.stream) {
return responseBody;
}

Expand Down
39 changes: 22 additions & 17 deletions dio/test/transformer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@ void main() {
// Regression: https://github.com/cfug/dio/issues/1834
test('null response body only when the response is JSON', () async {
final transformer = BackgroundTransformer();
final r1 = await transformer.transformResponse(
RequestOptions(responseType: ResponseType.json),
ResponseBody.fromBytes([], 200),
);
expect(r1, '');
final r2 = await transformer.transformResponse(
RequestOptions(responseType: ResponseType.bytes),
ResponseBody.fromBytes([], 200),
);
expect(r2, []);
final r3 = await transformer.transformResponse(
RequestOptions(responseType: ResponseType.plain),
ResponseBody.fromBytes([], 200),
);
expect(r3, '');
final r4 = await transformer.transformResponse(
for (final responseType in ResponseType.values) {
final response = await transformer.transformResponse(
RequestOptions(responseType: responseType),
ResponseBody.fromBytes([], 200),
);
switch (responseType) {
case ResponseType.json:
case ResponseType.plain:
expect(response, '');
break;
case ResponseType.stream:
expect(response, isA<ResponseBody>());
break;
case ResponseType.bytes:
expect(response, []);
break;
default:
throw AssertionError('Unknown response type: $responseType');
}
}
final jsonResponse = await transformer.transformResponse(
RequestOptions(responseType: ResponseType.json),
ResponseBody.fromBytes(
[],
Expand All @@ -47,6 +52,6 @@ void main() {
},
),
);
expect(r4, null);
expect(jsonResponse, null);
});
}

0 comments on commit 8039ca3

Please sign in to comment.