2016-08-03 10:36:47 +02:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 00:34:53 +01:00
|
|
|
namespace Shaarli\Http;
|
|
|
|
|
2016-08-03 10:36:47 +02:00
|
|
|
/**
|
|
|
|
* Unitary tests for getIpAddressFromProxy()
|
|
|
|
*/
|
2020-09-29 14:41:40 +02:00
|
|
|
class GetIpAdressFromProxyTest extends \Shaarli\TestCase
|
2018-10-13 00:35:47 +02:00
|
|
|
{
|
2016-08-03 10:36:47 +02:00
|
|
|
/**
|
|
|
|
* Test without proxy
|
|
|
|
*/
|
|
|
|
public function testWithoutProxy()
|
|
|
|
{
|
2021-04-05 09:39:34 +02:00
|
|
|
$this->assertFalse(getIpAddressFromProxy([], []));
|
2016-08-03 10:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a single IP in proxy header.
|
|
|
|
*/
|
|
|
|
public function testWithOneForwardedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
2021-04-05 09:39:34 +02:00
|
|
|
$server = ['HTTP_X_FORWARDED_FOR' => $ip];
|
|
|
|
$this->assertEquals($ip, getIpAddressFromProxy($server, []));
|
2016-08-03 10:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a multiple IPs in proxy header.
|
|
|
|
*/
|
|
|
|
public function testWithMultipleForwardedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
|
|
|
$ip2 = '2.2.2.2';
|
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$server = ['HTTP_X_FORWARDED_FOR' => $ip . ',' . $ip2];
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, []));
|
2016-08-03 10:36:47 +02:00
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$server = ['HTTP_X_FORWARDED_FOR' => $ip . ' , ' . $ip2];
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, []));
|
2016-08-03 10:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test with a trusted IP address.
|
|
|
|
*/
|
|
|
|
public function testWithTrustedIp()
|
|
|
|
{
|
|
|
|
$ip = '1.1.1.1';
|
|
|
|
$ip2 = '2.2.2.2';
|
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$server = ['HTTP_X_FORWARDED_FOR' => $ip];
|
|
|
|
$this->assertFalse(getIpAddressFromProxy($server, [$ip]));
|
2016-08-03 10:36:47 +02:00
|
|
|
|
2021-04-05 09:39:34 +02:00
|
|
|
$server = ['HTTP_X_FORWARDED_FOR' => $ip . ',' . $ip2];
|
|
|
|
$this->assertEquals($ip2, getIpAddressFromProxy($server, [$ip]));
|
|
|
|
$this->assertFalse(getIpAddressFromProxy($server, [$ip, $ip2]));
|
2016-08-03 10:36:47 +02:00
|
|
|
}
|
|
|
|
}
|