Test Failed
Push — master ( d1c71c...c87f74 )
by mcfog
01:36
created

IpAddressTest::testHttpClientIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Lit\Middleware\IpAddress\Tests;
4
5
use Lit\Middleware\IpAddress;
6
use Nimo\Handlers\CallableHandler;
7
use Nimo\Tests\NimoTestCase;
8
use PHPUnit\Framework\Assert;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Zend\Diactoros\ServerRequestFactory;
11
12
class IpAddressTest extends NimoTestCase
13
{
14
    public function testIpSetByRemoteAddr()
15
    {
16
        $middleware = new IpAddress();
17
18
        $request = ServerRequestFactory::fromGlobals([
19
            'REMOTE_ADDR' => '192.168.1.1',
20
        ]);
21
        $this->runRequest($middleware, $request);
22
23
        Assert::assertSame('192.168.1.1', $middleware->getIpAddress());
24
    }
25
26
    public function testIpIsNullIfMissing()
27
    {
28
        $middleware = new IpAddress();
29
30
        $request = ServerRequestFactory::fromGlobals();
31
        $this->runRequest($middleware, $request);
32
33
34
        Assert::assertNull($middleware->getIpAddress());
35
    }
36
37 View Code Duplication
    public function testXForwardedForIp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $middleware = new IpAddress(['192.168.1.1']);
40
41
        $request = ServerRequestFactory::fromGlobals([
42
            'REMOTE_ADDR' => '192.168.1.1',
43
            'HTTP_X_FORWARDED_FOR' => '192.168.1.3, 192.168.1.2, 192.168.1.1'
44
        ]);
45
        $this->runRequest($middleware, $request);
46
47
        Assert::assertSame('192.168.1.3', $middleware->getIpAddress());
48
    }
49
50 View Code Duplication
    public function testProxyIpIsIgnored()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $middleware = new IpAddress();
53
54
        $request = ServerRequestFactory::fromGlobals([
55
            'REMOTE_ADDR' => '192.168.0.1',
56
            'HTTP_X_FORWARDED_FOR' => '192.168.1.3, 192.168.1.2, 192.168.1.1'
57
        ]);
58
        $this->runRequest($middleware, $request);
59
60
        Assert::assertSame('192.168.0.1', $middleware->getIpAddress());
61
    }
62
63 View Code Duplication
    public function testHttpClientIp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $middleware = new IpAddress(['192.168.1.1']);
66
67
        $request = ServerRequestFactory::fromGlobals([
68
            'REMOTE_ADDR' => '192.168.1.1',
69
            'HTTP_CLIENT_IP' => '192.168.1.3'
70
        ]);
71
        $this->runRequest($middleware, $request);
72
73
        Assert::assertSame('192.168.1.3', $middleware->getIpAddress());
74
    }
75
76 View Code Duplication
    public function testXForwardedForIpV6()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $middleware = new IpAddress(['192.168.1.1']);
79
80
        $request = ServerRequestFactory::fromGlobals([
81
            'REMOTE_ADDR' => '192.168.1.1',
82
            'HTTP_X_FORWARDED_FOR' => '001:DB8::21f:5bff:febf:ce22:8a2e'
83
        ]);
84
        $this->runRequest($middleware, $request);
85
86
        Assert::assertSame('001:DB8::21f:5bff:febf:ce22:8a2e', $middleware->getIpAddress());
87
    }
88
89 View Code Duplication
    public function testXForwardedForWithInvalidIp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $middleware = new IpAddress(['192.168.1.1']);
92
93
        $request = ServerRequestFactory::fromGlobals([
94
            'REMOTE_ADDR' => '192.168.1.1',
95
            'HTTP_X_FORWARDED_FOR' => 'foo-bar'
96
        ]);
97
        $this->runRequest($middleware, $request);
98
99
        Assert::assertSame('192.168.1.1', $middleware->getIpAddress());
100
    }
101
102 View Code Duplication
    public function testXForwardedForIpWithTrustedProxy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $middleware = new IpAddress(['192.168.0.1', '192.168.0.2']);
