Skip to content

Commit

Permalink
🐛 Fix null body converter (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guldem authored Jun 25, 2024
1 parent 3482cc6 commit 627ff1d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
10 changes: 3 additions & 7 deletions chopper/lib/src/interceptors/response_converter_interceptor.dart
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

0 comments on commit 627ff1d

Please sign in to comment.