2016-08-03 10:36:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'application/HttpUtils.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unitary tests for getIpAddressFromProxy()
|
|
|
|
*/
|
2018-10-13 00:35:47 +02:00
|
|
|
class GetIpAdressFromProxyTest extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2016-08-03 10:36:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test without proxy
|
|
|
|
*/
|
|
|
|
public function testWithoutProxy()
|
|
|
|
{
|
|
|
|
$this->assertFalse(getIpAddressFromProxy(array(), array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a single IP in proxy header.
|
|
|
|
*/
|
|
|
|
public function testWithOneForwardedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
|
|
|
$server = array('HTTP_X_FORWARDED_FOR' => $ip);
|
|
|
|
$this->assertEquals($ip, getIpAddressFromProxy($server, array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a multiple IPs in proxy header.
|
|
|
|
*/
|
|
|
|
public function testWithMultipleForwardedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
|
|
|
$ip2 = '2.2.2.2';
|
|
|
|
|
|
|
|
$server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
|
|
|
|
|
|
|
|
$server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2);
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a trusted IP address.
|
|
|
|
*/
|
|
|
|
public function testWithTrustedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
|
|
|
$ip2 = '2.2.2.2';
|
|
|
|
|
|
|
|
$server = array('HTTP_X_FORWARDED_FOR' => $ip);
|
|
|
|
$this->assertFalse(getIpAddressFromProxy($server, array($ip)));
|
|
|
|
|
|
|
|
$server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip)));
|
|
|
|
$this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2)));
|
|
|
|
}
|
|
|
|
}
|