aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/HttpUtils/GetIpAdressFromProxyTest.php
blob: fe3a639e296ed3fe6512c7186f43f9d49fb7694c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

namespace Shaarli\Http;

require_once 'application/http/HttpUtils.php';

/**
 * Unitary tests for getIpAddressFromProxy()
 */
class GetIpAdressFromProxyTest extends \PHPUnit\Framework\TestCase
{

    /**
     * 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)));
    }
}