Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix null body converter #623

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,13 @@ class ResponseConverterInterceptor<InnerType> implements InternalInterceptor {
Response response,
ConvertResponse? responseConverter,
) async {
Response? newResponse;
if (responseConverter != null) {
newResponse = await responseConverter(response);
response = await responseConverter(response);
} else if (_converter != null) {
newResponse = await _decodeResponse<BodyType>(response, _converter!);
response = await _decodeResponse<BodyType>(response, _converter!);
}

return Response<BodyType>(
newResponse?.base ?? response.base,
newResponse?.body ?? response.body,
);
return Response<BodyType>(response.base, response.body);
}

/// Converts the [response] using [_converter].
Expand Down
58 changes: 58 additions & 0 deletions chopper/test/chain/response_converter_interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ void main() {
expect(converter.called, 0);
});

test(
'response is successful converter is not null and response converter is null, response is converted with null body',
() async {
final converter = ResponseNullBodyConverter();
interceptorChain = InterceptorChain(
interceptors: [
ResponseConverterInterceptor(converter: converter),
ResponseInterceptor(),
],
request: testRequest,
);

final response = await interceptorChain.proceed(testRequest);

expect(response.body, null);
expect(converter.called, 1);
});

test(
'response is successful converter is not null and response converter is not null, response is converted by response converter with null body',
() async {
final converter = ResponseNullBodyConverter();
interceptorChain = InterceptorChain(
interceptors: [
ResponseConverterInterceptor(
converter: converter,
responseConverter: (response) => Response(response.base, null)),
ResponseInterceptor(),
],
request: testRequest,
);

final response = await interceptorChain.proceed(testRequest);

expect(response.body, null);
expect(converter.called, 0);
});

test(
'response is unsuccessful converter is not null and response converter is not null, response is not converted',
() async {
Expand Down Expand Up @@ -153,6 +191,8 @@ void main() {
expect(converter.called, 0);
});
});

group('response converter returns converted response tests', () {});
}

// ignore mutability warning for test class.
Expand All @@ -173,6 +213,24 @@ class ResponseConverter implements Converter {
}
}

// ignore mutability warning for test class.
//ignore: must_be_immutable
class ResponseNullBodyConverter implements Converter {
int called = 0;

@override
FutureOr<Request> convertRequest(Request request) {
return request;
}

@override
FutureOr<Response<BodyType>> convertResponse<BodyType, InnerType>(
Response response) {
called++;
return Response(response.base, null as BodyType);
}
}

// ignore mutability warning for test class.
//ignore: must_be_immutable
class ResponseErrorConverter implements ErrorConverter {
Expand Down