Repository navigation
Expand file tree
/
Copy pathRateLimitTest.php
More file actions
191 lines (150 loc) · 7.57 KB
/
Copy pathRateLimitTest.php
File metadata and controls
191 lines (150 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Dingo\Api\Tests\Http\Middleware;
use Mockery as m;
use Dingo\Api\Http\Request;
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Route;
use Dingo\Api\Routing\Router;
use PHPUnit\Framework\TestCase;
use Illuminate\Cache\CacheManager;
use Dingo\Api\Http\InternalRequest;
use Illuminate\Container\Container;
use Dingo\Api\Http\RateLimit\Handler;
use Dingo\Api\Tests\Stubs\ThrottleStub;
use Dingo\Api\Http\Middleware\RateLimit;
use Dingo\Api\Exception\RateLimitExceededException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class RateLimitTest extends TestCase
{
protected $container;
protected $router;
protected $cache;
protected $handler;
protected $middleware;
public function setUp()
{
$this->container = new Container;
$this->container['config'] = ['cache.default' => 'array', 'cache.stores.array' => ['driver' => 'array']];
$this->router = m::mock(Router::class);
$this->cache = new CacheManager($this->container);
$this->handler = new Handler($this->container, $this->cache, []);
$this->middleware = new RateLimit($this->router, $this->handler);
$this->handler->setRateLimiter(function ($container, $request) {
return $request->getClientIp();
});
}
public function tearDown()
{
m::close();
}
public function testMiddlewareBypassesRequestsWithNoRateLimiting()
{
$request = Request::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->once()->andReturn(false);
$route->shouldReceive('getRateLimit')->once()->andReturn(0);
$route->shouldReceive('getRateLimitExpiration')->once()->andReturn(0);
$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
$this->handler->extend(new ThrottleStub([], false));
$response = $this->middleware->handle($request, function ($request) {
return new Response('foo');
});
$this->assertSame('foo', $response->getContent());
$this->assertArrayNotHasKey('x-ratelimit-limit', $response->headers->all());
$this->assertArrayNotHasKey('x-ratelimit-remaining', $response->headers->all());
$this->assertArrayNotHasKey('x-ratelimit-reset', $response->headers->all());
}
public function testMiddlewareBypassesInternalRequest()
{
$request = InternalRequest::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->never();
$route->shouldReceive('getRateLimit')->never();
$route->shouldReceive('getRateLimitExpiration')->never();
$this->router->shouldReceive('getCurrentRoute')->never();
$this->handler->extend(new ThrottleStub([], false));
$response = $this->middleware->handle($request, function ($request) {
return new Response('foo');
});
$this->assertSame('foo', $response->getContent());
$this->assertArrayNotHasKey('x-ratelimit-limit', $response->headers->all());
$this->assertArrayNotHasKey('x-ratelimit-remaining', $response->headers->all());
$this->assertArrayNotHasKey('x-ratelimit-reset', $response->headers->all());
}
public function testRateLimitingPassesAndResponseHeadersAreSet()
{
$request = Request::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->once()->andReturn(false);
$route->shouldReceive('getRateLimit')->once()->andReturn(0);
$route->shouldReceive('getRateLimitExpiration')->once()->andReturn(0);
$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
$this->handler->extend(new ThrottleStub(['limit' => 1]));
$response = $this->middleware->handle($request, function ($request) {
return new Response('foo');
});
$this->assertSame('foo', $response->getContent());
$this->assertArrayHasKey('x-ratelimit-limit', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-remaining', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-reset', $response->headers->all());
}
public function testRateLimitingFailsAndHeadersAreSetOnException()
{
$request = Request::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->once()->andReturn(false);
$route->shouldReceive('getRateLimit')->once()->andReturn(0);
$route->shouldReceive('getRateLimitExpiration')->once()->andReturn(0);
$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
$this->handler->extend(new ThrottleStub(['limit' => 0]));
try {
$this->middleware->handle($request, function ($request) {
return new Response('foo');
});
} catch (HttpException $exception) {
$this->assertInstanceOf(RateLimitExceededException::class, $exception);
$this->assertSame(429, $exception->getStatusCode());
$this->assertSame('You have exceeded your rate limit.', $exception->getMessage());
$headers = $exception->getHeaders();
$this->assertSame($headers['X-RateLimit-Reset'] - time(), $headers['Retry-After']);
$this->assertArrayHasKey('X-RateLimit-Limit', $headers);
$this->assertArrayHasKey('X-RateLimit-Remaining', $headers);
$this->assertArrayHasKey('X-RateLimit-Reset', $headers);
}
}
public function testRateLimitingWithLimitsSetOnRoute()
{
$request = Request::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->once()->andReturn(false);
$route->shouldReceive('getRateLimit')->once()->andReturn(5);
$route->shouldReceive('getRateLimitExpiration')->once()->andReturn(10);
$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
$response = $this->middleware->handle($request, function ($request) {
return new Response('foo');
});
$this->assertArrayHasKey('x-ratelimit-limit', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-remaining', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-reset', $response->headers->all());
$this->assertSame(4, $response->headers->get('x-ratelimit-remaining'));
$this->assertSame(5, $response->headers->get('x-ratelimit-limit'));
}
public function testRateLimitingWithRouteThrottle()
{
$request = Request::create('test', 'GET');
$route = m::mock(Route::class);
$route->shouldReceive('hasThrottle')->once()->andReturn(true);
$route->shouldReceive('getThrottle')->once()->andReturn(new ThrottleStub(['limit' => 10, 'expires' => 20]));
$route->shouldReceive('getRateLimit')->once()->andReturn(0);
$route->shouldReceive('getRateLimitExpiration')->once()->andReturn(0);
$this->router->shouldReceive('getCurrentRoute')->once()->andReturn($route);
$response = $this->middleware->handle($request, function ($request) {
return new Response('foo');
});
$this->assertArrayHasKey('x-ratelimit-limit', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-remaining', $response->headers->all());
$this->assertArrayHasKey('x-ratelimit-reset', $response->headers->all());
$this->assertSame(9, $response->headers->get('x-ratelimit-remaining'));
$this->assertSame(10, $response->headers->get('x-ratelimit-limit'));
}
}