aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/HttpUtils/ClientIpIdTest.php
blob: 982e57e0b6ff37ae8b42b1a5a11707ddf0bb08b9 (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
<?php
/**
 * HttpUtils' tests
 */

namespace Shaarli\Http;

require_once 'application/http/HttpUtils.php';

/**
 * Unitary tests for client_ip_id()
 */
class ClientIpIdTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Get a remote client ID based on its IP
     */
    public function testClientIpIdRemote()
    {
        $this->assertEquals(
            '10.1.167.42',
            client_ip_id(['REMOTE_ADDR' => '10.1.167.42'])
        );
    }

    /**
     * Get a remote client ID based on its IP and proxy information (1)
     */
    public function testClientIpIdRemoteForwarded()
    {
        $this->assertEquals(
            '10.1.167.42_127.0.1.47',
            client_ip_id([
                'REMOTE_ADDR' => '10.1.167.42',
                'HTTP_X_FORWARDED_FOR' => '127.0.1.47'
            ])
        );
    }

    /**
     * Get a remote client ID based on its IP and proxy information (2)
     */
    public function testClientIpIdRemoteForwardedClient()
    {
        $this->assertEquals(
            '10.1.167.42_10.1.167.56_127.0.1.47',
            client_ip_id([
                'REMOTE_ADDR' => '10.1.167.42',
                'HTTP_X_FORWARDED_FOR' => '10.1.167.56',
                'HTTP_CLIENT_IP' => '127.0.1.47'
            ])
        );
    }
}