| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- namespace Tests\Unit;
- use App\Services\Agent\AgentContextService;
- use Illuminate\Http\Request;
- use PHPUnit\Framework\TestCase;
- class AgentContextServiceTest extends TestCase
- {
- public function test_mobile_device_and_gateway_location_are_recorded(): void
- {
- $request = Request::create('https://api.example.com/agent/auth/login', 'POST', [], [], [], [
- 'HTTP_USER_AGENT' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)',
- 'HTTP_ORIGIN' => 'https://agent.example.com',
- 'HTTP_CF_IPCOUNTRY' => 'CN',
- 'HTTP_X_IP_REGION' => 'Guangdong',
- 'HTTP_X_IP_CITY' => 'Shenzhen',
- ]);
- $service = new AgentContextService();
- $this->assertSame('mobile', $service->device($request));
- $this->assertSame('agent.example.com', $service->domain($request));
- $this->assertSame([
- 'country' => 'CN',
- 'region' => 'Guangdong',
- 'city' => 'Shenzhen',
- ], $service->location($request));
- }
- public function test_desktop_device_falls_back_to_request_host(): void
- {
- $request = Request::create('https://api.example.com/agent/auth/login', 'POST', [], [], [], [
- 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
- ]);
- $service = new AgentContextService();
- $this->assertSame('pc', $service->device($request));
- $this->assertSame('api.example.com', $service->domain($request));
- }
- }
|