Skip to content

Commit

Permalink
🐛 remove charset from http request headers when the body is in bytes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse authored Oct 7, 2023
1 parent 14bc48f commit b48066d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
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

0 comments on commit b48066d

Please sign in to comment.