Passed
Pull Request — master (#203)
by
unknown
02:01
created

RateLimiterTest::createRequestHandler()

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A RateLimiterTest.php$0 ➔ handle() 0 3 1
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 singleRequestIsAllowed(): 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 moreThanDefaultNumberOfRequestsIsNotAllowed(): void
33
    {
34
        $middleware = $this->createRateLimiter($this->getCache());
35
36
        for ($i = 0; $i < 1000; $i++) {
37
            $middleware->process($this->createRequest(), $this->createRequestHandler());
38
        }
39
40
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
41
        $this->assertEquals(429, $response->getStatusCode());
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function customLimitWorksAsExpected(): void
48
    {
49
        $middleware = $this->createRateLimiter($this->getCache())->withLimit(11);
50
51
        for ($i = 0; $i < 10; $i++) {
52
            $middleware->process($this->createRequest(), $this->createRequestHandler());
53
        }
54
55
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
56
        $this->assertEquals(200, $response->getStatusCode());
57
58
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
59
        $this->assertEquals(429, $response->getStatusCode());
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function customCacheKey(): void
66
    {
67
        $cache = $this->getCache();
68
        $cache->set('custom-cache-key', 999);
69
70
        $middleware = $this->createRateLimiter($cache)->setCacheKey('custom-cache-key');
71
72
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
73
        $this->assertEquals(200, $response->getStatusCode());
74
75
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
76
        $this->assertEquals(429, $response->getStatusCode());
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function customCacheKeyCallback(): void
83
    {
84
        $cache = $this->getCache();
85
        $cache->set('POST', 1000);
86
87
        $middleware = $this->createRateLimiter($cache)
88
            ->setCacheKeyCallback(
89
                static function (ServerRequestInterface $request) {
90
                    return $request->getMethod();
91
                }
92
            );
93
94
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
95
        $this->assertEquals(200, $response->getStatusCode());
96
97
        $response = $middleware->process($this->createRequest(Method::POST), $this->createRequestHandler());
98
        $this->assertEquals(429, $response->getStatusCode());
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function customInterval(): void
105
    {
106
        $middleware = $this->createRateLimiter($this->getCache())
107
            ->withLimit(1)
108
            ->withInterval(1);
109
110
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
111
        $this->assertEquals(200, $response->getStatusCode());
112
113
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
114
        $this->assertEquals(429, $response->getStatusCode());
115
116
        sleep(1);
117
118
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
119
        $this->assertEquals(200, $response->getStatusCode());
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function disableAutoIncrement(): void
126
    {
127
        $cache = $this->getCache();
128
        $middleware = $this->createRateLimiter($cache)->setAutoIncrement(false);
129
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
130
        $this->assertEquals(200, $response->getStatusCode());
131
        $this->assertEquals(0, $cache->get('rate-limiter-get-/'));
132
    }
133
134
    private function getCache(): CacheInterface
135
    {
136
        return new ArrayCache();
137
    }
138
139
    private function createRequestHandler(): RequestHandlerInterface
140
    {
141
        return new class implements RequestHandlerInterface {
142
            public function handle(ServerRequestInterface $request): ResponseInterface
143
            {
144
                return new Response(200);
145
            }
146
        };
147
    }
148
149
    private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface
150
    {
151
        return new ServerRequest($method, $uri);
152
    }
153
154
    private function createRateLimiter(CacheInterface $cache): RateLimiter
155
    {
156
        return new RateLimiter($cache, new Psr17Factory());
157
    }
158
}
159