From 6c8f31f561ec384a280182c49251f12be8469ff9 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Wed, 23 Aug 2023 09:39:43 +0200 Subject: [PATCH] Some minor changes --- lib/src/model/user_agent.dart | 4 ++-- lib/src/utils/http_helper.dart | 8 +++----- test/configuration_test.dart | 25 +++++++++++++------------ test/test_constants.dart | 3 ++- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/src/model/user_agent.dart b/lib/src/model/user_agent.dart index 79dbd3b3ab..7e08847c28 100644 --- a/lib/src/model/user_agent.dart +++ b/lib/src/model/user_agent.dart @@ -16,13 +16,13 @@ class UserAgent extends JsonObject { /// Additional information about your application final String? comment; - const UserAgent({ + UserAgent({ required this.name, this.version, this.system, this.url, this.comment, - }) : assert(name.length > 1, 'A non empty name is required'); + }) : assert(name.trim().length > 1, 'A non empty name is required'); @override Map toJson() => { diff --git a/lib/src/utils/http_helper.dart b/lib/src/utils/http_helper.dart index c3b7229c9e..854df2382a 100644 --- a/lib/src/utils/http_helper.dart +++ b/lib/src/utils/http_helper.dart @@ -33,11 +33,9 @@ class HttpHelper { static Map addUserAgentParameters( Map? map, ) { - assert( - OpenFoodAPIConfiguration.userAgent != null, - 'A User-Agent must be set before calling this method', - ); - + if (OpenFoodAPIConfiguration.userAgent == null) { + throw Exception('A User-Agent must be set before calling this method'); + } map ??= {}; map['app_name'] = OpenFoodAPIConfiguration.userAgent!.name; diff --git a/test/configuration_test.dart b/test/configuration_test.dart index b77cf05e11..5e672d0534 100644 --- a/test/configuration_test.dart +++ b/test/configuration_test.dart @@ -17,7 +17,7 @@ void main() { path: '/test/test.pl', ), throwsA( - const TypeMatcher(), + const TypeMatcher(), ), ); }); @@ -41,7 +41,7 @@ void main() { ); expect( uri1.toString(), - 'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appName', + 'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appNameValue', ); }); @@ -100,7 +100,7 @@ void main() { ); expect( uri.toString(), - 'https://world.openfoodfacts.org/test/test.pl?$_appName&app_uuid=$uuid', + 'https://world.openfoodfacts.org/test/test.pl?$_appNameValue&app_uuid=$uuid', ); uri = UriHelper.getUri( @@ -109,7 +109,7 @@ void main() { ); expect( uri.toString(), - 'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appName&app_uuid=$uuid', + 'https://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appNameValue&app_uuid=$uuid', ); uri = UriHelper.getUri( @@ -140,7 +140,7 @@ void main() { ); expect( uri.toString(), - 'https://world.openfoodfacts.net/test/test.pl?$_appName', + 'https://world.openfoodfacts.net/test/test.pl?$_appNameValue', ); Uri uri1 = UriHelper.getUri( @@ -150,7 +150,7 @@ void main() { ); expect( uri1.toString(), - 'https://world.openfoodfacts.net/test/test.pl?test=true&queryType=PROD&$_appName', + 'https://world.openfoodfacts.net/test/test.pl?test=true&queryType=PROD&$_appNameValue', ); }); @@ -206,7 +206,7 @@ void main() { ); expect( uri.toString(), - 'http://world.openfoodfacts.org/test/test.pl?$_appName', + 'http://world.openfoodfacts.org/test/test.pl?$_appNameValue', ); Uri uri1 = UriHelper.getUri( @@ -215,13 +215,14 @@ void main() { ); expect( uri1.toString(), - 'http://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appName', + 'http://world.openfoodfacts.org/test/test.pl?test=true&queryType=PROD&$_appNameValue', ); }); } -String get _appName => - 'app_name=${OpenFoodAPIConfiguration.userAgent!.name.replaceAll( - ' ', - '+', +String get _appNameValue => 'app_name=${Uri.encodeFull( + OpenFoodAPIConfiguration.userAgent!.name.replaceAll( + ' ', + '+', + ), )}'; diff --git a/test/test_constants.dart b/test/test_constants.dart index 848e472d43..dc4a10275d 100644 --- a/test/test_constants.dart +++ b/test/test_constants.dart @@ -1,7 +1,8 @@ import 'package:openfoodfacts/openfoodfacts.dart'; class TestConstants { - static const UserAgent TEST_USER_AGENT = UserAgent( + // ignore: non_constant_identifier_names + static UserAgent TEST_USER_AGENT = UserAgent( name: 'off-dart integration tests', );