1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
5
|
|
|
|
6
|
|
|
use Http\Message\ResponseFactory; |
7
|
|
|
use Nyholm\Psr7\Response; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Yii\Web\Middleware\IpFilter; |
13
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
14
|
|
|
|
15
|
|
|
class IpFilterTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
private const REQUEST_PARAMS = [ |
18
|
|
|
'REMOTE_ADDR' => '8.8.8.8', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private const ALLOWED_IP = '1.1.1.1'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ResponseFactory|PHPUnit_Framework_MockObject_MockObject |
25
|
|
|
*/ |
26
|
|
|
private $responseFactoryMock; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestHandlerInterface|PHPUnit_Framework_MockObject_MockObject |
30
|
|
|
*/ |
31
|
|
|
private $requestHandlerMock; |
32
|
|
|
|
33
|
|
|
private $ipFilter; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
$this->responseFactoryMock = $this->createMock(ResponseFactoryInterface::class); |
39
|
|
|
$this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class); |
40
|
|
|
$this->ipFilter = new IpFilter(self::ALLOWED_IP, $this->responseFactoryMock); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function processReturnsAccessDeniedResponseWhenIpIsNotAllowed() |
47
|
|
|
{ |
48
|
|
|
$this->setUpResponseFactory(); |
49
|
|
|
$requestMock = $this->createMock(ServerRequestInterface::class); |
50
|
|
|
$requestMock |
51
|
|
|
->expects($this->once()) |
52
|
|
|
->method('getServerParams') |
53
|
|
|
->willReturn(self::REQUEST_PARAMS); |
54
|
|
|
|
55
|
|
|
$this->requestHandlerMock |
56
|
|
|
->expects($this->never()) |
57
|
|
|
->method('handle') |
58
|
|
|
->with($requestMock); |
59
|
|
|
|
60
|
|
|
$response = $this->ipFilter->process($requestMock, $this->requestHandlerMock); |
61
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
*/ |
67
|
|
|
public function processCallsRequestHandlerWhenRemoteAddressIsAllowed() |
68
|
|
|
{ |
69
|
|
|
$requestParams = [ |
70
|
|
|
'REMOTE_ADDR' => self::ALLOWED_IP, |
71
|
|
|
]; |
72
|
|
|
$requestMock = $this->createMock(ServerRequestInterface::class); |
73
|
|
|
$requestMock |
74
|
|
|
->expects($this->once()) |
75
|
|
|
->method('getServerParams') |
76
|
|
|
->willReturn($requestParams); |
77
|
|
|
|
78
|
|
|
$this->requestHandlerMock |
79
|
|
|
->expects($this->once()) |
80
|
|
|
->method('handle') |
81
|
|
|
->with($requestMock); |
82
|
|
|
|
83
|
|
|
$this->ipFilter->process($requestMock, $this->requestHandlerMock); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setUpResponseFactory() |
87
|
|
|
{ |
88
|
|
|
$response = new Response(403); |
89
|
|
|
$this->responseFactoryMock |
90
|
|
|
->expects($this->once()) |
91
|
|
|
->method('createResponse') |
92
|
|
|
->willReturn($response); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|