Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
snoopdoggy322 committed Dec 12, 2024
1 parent 0820553 commit 8c267c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
6 changes: 5 additions & 1 deletion pkgs/http/lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class Response extends BaseResponse {

/// Create a new HTTP response with a byte array body.
Response.bytes(List<int> bodyBytes, super.statusCode,
{super.request, super.headers, super.isRedirect, super.persistentConnection, super.reasonPhrase})
{super.request,
super.headers,
super.isRedirect,
super.persistentConnection,
super.reasonPhrase})
: bodyBytes = toUint8List(bodyBytes),
super(contentLength: bodyBytes.length);

Expand Down
19 changes: 12 additions & 7 deletions pkgs/http/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ import 'byte_stream.dart';
///
/// mapToQuery({"foo": "bar", "baz": "bang"});
/// //=> "foo=bar&baz=bang"
String mapToQuery(Map<String, String> map, {required Encoding encoding}) => map.entries
.map((e) => '${Uri.encodeQueryComponent(e.key, encoding: encoding)}'
'=${Uri.encodeQueryComponent(e.value, encoding: encoding)}')
.join('&');
String mapToQuery(Map<String, String> map, {required Encoding encoding}) =>
map.entries
.map((e) => '${Uri.encodeQueryComponent(e.key, encoding: encoding)}'
'=${Uri.encodeQueryComponent(e.value, encoding: encoding)}')
.join('&');

/// Determines the appropriate [Encoding] based on the given [contentTypeHeader].
///
/// - If the `Content-Type` is `application/json` and no charset is specified, it defaults to [utf8].
/// - If a charset is specified in the parameters, it attempts to find a matching [Encoding].
/// - If no charset is specified or the charset is unknown, it falls back to the provided [fallback], which defaults to [latin1].
Encoding encodingForContentTypeHeader(MediaType contentTypeHeader, [Encoding fallback = latin1]) {
Encoding encodingForContentTypeHeader(MediaType contentTypeHeader,
[Encoding fallback = latin1]) {
final charset = contentTypeHeader.parameters['charset'];

// Default to utf8 for application/json when charset is unspecified.
if (contentTypeHeader.type == 'application' && contentTypeHeader.subtype == 'json' && charset == null) {
if (contentTypeHeader.type == 'application' &&
contentTypeHeader.subtype == 'json' &&
charset == null) {
return utf8;
}

Expand All @@ -41,7 +45,8 @@ Encoding encodingForContentTypeHeader(MediaType contentTypeHeader, [Encoding fal
/// Throws a [FormatException] if no [Encoding] was found that corresponds to
/// [charset].
Encoding requiredEncodingForCharset(String charset) =>
Encoding.getByName(charset) ?? (throw FormatException('Unsupported encoding "$charset".'));
Encoding.getByName(charset) ??
(throw FormatException('Unsupported encoding "$charset".'));

/// A regular expression that matches strings that are composed entirely of
/// ASCII-compatible characters.
Expand Down
31 changes: 21 additions & 10 deletions pkgs/http/test/response_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ void main() {

test('sets bodyBytes', () {
var response = http.Response('Hello, world!', 200);
expect(response.bodyBytes, equals([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]));
expect(
response.bodyBytes,
equals(
[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]));
});

test('respects the inferred encoding', () {
var response = http.Response('föøbãr', 200, headers: {'content-type': 'text/plain; charset=iso-8859-1'});
var response = http.Response('föøbãr', 200,
headers: {'content-type': 'text/plain; charset=iso-8859-1'});
expect(response.bodyBytes, equals([102, 246, 248, 98, 227, 114]));
});

test('test empty charset', () {
var response = http.Response('{"foo":"Привет, мир!"}', 200, headers: {'content-type': 'application/json'});
var response = http.Response('{"foo":"Привет, мир!"}', 200,
headers: {'content-type': 'application/json'});
expect(response.body, equals('{"foo":"Привет, мир!"}'));
});
});
Expand All @@ -51,7 +56,8 @@ void main() {
group('.fromStream()', () {
test('sets body', () async {
var controller = StreamController<List<int>>(sync: true);
var streamResponse = http.StreamedResponse(controller.stream, 200, contentLength: 13);
var streamResponse =
http.StreamedResponse(controller.stream, 200, contentLength: 13);
controller
..add([72, 101, 108, 108, 111, 44, 32])
..add([119, 111, 114, 108, 100, 33]);
Expand All @@ -62,7 +68,8 @@ void main() {

test('sets bodyBytes', () async {
var controller = StreamController<List<int>>(sync: true);
var streamResponse = http.StreamedResponse(controller.stream, 200, contentLength: 5);
var streamResponse =
http.StreamedResponse(controller.stream, 200, contentLength: 5);
controller.add([104, 101, 108, 108, 111]);
unawaited(controller.close());
var response = await http.Response.fromStream(streamResponse);
Expand All @@ -77,29 +84,33 @@ void main() {
});

test('one header', () async {
var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple'});
var response =
http.Response('Hello, world!', 200, headers: {'fruit': 'apple'});
expect(response.headersSplitValues, const {
'fruit': ['apple']
});
});

test('two headers', () async {
var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple,banana'});
var response = http.Response('Hello, world!', 200,
headers: {'fruit': 'apple,banana'});
expect(response.headersSplitValues, const {
'fruit': ['apple', 'banana']
});
});

test('two headers with lots of spaces', () async {
var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple \t , \tbanana'});
var response = http.Response('Hello, world!', 200,
headers: {'fruit': 'apple \t , \tbanana'});
expect(response.headersSplitValues, const {
'fruit': ['apple', 'banana']
});
});

test('one set-cookie', () async {
var response = http.Response('Hello, world!', 200,
headers: {'set-cookie': 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT'});
var response = http.Response('Hello, world!', 200, headers: {
'set-cookie': 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT'
});
expect(response.headersSplitValues, const {
'set-cookie': ['id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT']
});
Expand Down

0 comments on commit 8c267c4

Please sign in to comment.