<?phpnamespace Tests;use App\Net\ClientIp;use PHPUnit\Framework\TestCase;final class ClientIpTest extends TestCase{ public function testADirectClientIsItsSocketsAddress(): void { self::assertSame('82.67.98.164', ClientIp::resolve('82.67.98.164:51000', ['x-forwarded-for' => '9.9.9.9', 'cf-connecting-ip' => '9.9.9.9'])); } public function testBehindTraefikTheForwardedAddressCounts(): void { self::assertSame('82.67.98.164', ClientIp::resolve('172.18.0.7:40000', ['x-forwarded-for' => '82.67.98.164'])); } public function testCloudflaresHeaderCountsOnlyWhenTraefikSawCloudflare(): void { self::assertSame('82.67.98.164', ClientIp::resolve('172.18.0.7:40000', ['x-forwarded-for' => '162.158.172.118', 'cf-connecting-ip' => '82.67.98.164'])); // somebody who reaches the origin directly and writes the header himself self::assertSame('5.6.7.8', ClientIp::resolve('172.18.0.7:40000', ['x-forwarded-for' => '5.6.7.8', 'cf-connecting-ip' => '82.67.98.164'])); } public function testRangesAreMatchedBitByBit(): void { self::assertTrue(ClientIp::inAny('104.23.255.1', ['104.16.0.0/13'])); self::assertFalse(ClientIp::inAny('104.24.0.1', ['104.16.0.0/13'])); self::assertTrue(ClientIp::inAny('2606:4700::1', ['2606:4700::/32'])); self::assertSame('::1', ClientIp::host('[::1]:80')); }}