Skip to content

Commit

Permalink
Merge pull request #81 from Floating-Dartists/fix/#64_double-slashes-…
Browse files Browse the repository at this point in the history
…in-url

fix + tests
  • Loading branch information
TesteurManiak authored May 15, 2023
2 parents 588ec55 + ecdcea1 commit bd34a5e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/src/matomo_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: {
Expand Down
7 changes: 7 additions & 0 deletions lib/utils/extensions.dart
Original file line number Diff line number Diff line change
@@ -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';
}
}
18 changes: 18 additions & 0 deletions test/utils/extensions_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
}

0 comments on commit bd34a5e

Please sign in to comment.