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 ChopperClient.send() sending wrong request when using an Authenticator #497

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,8 @@ base class ChopperClient {
dynamic res = Response(response, response.body);

if (authenticator != null) {
final Request? updatedRequest = await authenticator!.authenticate(
request,
res,
request,
);
final Request? updatedRequest =
await authenticator!.authenticate(req, res, request);

if (updatedRequest != null) {
res = await send<BodyType, InnerType>(
Expand Down
19 changes: 16 additions & 3 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,12 @@ class JsonConverter implements Converter, ErrorConverter {
Request encodeJson(Request request) {
final String? contentType = request.headers[contentTypeKey];

return (contentType?.contains(jsonHeaders) ?? false)
? request.copyWith(body: json.encode(request.body))
: request;
if ((contentType?.contains(jsonHeaders) ?? false) &&
(request.body.runtimeType != String || !isJson(request.body))) {
return request.copyWith(body: json.encode(request.body));
}

return request;
}

FutureOr<Response> decodeJson<BodyType, InnerType>(Response response) async {
Expand Down Expand Up @@ -255,6 +258,16 @@ class JsonConverter implements Converter, ErrorConverter {

static Request requestFactory(Request request) =>
const JsonConverter().convertRequest(request);

@visibleForTesting
static bool isJson(dynamic data) {
try {
json.decode(data);
return true;
} catch (_) {
return false;
}
}
}

/// A [Converter] implementation that converts only [Request]s having a [Map] as their body.
Expand Down
19 changes: 19 additions & 0 deletions chopper/test/converter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ void main() {

expect(converted.body, equals({'foo': 'bar'}));
});

test('JsonConverter.isJson', () {
expect(JsonConverter.isJson('{"foo":"bar"}'), isTrue);
expect(JsonConverter.isJson('foo'), isFalse);
expect(JsonConverter.isJson(''), isFalse);
expect(JsonConverter.isJson(null), isFalse);
expect(JsonConverter.isJson(42), isFalse);
expect(JsonConverter.isJson([]), isFalse);
expect(JsonConverter.isJson([1, 2, 3]), isFalse);
expect(JsonConverter.isJson(['a', 'b', 'c']), isFalse);
expect(JsonConverter.isJson({}), isFalse);
expect(
JsonConverter.isJson({
'foo': 'bar',
'list': [1, 2, 3],
}),
isFalse,
);
});
});

test('respects content-type headers', () {
Expand Down