Skip to content

#94 UTF-8 as default charset for response body decoding #95

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

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
42 changes: 39 additions & 3 deletions lib/src/client/dart_http.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:http_parser/http_parser.dart';
import 'package:json_api/http.dart';

/// A handler using the Dart's built-in http client
class DartHttp implements HttpHandler {
DartHttp(this._client);
DartHttp(this._client, [this._defaultEncoding = utf8])
: assert(_defaultEncoding != null, "Default encoding can't be null");

@override
Future<HttpResponse> call(HttpRequest request) async {
final response = await _send(Request(request.method, request.uri)
..headers.addAll(request.headers)
..body = request.body);
return HttpResponse(response.statusCode,
body: response.body, headers: response.headers);
final responseBody =
_encodingForHeaders(response.headers).decode(response.bodyBytes);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this change please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return HttpResponse(
response.statusCode,
body: responseBody,
headers: response.headers,
);
}

final Client _client;
final Encoding _defaultEncoding;

Future<Response> _send(Request dartRequest) async =>
Response.fromStream(await _client.send(dartRequest));

/// Returns the encoding to use for a response with the given headers.
///
/// Defaults to [_defaultEncoding] if the headers don't specify a charset or if that
/// charset is unknown.
Encoding _encodingForHeaders(Map<String, String> headers) =>
_encodingForCharset(
_contentTypeForHeaders(headers).parameters['charset']);

/// Returns the [Encoding] that corresponds to [charset].
///
/// Returns [_defaultEncoding] if [charset] is null or if no [Encoding] was found that
/// corresponds to [charset].
Encoding _encodingForCharset(String charset) {
if (charset == null) return _defaultEncoding;
return Encoding.getByName(charset) ?? _defaultEncoding;
}

/// Returns the [MediaType] object for the given headers's content-type.
///
/// Defaults to `application/octet-stream`.
MediaType _contentTypeForHeaders(Map<String, String> headers) {
var contentType = headers['content-type'];
if (contentType != null) return MediaType.parse(contentType);
return MediaType('application', 'octet-stream');
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ environment:
sdk: '>=2.6.0 <3.0.0'
dependencies:
http: ^0.12.0
http_parser: ^3.1.4
dev_dependencies:
args: ^1.5.2
pedantic: ^1.9.0
Expand Down
52 changes: 52 additions & 0 deletions test/unit/client/dart_http_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:json_api/client.dart';
import 'package:json_api/http.dart';
import 'package:test/test.dart';

void main() {
group('Decode body with', () {
final stringBodyRu = 'йцукен';
final bytesBodyRu = utf8.encode(stringBodyRu);
final stringBodyEn = 'qwerty';
final bytesBodyEn = utf8.encode(stringBodyEn);

final buildResponse = (
List<int> bytesBody,
Encoding encoding,
) async {
final dartHttp = DartHttp(
MockClient(
(request) async {
return http.Response.bytes(bytesBody, 200);
},
),
encoding,
);

return dartHttp.call(HttpRequest('', Uri.parse('http://test.com')));
};

test('UTF-8 ru', () async {
final response = await buildResponse(bytesBodyRu, utf8);
expect(response.body, equals(stringBodyRu));
});

test('latin1 ru', () async {
final response = await buildResponse(bytesBodyRu, latin1);
expect(response.body, isNot(equals(stringBodyRu)));
});

test('UTF-8 en', () async {
final response = await buildResponse(bytesBodyEn, utf8);
expect(response.body, equals(stringBodyEn));
});

test('latin1 en', () async {
final response = await buildResponse(bytesBodyEn, latin1);
expect(response.body, equals(stringBodyEn));
});
});
}