From c012458cea8c8bfde7318a89b5658bc7050ac415 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Mon, 15 May 2023 09:55:44 +0200 Subject: [PATCH 1/2] fix + tests --- lib/src/matomo_event.dart | 6 +++++- lib/utils/extensions.dart | 7 +++++++ test/utils/extensions_test.dart | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/utils/extensions.dart create mode 100644 test/utils/extensions_test.dart diff --git a/lib/src/matomo_event.dart b/lib/src/matomo_event.dart index b9e4747..47f5119 100644 --- a/lib/src/matomo_event.dart +++ b/lib/src/matomo_event.dart @@ -4,6 +4,7 @@ import 'dart:ui'; import 'package:clock/clock.dart'; import 'package:matomo_tracker/matomo_tracker.dart'; +import 'package:matomo_tracker/utils/extensions.dart'; class MatomoEvent { MatomoEvent({ @@ -139,8 +140,11 @@ class MatomoEvent { final actionName = action; final camp = campaign; final campKeyword = camp?.keyword; + final localPath = path; final uri = Uri.parse( - path != null ? '${tracker.contentBase}/$path' : tracker.contentBase, + localPath != null + ? '${tracker.contentBase}/${localPath.prefixWithSlash()}' + : tracker.contentBase, ); final url = uri.replace( queryParameters: { diff --git a/lib/utils/extensions.dart b/lib/utils/extensions.dart new file mode 100644 index 0000000..b085fa8 --- /dev/null +++ b/lib/utils/extensions.dart @@ -0,0 +1,7 @@ +extension StringExtensions on String { + /// If the string is not already prefixed with a slash, prefix it with one. + String prefixWithSlash() { + if (startsWith('/')) return this; + return '/$this'; + } +} diff --git a/test/utils/extensions_test.dart b/test/utils/extensions_test.dart new file mode 100644 index 0000000..1c2b3e4 --- /dev/null +++ b/test/utils/extensions_test.dart @@ -0,0 +1,18 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:matomo_tracker/utils/extensions.dart'; + +void main() { + group('StringExtensions', () { + group('prefixWithSlash', () { + test('should return the same string', () { + const String testString = '/test'; + expect(testString.prefixWithSlash(), testString); + }); + + test('should return the string with a slash prefix', () { + const String testString = 'test'; + expect(testString.prefixWithSlash(), '/$testString'); + }); + }); + }); +} From ecdcea159be8fcc9845b3545fecf9abdce6e00b9 Mon Sep 17 00:00:00 2001 From: Guillaume Roux Date: Mon, 15 May 2023 10:01:05 +0200 Subject: [PATCH 2/2] fix test --- lib/src/matomo_event.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/matomo_event.dart b/lib/src/matomo_event.dart index 47f5119..1613797 100644 --- a/lib/src/matomo_event.dart +++ b/lib/src/matomo_event.dart @@ -143,7 +143,7 @@ class MatomoEvent { final localPath = path; final uri = Uri.parse( localPath != null - ? '${tracker.contentBase}/${localPath.prefixWithSlash()}' + ? '${tracker.contentBase}${localPath.prefixWithSlash()}' : tracker.contentBase, ); final url = uri.replace(