From 3246f9572e40f0d7787b1f692b90570bf52b66a5 Mon Sep 17 00:00:00 2001 From: Bob Jackman Date: Fri, 14 Apr 2017 12:37:01 -0700 Subject: [PATCH 1/3] Properly handle `null` field values when encoding requests. See: https://github.com/dart-lang/http/issues/75 --- lib/src/utils.dart | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 789c2d964f..4acff34248 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -13,6 +13,10 @@ import 'byte_stream.dart'; /// mapToQuery({"foo": "bar", "baz": "bang"}); /// //=> "foo=bar&baz=bang" String mapToQuery(Map map, {Encoding encoding}) { + map.keys + .where((k) => (map[k] == null)).toList() // -- keys for null elements + .forEach(map.remove); + var pairs = >[]; map.forEach((key, value) => pairs.add([Uri.encodeQueryComponent(key, encoding: encoding), From 20d6001149f8755c720e23bbde83c6e18a5ab1d2 Mon Sep 17 00:00:00 2001 From: Bob Jackman Date: Fri, 14 Apr 2017 13:05:48 -0700 Subject: [PATCH 2/3] Add test cases for null request fields. See: https://github.com/dart-lang/http/issues/75 --- test/io/http_test.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/io/http_test.dart b/test/io/http_test.dart index 2419e198ef..041f7de82f 100644 --- a/test/io/http_test.dart +++ b/test/io/http_test.dart @@ -125,7 +125,8 @@ main() { 'User-Agent': 'Dart' }, body: { 'some-field': 'value', - 'other-field': 'other value' + 'other-field': 'other value', + 'null-field': null }).then((response) { expect(response.statusCode, equals(200)); expect(response.body, parse(equals({ @@ -229,7 +230,8 @@ main() { 'User-Agent': 'Dart' }, body: { 'some-field': 'value', - 'other-field': 'other value' + 'other-field': 'other value', + 'null-field': null }).then((response) { expect(response.statusCode, equals(200)); expect(response.body, parse(equals({ @@ -333,7 +335,8 @@ main() { 'User-Agent': 'Dart' }, body: { 'some-field': 'value', - 'other-field': 'other value' + 'other-field': 'other value', + 'null-field': null }).then((response) { expect(response.statusCode, equals(200)); expect(response.body, parse(equals({ From da64c26c61db5b47ab5833674637dc78ea7a91ae Mon Sep 17 00:00:00 2001 From: Bob Jackman Date: Fri, 14 Apr 2017 13:10:55 -0700 Subject: [PATCH 3/3] Refactor handling of null request values as per PR comments. See: https://github.com/dart-lang/http/pull/77/files/26e6075d93a41f381012b1588b2f4ccb056e9231 --- lib/src/utils.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/src/utils.dart b/lib/src/utils.dart index 4acff34248..18079dbd4e 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -13,9 +13,8 @@ import 'byte_stream.dart'; /// mapToQuery({"foo": "bar", "baz": "bang"}); /// //=> "foo=bar&baz=bang" String mapToQuery(Map map, {Encoding encoding}) { - map.keys - .where((k) => (map[k] == null)).toList() // -- keys for null elements - .forEach(map.remove); + var nullKeys = map.keys.where((k) => (map[k] == null)).toList(); + nullKeys.forEach(map.remove); // -- remove elements with null values var pairs = >[]; map.forEach((key, value) =>