|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\RateLimiter; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
|
6
|
|
|
use Nyholm\Psr7\Response; |
|
7
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use Yiisoft\Http\Method; |
|
13
|
|
|
use Yiisoft\Yii\Web\RateLimiter\RateLimiter; |
|
14
|
|
|
use Yiisoft\Yii\Web\RateLimiter\CounterInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class RateLimiterTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @test |
|
20
|
|
|
*/ |
|
21
|
|
|
public function singleRequestIsAllowed(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$middleware = $this->createRateLimiter($this->getCounter()); |
|
24
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
25
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
*/ |
|
31
|
|
|
public function moreThanDefaultNumberOfRequestsIsNotAllowed(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$middleware = $this->createRateLimiter($this->getCounter()); |
|
34
|
|
|
|
|
35
|
|
|
for ($i = 0; $i < 1000; $i++) { |
|
36
|
|
|
$middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
40
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function customLimitWorksAsExpected(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$middleware = $this->createRateLimiter($this->getCounter())->withLimit(11); |
|
49
|
|
|
|
|
50
|
|
|
for ($i = 0; $i < 10; $i++) { |
|
51
|
|
|
$middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
55
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
56
|
|
|
|
|
57
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
58
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
*/ |
|
64
|
|
|
public function disableAutoIncrement(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$counter = $this->getCounter(); |
|
67
|
|
|
$middleware = $this->createRateLimiter($counter)->setAutoIncrement(false); |
|
68
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
69
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
70
|
|
|
$this->assertEquals(0, $counter->getCounterValue()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getCounter(): CounterInterface |
|
74
|
|
|
{ |
|
75
|
|
|
return new class implements CounterInterface { |
|
76
|
|
|
|
|
77
|
|
|
private int $count = 0; |
|
78
|
|
|
|
|
79
|
|
|
public function init(ServerRequestInterface $request): CounterInterface |
|
80
|
|
|
{ |
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function setIdCallback(callable $callback): CounterInterface |
|
85
|
|
|
{ |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function setId(string $id): CounterInterface |
|
90
|
|
|
{ |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function setInterval(int $interval): CounterInterface |
|
95
|
|
|
{ |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function increment(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$this->count++; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getCounterValue(): int |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->count; |
|
107
|
|
|
} |
|
108
|
|
|
}; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
|
112
|
|
|
{ |
|
113
|
|
|
return new class implements RequestHandlerInterface { |
|
114
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
115
|
|
|
{ |
|
116
|
|
|
return new Response(200); |
|
117
|
|
|
} |
|
118
|
|
|
}; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
|
122
|
|
|
{ |
|
123
|
|
|
return new ServerRequest($method, $uri); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function createRateLimiter(CounterInterface $counter): RateLimiter |
|
127
|
|
|
{ |
|
128
|
|
|
return new RateLimiter($counter, new Psr17Factory()); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|