|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
|
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 Psr\SimpleCache\CacheInterface; |
|
13
|
|
|
use Yiisoft\Cache\ArrayCache; |
|
14
|
|
|
use Yiisoft\Http\Method; |
|
15
|
|
|
use Yiisoft\Yii\Web\Middleware\RateLimiter; |
|
16
|
|
|
|
|
17
|
|
|
final class RateLimiterTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @test |
|
21
|
|
|
*/ |
|
22
|
|
|
public function isAllowed(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$middleware = $this->createRateLimiter($this->getCache()); |
|
25
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
26
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function isNotAllowed(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$cache = $this->getCache(); |
|
35
|
|
|
$cache->set('rate-limiter-get-/', 1000); |
|
36
|
|
|
|
|
37
|
|
|
$middleware = $this->createRateLimiter($cache); |
|
38
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
39
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @test |
|
44
|
|
|
*/ |
|
45
|
|
|
public function customLimit(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$cache = $this->getCache(); |
|
48
|
|
|
$cache->set('rate-limiter-get-/', 10); |
|
49
|
|
|
|
|
50
|
|
|
$middleware = $this->createRateLimiter($cache)->setLimit(11); |
|
51
|
|
|
|
|
52
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
53
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
54
|
|
|
|
|
55
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
56
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function customCacheKey(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$cache = $this->getCache(); |
|
65
|
|
|
$cache->set('custom-cache-key', 999); |
|
66
|
|
|
|
|
67
|
|
|
$middleware = $this->createRateLimiter($cache)->setCacheKey('custom-cache-key'); |
|
68
|
|
|
|
|
69
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
70
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
71
|
|
|
|
|
72
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
73
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @test |
|
78
|
|
|
*/ |
|
79
|
|
|
public function customCacheKeyCallback(): void |
|
80
|
|
|
{ |
|
81
|
|
|
$cache = $this->getCache(); |
|
82
|
|
|
$cache->set('POST', 1000); |
|
83
|
|
|
|
|
84
|
|
|
$middleware = $this->createRateLimiter($cache) |
|
85
|
|
|
->setCacheKeyByCallback( |
|
86
|
|
|
function (ServerRequestInterface $request) { |
|
87
|
|
|
return $request->getMethod(); |
|
88
|
|
|
} |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
92
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
93
|
|
|
|
|
94
|
|
|
$response = $middleware->process($this->createRequest(Method::POST), $this->createRequestHandler()); |
|
95
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @test |
|
100
|
|
|
*/ |
|
101
|
|
|
public function customCacheTtl(): void |
|
102
|
|
|
{ |
|
103
|
|
|
$middleware = $this->createRateLimiter($this->getCache()) |
|
104
|
|
|
->setLimit(1) |
|
105
|
|
|
->setCacheTtl(1); |
|
106
|
|
|
|
|
107
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
108
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
109
|
|
|
|
|
110
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
111
|
|
|
$this->assertEquals(429, $response->getStatusCode()); |
|
112
|
|
|
|
|
113
|
|
|
sleep(1); |
|
114
|
|
|
|
|
115
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
116
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @test |
|
121
|
|
|
*/ |
|
122
|
|
|
public function disableAutoIncrement(): void |
|
123
|
|
|
{ |
|
124
|
|
|
$cache = $this->getCache(); |
|
125
|
|
|
|
|
126
|
|
|
$middleware = $this->createRateLimiter($cache)->setAutoIncrement(false); |
|
127
|
|
|
$response = $middleware->process($this->createRequest(), $this->createRequestHandler()); |
|
128
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
129
|
|
|
$this->assertEquals(0, $cache->get('rate-limiter-get-/')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function getCache(): CacheInterface |
|
133
|
|
|
{ |
|
134
|
|
|
return new ArrayCache(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function createRequestHandler(): RequestHandlerInterface |
|
138
|
|
|
{ |
|
139
|
|
|
return new class implements RequestHandlerInterface { |
|
140
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
141
|
|
|
{ |
|
142
|
|
|
return new Response(200); |
|
143
|
|
|
} |
|
144
|
|
|
}; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
|
148
|
|
|
{ |
|
149
|
|
|
return new ServerRequest($method, $uri); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
private function createRateLimiter(CacheInterface $cache): RateLimiter |
|
153
|
|
|
{ |
|
154
|
|
|
return new RateLimiter($cache, new Psr17Factory()); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|