dependabot-auto-merge / dependabot (pull_request_target) Skipped
Fix PHP code style issues / php-code-styling (push) Successful in 6s
PHPStan / phpstan (push) Successful in 2m32s
run-tests / P8.3 - L11.* - prefer-lowest - ubuntu-latest (push) Failing after 34s
run-tests / P8.4 - L11.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-lowest - ubuntu-latest (push) Canceled after 0s
run-tests / P8.4 - L12.* - prefer-stable - ubuntu-latest (push) Canceled after 0s
run-tests / P8.3 - L11.* - prefer-stable - ubuntu-latest (push) Canceled after 2s
- Rename Composer package to mattitja/laravel-plausible-proxy and namespace to Mattitja\LaravelPlausibleProxy - Point homepage and links to git.mattitja.se - Forward only User-Agent and visitor IP (X-Forwarded-For) to Plausible instead of all request headers - Forward all event fields and return Plausible's upstream response - Accept props as array or JSON string - Drop Laravel 10, upgrade Larastan to 3 and PHPStan plugins to 2 - Remove broken facade alias and dead autoload paths - Clean up README, issue template and remove invalid FUNDING.yml
118 lines
3.7 KiB
PHP
118 lines
3.7 KiB
PHP
<?php
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
it('returns plausible script from cache', function () {
|
|
Cache::put('plausible_script_js', 'console.log("Cached script");', now()->addHours(6));
|
|
|
|
$response = $this->get('/js/script.js');
|
|
|
|
$response->assertStatus(200)
|
|
->assertHeader('Content-Type', 'application/javascript')
|
|
->assertSee('Cached script');
|
|
});
|
|
|
|
it('fetches plausible script if not cached', function () {
|
|
Http::fake([
|
|
config('plausible-proxy.domain').'/js/script.js' => Http::response('console.log("Plausible script");', 200),
|
|
]);
|
|
|
|
$response = $this->get('/js/script.js');
|
|
|
|
$response->assertStatus(200)
|
|
->assertHeader('Content-Type', 'application/javascript')
|
|
->assertSee('Plausible script');
|
|
|
|
expect(Cache::get('plausible_script_js'))->toBe('console.log("Plausible script");');
|
|
});
|
|
|
|
it('handles failed script fetch', function () {
|
|
Http::fake([
|
|
config('plausible-proxy.domain').'/js/script.js' => Http::response('', 500),
|
|
]);
|
|
|
|
$response = $this->get('/js/script.js');
|
|
|
|
$response->assertStatus(500)
|
|
->assertSee('Error fetching Plausible script');
|
|
});
|
|
|
|
it('rejects invalid event data', function () {
|
|
$response = $this->postJson('/api/event', [
|
|
'n' => 'pageview',
|
|
'u' => 'invalid-url',
|
|
'd' => 'example.com',
|
|
]);
|
|
|
|
$response->assertStatus(422)
|
|
->assertJsonValidationErrors(['u' => 'The u field must be a valid URL.']);
|
|
});
|
|
|
|
it('forwards only user agent and client ip to plausible', function () {
|
|
config()->set('plausible-proxy.domain', 'https://plausible.io');
|
|
|
|
Http::fake([
|
|
'https://plausible.io/api/event' => Http::response('ok', 202),
|
|
]);
|
|
|
|
$response = $this->withServerVariables(['REMOTE_ADDR' => '203.0.113.7'])
|
|
->postJson('/api/event', [
|
|
'n' => 'pageview',
|
|
'u' => 'https://example.com/page',
|
|
'd' => 'example.com',
|
|
], [
|
|
'User-Agent' => 'TestAgent',
|
|
'Cookie' => 'laravel_session=secret',
|
|
'Authorization' => 'Bearer secret',
|
|
]);
|
|
|
|
$response->assertStatus(202);
|
|
expect($response->getContent())->toBe('ok');
|
|
|
|
Http::assertSentCount(1);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return $request->url() === 'https://plausible.io/api/event'
|
|
&& $request->header('User-Agent') === ['TestAgent']
|
|
&& $request->header('X-Forwarded-For') === ['203.0.113.7']
|
|
&& ! $request->hasHeader('Cookie')
|
|
&& ! $request->hasHeader('Authorization')
|
|
&& $request['n'] === 'pageview'
|
|
&& $request['u'] === 'https://example.com/page'
|
|
&& $request['d'] === 'example.com';
|
|
});
|
|
});
|
|
|
|
it('forwards additional plausible fields', function () {
|
|
config()->set('plausible-proxy.domain', 'https://plausible.io');
|
|
|
|
Http::fake([
|
|
'https://plausible.io/api/event' => Http::response('ok', 202),
|
|
]);
|
|
|
|
$this->postJson('/api/event', [
|
|
'n' => 'Purchase',
|
|
'u' => 'https://example.com/checkout',
|
|
'd' => 'example.com',
|
|
'i' => false,
|
|
'p' => '{"plan":"pro"}',
|
|
'$' => ['amount' => '9.99', 'currency' => 'EUR'],
|
|
])->assertStatus(202);
|
|
|
|
Http::assertSent(function ($request) {
|
|
return $request['i'] === false
|
|
&& $request['p'] === '{"plan":"pro"}'
|
|
&& $request['$'] === ['amount' => '9.99', 'currency' => 'EUR'];
|
|
});
|
|
});
|
|
|
|
it('rejects invalid props', function () {
|
|
$this->postJson('/api/event', [
|
|
'n' => 'pageview',
|
|
'u' => 'https://example.com/page',
|
|
'd' => 'example.com',
|
|
'p' => 'not-json',
|
|
])->assertStatus(422)->assertJsonValidationErrors(['p']);
|
|
});
|