Add GeoIP country fallback for link click analytics.
Deploy Ladill Link / deploy (push) Successful in 39s
Deploy Ladill Link / deploy (push) Successful in 39s
Resolve countries from CDN headers first, then MaxMind GeoLite2 via visitor IP, with an artisan command to download and refresh the database. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Support\Link\LinkGeoResolver;
|
||||
use Illuminate\Http\Request;
|
||||
use Tests\TestCase;
|
||||
|
||||
class LinkGeoResolverTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
LinkGeoResolver::resetReader();
|
||||
config(['link.geoip_database' => base_path('tests/fixtures/GeoLite2-Country-Test.mmdb')]);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
LinkGeoResolver::resetReader();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_prefers_proxy_country_header_over_ip_lookup(): void
|
||||
{
|
||||
$request = Request::create('/', 'GET', server: [
|
||||
'REMOTE_ADDR' => '81.2.69.142',
|
||||
'HTTP_CF_IPCOUNTRY' => 'GH',
|
||||
]);
|
||||
|
||||
$this->assertSame('GH', LinkGeoResolver::countryCode($request));
|
||||
}
|
||||
|
||||
public function test_resolves_country_from_public_ip_when_header_missing(): void
|
||||
{
|
||||
$request = Request::create('/', 'GET', server: [
|
||||
'REMOTE_ADDR' => '81.2.69.142',
|
||||
]);
|
||||
|
||||
$this->assertSame('GB', LinkGeoResolver::countryCode($request));
|
||||
}
|
||||
|
||||
public function test_ignores_private_ips(): void
|
||||
{
|
||||
$this->assertNull(LinkGeoResolver::countryFromIp('127.0.0.1'));
|
||||
$this->assertNull(LinkGeoResolver::countryFromIp('10.0.0.4'));
|
||||
}
|
||||
|
||||
public function test_returns_null_when_geoip_database_is_missing(): void
|
||||
{
|
||||
config(['link.geoip_database' => storage_path('app/geoip/missing.mmdb')]);
|
||||
LinkGeoResolver::resetReader();
|
||||
|
||||
$request = Request::create('/', 'GET', server: [
|
||||
'REMOTE_ADDR' => '81.2.69.142',
|
||||
]);
|
||||
|
||||
$this->assertNull(LinkGeoResolver::countryCode($request));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user