105
106
        $request = ServerRequestFactory::fromGlobals([
107
            'REMOTE_ADDR' => '192.168.0.2',
108
            'HTTP_X_FORWARDED_FOR' => '192.168.1.3, 192.168.1.2, 192.168.1.1'
109
        ]);
110
        $this->runRequest($middleware, $request);
111
112
        Assert::assertSame('192.168.1.3', $middleware->getIpAddress());
113
    }
114
115 View Code Duplication
    public function testXForwardedForIpWithUntrustedProxy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $middleware = new IpAddress(['192.168.0.1']);
118
119
        $request = ServerRequestFactory::fromGlobals([
120
            'REMOTE_ADDR' => '192.168.0.2',
121
            'HTTP_X_FORWARDED_FOR' => '192.168.1.3, 192.168.1.2, 192.168.1.1'
122
        ]);
123
        $this->runRequest($middleware, $request);
124
125
        Assert::assertSame('192.168.0.2', $middleware->getIpAddress());
126
    }
127
128 View Code Duplication
    public function testForwardedWithMultipleFor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $middleware = new IpAddress(['192.168.1.1']);
131
132
        $request = ServerRequestFactory::fromGlobals([
133
            'REMOTE_ADDR' => '192.168.1.1',
134
            'HTTP_FORWARDED' => 'for=192.0.2.43, for=198.51.100.17;by=203.0.113.60;proto=http;host=example.com',
135
        ]);
136
        $this->runRequest($middleware, $request);
137
138
        Assert::assertSame('192.0.2.43', $middleware->getIpAddress());
139
    }
140
141 View Code Duplication
    public function testForwardedWithAllOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $middleware = new IpAddress(['192.168.1.1']);
144
145
        $request = ServerRequestFactory::fromGlobals([
146
            'REMOTE_ADDR' => '192.168.1.1',
147
            'HTTP_FORWARDED' => 'for=192.0.2.60; proto=http;by=203.0.113.43; host=_hiddenProxy, for=192.0.2.61',
148
        ]);
149
        $this->runRequest($middleware, $request);
150
151
        Assert::assertSame('192.0.2.60', $middleware->getIpAddress());
152
    }
153
154 View Code Duplication
    public function testForwardedWithWithIpV6()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $middleware = new IpAddress(['192.168.1.1']);
157
158
        $request = ServerRequestFactory::fromGlobals([
159
            'REMOTE_ADDR' => '192.168.1.1',
160
            'HTTP_FORWARDED' => 'For="[2001:db8:cafe::17]:4711", for=_internalProxy',
161
        ]);
162
        $this->runRequest($middleware, $request);
163
164
        Assert::assertSame('2001:db8:cafe::17', $middleware->getIpAddress());
165
    }
166
167
    public function testCustomHeader()
168
    {
169
        $headersToInspect = [
170
            'Foo-Bar'
171
        ];
172
        $middleware = new IpAddress(['192.168.1.1'], $headersToInspect);
173
174
        $request = ServerRequestFactory::fromGlobals([
175
            'REMOTE_ADDR' => '192.168.1.1',
176
        ]);
177
        $request = $request->withAddedHeader('Foo-Bar', '192.168.1.3');
178
        $this->runRequest($middleware, $request);
179
180
        Assert::assertSame('192.168.1.3', $middleware->getIpAddress());
181
    }
182
183
    /**
184
     * @param $middleware
185
     * @param $request
186
     */
187
    protected function runRequest(IpAddress $middleware, ServerRequestInterface $request): void
188
    {
189
        $response = $this->getResponseMock();
190
        $handler = CallableHandler::wrap(function (ServerRequestInterface $request) use ($response, $middleware) {
191
            $actualMiddleware = IpAddress::fromRequest($request);
192
            Assert::assertSame($middleware, $actualMiddleware);
193
194
            return $response;
195
        });
196
        $acturlResponse = $middleware->process($request, $handler);
197
        Assert::assertSame($response, $acturlResponse);
198
    }
199
}
200