Skip to content

Commit

Permalink
Merge pull request #61 from Floating-Dartists/dev
Browse files Browse the repository at this point in the history
Release v3.1.0
  • Loading branch information
TesteurManiak authored May 3, 2023
2 parents 04caa00 + ec3df14 commit 3360df8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [dev]
pull_request:
branches: [main]
branches: [main, dev]
workflow_dispatch:

jobs:
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.1.0]

* Contributions from [Eric Prokop](https://github.com/EPNW-Eric)
* feat: Allow to set custom http headers [#59](https://github.com/Floating-Dartists/matomo-tracker/pull/59)
* Contribution from [TesteurManiak](https://github.com/TesteurManiak)
* fix: Added custom headers to `sendBatch` and added test cases [#60](https://github.com/Floating-Dartists/matomo-tracker/pull/60)

## [3.0.0]

**Check the [Migration Guide](https://github.com/Floating-Dartists/matomo-tracker#v300) section to learn about breaking changes in this version.**
Expand Down
5 changes: 5 additions & 0 deletions lib/src/matomo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class MatomoTracker {
/// The user agent is used to detect the operating system and browser used.
late final String? userAgent;

/// Custom http headers to add to each request.
late final Map<String, String> customHeaders;

/// URL for the current action.
late final String contentBase;

Expand Down Expand Up @@ -123,6 +126,7 @@ class MatomoTracker {
PlatformInfo? platformInfo,
bool cookieless = false,
Level verbosityLevel = Level.off,
Map<String, String> customHeaders = const {},
}) async {
if (_initialized) {
throw const AlreadyInitializedMatomoInstanceException();
Expand All @@ -140,6 +144,7 @@ class MatomoTracker {

this.siteId = siteId;
this.url = url;
this.customHeaders = customHeaders;
_dequeueInterval = dequeueInterval;
_lock = sync.Lock();
_platformInfo = platformInfo ?? PlatformInfo.instance;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/matomo_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MatomoDispatcher {
Future<void> send(MatomoEvent event) async {
final headers = <String, String>{
if (!kIsWeb) 'User-Agent': 'Dart Matomo Tracker',
...event.tracker.customHeaders,
};

final uri = buildUriForEvent(event);
Expand All @@ -46,6 +47,7 @@ class MatomoDispatcher {
final userAgent = events.first.tracker.userAgent;
final headers = <String, String>{
if (!kIsWeb && userAgent != null) userAgentHeaderKeys: userAgent,
...events.first.tracker.customHeaders,
};

final batch = {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: matomo_tracker
description: A fully cross-platform wrap of the Matomo tracking client for
Flutter, using the Matomo API.
version: 3.0.0
version: 3.1.0
homepage: https://github.com/Floating-Dartists/matomo-tracker
repository: https://github.com/Floating-Dartists/matomo-tracker
issue_tracker: https://github.com/Floating-Dartists/matomo-tracker/issues
Expand Down
52 changes: 52 additions & 0 deletions test/src/matomo_dispatcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import '../ressources/mock/data.dart';
import '../ressources/mock/mock.dart';

void main() {
const headerKey = 'foo';
const headerValue = 'bar';

setUpAll(() {
registerFallbackValue(Uri());
when(mockMatomoEvent.toMap).thenReturn({});
when(() => mockMatomoEvent.tracker).thenReturn(mockMatomoTracker);
when(() => mockMatomoTracker.userAgent).thenReturn(null);
when(() => mockMatomoTracker.log).thenReturn(Logger());
when(() => mockMatomoTracker.customHeaders).thenReturn({});
});

group('send', () {
Expand Down Expand Up @@ -50,6 +54,30 @@ void main() {

await expectLater(matomoDispatcher.send(mockMatomoEvent), completes);
});

test('should use customHeaders from the tracker', () async {
when(() => mockMatomoTracker.customHeaders).thenReturn({
headerKey: headerValue,
});

final matomoDispatcher = MatomoDispatcher(
matomoDispatcherBaseUrl,
matomoDispatcherToken,
httpClient: mockHttpClient,
);

await matomoDispatcher.send(mockMatomoEvent);

verify(
() => mockHttpClient.post(
any(),
headers: any(
named: 'headers',
that: containsPair(headerKey, headerValue),
),
),
);
});
});

group('sendBatch', () {
Expand Down Expand Up @@ -171,4 +199,28 @@ void main() {
matomoDispatcherToken,
);
});

test('should use customHeaders from the tracker', () async {
when(() => mockMatomoTracker.customHeaders).thenReturn({
headerKey: headerValue,
});

final matomoDispatcher = MatomoDispatcher(
matomoDispatcherBaseUrl,
matomoDispatcherToken,
httpClient: mockHttpClient,
);

await matomoDispatcher.send(mockMatomoEvent);

verify(
() => mockHttpClient.post(
any(),
headers: any(
named: 'headers',
that: containsPair(headerKey, headerValue),
),
),
);
});
}

0 comments on commit 3360df8

Please sign in to comment.