Skip to content

Commit

Permalink
Some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
g123k committed Aug 23, 2023
1 parent 2cd9c8b commit 6c8f31f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/src/model/user_agent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> toJson() => {
Expand Down
8 changes: 3 additions & 5 deletions lib/src/utils/http_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ class HttpHelper {
static Map<String, dynamic> addUserAgentParameters(
Map<String, dynamic>? 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 ??= <String, dynamic>{};
map['app_name'] = OpenFoodAPIConfiguration.userAgent!.name;

Expand Down
25 changes: 13 additions & 12 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
path: '/test/test.pl',
),
throwsA(
const TypeMatcher<AssertionError>(),
const TypeMatcher<Exception>(),
),
);
});
Expand All @@ -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',
);
});

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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',
);
});

Expand Down Expand Up @@ -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(
Expand All @@ -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(
' ',
'+',
),
)}';
3 changes: 2 additions & 1 deletion test/test_constants.dart
Original file line number Diff line number Diff line change
@@ -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',
);

Expand Down

0 comments on commit 6c8f31f

Please sign in to comment.