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

🐛 remove charset from http request headers when the body is in bytes #508

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions chopper/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,23 @@ base class Request extends http.BaseRequest with EquatableMixin {
@visibleForTesting
http.Request toHttpRequest() {
final http.Request request = http.Request(method, url)
..followRedirects = followRedirects
..headers.addAll(headers);
..followRedirects = followRedirects;

if (body != null) {
if (body == null) {
request.headers.addAll(headers);
} else {
if (body is String) {
request.body = body;
request
..headers.addAll(headers)
..body = body;
} else if (body is List<int>) {
request.bodyBytes = body;
request
..bodyBytes = body
..headers.addAll(headers);
} else if (body is Map<String, String>) {
request.bodyFields = body;
request
..headers.addAll(headers)
..bodyFields = body;
} else {
throw ArgumentError.value('$body', 'body');
}
Expand Down
20 changes: 20 additions & 0 deletions chopper/test/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:chopper/chopper.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:test/test.dart';
import 'package:transparent_image/transparent_image.dart';

import 'test_service.dart';
import 'test_service_variable.dart';
Expand Down Expand Up @@ -763,6 +764,25 @@ void main() {
expect(request.bodyBytes, equals([1, 2, 3]));
});

test('BodyBytes does not have charset header', () {
final request = Request(
HttpMethod.Post,
Uri.parse('https://foo/'),
Uri.parse(''),
headers: {
'authorization': 'Bearer fooBarBaz',
'x-foo': 'bar',
},
body: kTransparentImage,
).toHttpRequest();

expect(request.headers['authorization'], equals('Bearer fooBarBaz'));
expect(request.headers['x-foo'], equals('bar'));
expect(request.headers['content-type'], isNull);
expect(request.headers['content-type'], isNot(contains('charset=')));
expect(request.bodyBytes, equals(kTransparentImage));
});

test('BodyFields', () {
final request = Request(
HttpMethod.Post,
Expand Down
Loading