AgentContextServiceTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Services\Agent\AgentContextService;
  4. use Illuminate\Http\Request;
  5. use PHPUnit\Framework\TestCase;
  6. class AgentContextServiceTest extends TestCase
  7. {
  8. public function test_mobile_device_and_gateway_location_are_recorded(): void
  9. {
  10. $request = Request::create('https://api.example.com/agent/auth/login', 'POST', [], [], [], [
  11. 'HTTP_USER_AGENT' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)',
  12. 'HTTP_ORIGIN' => 'https://agent.example.com',
  13. 'HTTP_CF_IPCOUNTRY' => 'CN',
  14. 'HTTP_X_IP_REGION' => 'Guangdong',
  15. 'HTTP_X_IP_CITY' => 'Shenzhen',
  16. ]);
  17. $service = new AgentContextService();
  18. $this->assertSame('mobile', $service->device($request));
  19. $this->assertSame('agent.example.com', $service->domain($request));
  20. $this->assertSame([
  21. 'country' => 'CN',
  22. 'region' => 'Guangdong',
  23. 'city' => 'Shenzhen',
  24. ], $service->location($request));
  25. }
  26. public function test_desktop_device_falls_back_to_request_host(): void
  27. {
  28. $request = Request::create('https://api.example.com/agent/auth/login', 'POST', [], [], [], [
  29. 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
  30. ]);
  31. $service = new AgentContextService();
  32. $this->assertSame('pc', $service->device($request));
  33. $this->assertSame('api.example.com', $service->domain($request));
  34. }
  35. }