GetIpAdressFromProxyTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. require_once 'application/HttpUtils.php';
  3. /**
  4. * Unitary tests for getIpAddressFromProxy()
  5. */
  6. class GetIpAdressFromProxyTest extends PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * Test without proxy
  10. */
  11. public function testWithoutProxy()
  12. {
  13. $this->assertFalse(getIpAddressFromProxy(array(), array()));
  14. }
  15. /**
  16. * Test with a single IP in proxy header.
  17. */
  18. public function testWithOneForwardedIp()
  19. {
  20. $ip = '1.1.1.1';
  21. $server = array('HTTP_X_FORWARDED_FOR' => $ip);
  22. $this->assertEquals($ip, getIpAddressFromProxy($server, array()));
  23. }
  24. /**
  25. * Test with a multiple IPs in proxy header.
  26. */
  27. public function testWithMultipleForwardedIp()
  28. {
  29. $ip = '1.1.1.1';
  30. $ip2 = '2.2.2.2';
  31. $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
  32. $this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
  33. $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2);
  34. $this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
  35. }
  36. /**
  37. * Test with a trusted IP address.
  38. */
  39. public function testWithTrustedIp()
  40. {
  41. $ip = '1.1.1.1';
  42. $ip2 = '2.2.2.2';
  43. $server = array('HTTP_X_FORWARDED_FOR' => $ip);
  44. $this->assertFalse(getIpAddressFromProxy($server, array($ip)));
  45. $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
  46. $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip)));
  47. $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2)));
  48. }
  49. }