|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Web\Tests\RateLimiter; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Yiisoft\Cache\ArrayCache; |
|
9
|
|
|
use Yiisoft\Http\Method; |
|
10
|
|
|
use Yiisoft\Yii\Web\RateLimiter\CacheStorage; |
|
11
|
|
|
use Yiisoft\Yii\Web\RateLimiter\Counter; |
|
12
|
|
|
use Yiisoft\Yii\Web\RateLimiter\StorageInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class CounterTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @test |
|
18
|
|
|
*/ |
|
19
|
|
|
public function expectedCounterValueInitial(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$storage = $this->getStorage(); |
|
22
|
|
|
$counter = (new Counter($storage)) |
|
23
|
|
|
->init($this->createRequest()); |
|
24
|
|
|
|
|
25
|
|
|
$this->assertEquals(0, $counter->getCounterValue()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @test |
|
30
|
|
|
*/ |
|
31
|
|
|
public function expectedCounterValue(): void |
|
32
|
|
|
{ |
|
33
|
|
|
$storage = $this->getStorage(); |
|
34
|
|
|
$storage->setCounterValue('test-id', 30, 10); |
|
35
|
|
|
|
|
36
|
|
|
$counter = (new Counter($storage)) |
|
37
|
|
|
->setId('test-id') |
|
38
|
|
|
->init($this->createRequest()); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals(30, $counter->getCounterValue()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function init(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$storage = $this->getStorage(); |
|
49
|
|
|
(new Counter($storage))->init($this->createRequest()); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertTrue($storage->hasCounterValue('rate-limiter-get-/')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @test |
|
56
|
|
|
*/ |
|
57
|
|
|
public function increment(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$counter = (new Counter($this->getStorage())) |
|
60
|
|
|
->init($this->createRequest()); |
|
61
|
|
|
|
|
62
|
|
|
for ($i = 0; $i < 1000; $i++) { |
|
63
|
|
|
$counter->increment(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals(1000, $counter->getCounterValue()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
*/ |
|
72
|
|
|
public function generateIdByCallback(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$storage = $this->getStorage(); |
|
75
|
|
|
$storage->setCounterValue('POST', 101, 10); |
|
76
|
|
|
|
|
77
|
|
|
$counter = (new Counter($storage)) |
|
78
|
|
|
->setIdCallback( |
|
79
|
|
|
static function (ServerRequestInterface $request) { |
|
80
|
|
|
return $request->getMethod(); |
|
81
|
|
|
} |
|
82
|
|
|
) |
|
83
|
|
|
->init($this->createRequest(Method::POST)); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals(101, $counter->getCounterValue()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @test |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setInterval(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$counter = (new Counter($this->getStorage())) |
|
94
|
|
|
->setInterval(3) |
|
95
|
|
|
->init($this->createRequest()); |
|
96
|
|
|
|
|
97
|
|
|
$counter->increment(); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals(1, $counter->getCounterValue()); |
|
100
|
|
|
sleep(3); |
|
101
|
|
|
$this->assertEquals(0, $counter->getCounterValue()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @test |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setCounterId(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$storage = $this->getStorage(); |
|
110
|
|
|
(new Counter($storage)) |
|
111
|
|
|
->setId('test') |
|
112
|
|
|
->init($this->createRequest()); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertTrue($storage->hasCounterValue('test')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @test |
|
119
|
|
|
*/ |
|
120
|
|
|
public function expectNotInitException(): void |
|
121
|
|
|
{ |
|
122
|
|
|
$this->expectException(\RuntimeException::class); |
|
123
|
|
|
|
|
124
|
|
|
(new Counter($this->getStorage()))->increment(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
private function getStorage(): StorageInterface |
|
128
|
|
|
{ |
|
129
|
|
|
return new CacheStorage(new ArrayCache()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface |
|
133
|
|
|
{ |
|
134
|
|
|
return new ServerRequest($method, $uri); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|