-
-
Notifications
You must be signed in to change notification settings - Fork 24
#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
f3ath
merged 4 commits into
f3ath:master
from
sanekyy:94-utf-8-as-default-charset-for-response-body-decoding
May 28, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done