Skip to content

Commit 63d2b78

Browse files
committed
Test coverage for rate limiting middleware.
Signed-off-by: Jason Lewis <jason.lewis1991@gmail.com>
1 parent a9ef146 commit 63d2b78

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/Http/Middleware/RateLimit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ protected function exceededRateLimit()
151151
*
152152
* @return bool
153153
*/
154-
protected function isRequestAuthenticated()
154+
protected function isAuthenticatedRequest()
155155
{
156156
if ( ! is_null($this->authenticatedRequest))
157157
{
@@ -171,7 +171,7 @@ protected function prepareConfig($request)
171171
{
172172
$this->config = array_merge($this->config, $this->container->make('config')->get('api::rate_limiting'));
173173

174-
if ($this->isRequestAuthenticated())
174+
if ($this->isAuthenticatedRequest())
175175
{
176176
$this->config = array_merge(['exceeded' => $this->config['exceeded']], $this->config['authenticated']);
177177
}

tests/HttpMiddlewareRateLimitTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public function setUp()
2525

2626
$this->container->shouldReceive('boot')->atLeast()->once();
2727
$this->container->shouldReceive('make')->once()->with('dingo.api.auth')->andReturn($this->auth);
28-
29-
$this->auth->shouldReceive('check')->once()->andReturn(false);
3028
}
3129

3230

@@ -40,6 +38,8 @@ public function testWrappedKernelIsHandledForInternalRequests()
4038
{
4139
$request = InternalRequest::create('/', 'GET');
4240

41+
$this->auth->shouldReceive('check')->once()->andReturn(false);
42+
4343
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => []]));
4444
$this->app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn(new Response('test'));
4545
$this->assertEquals('test', $this->middleware->handle($request)->getContent());
@@ -50,6 +50,8 @@ public function testWrappedKernelIsHandledForRequestsNotTargettingTheApi()
5050
{
5151
$request = Request::create('/', 'GET');
5252

53+
$this->auth->shouldReceive('check')->once()->andReturn(false);
54+
5355
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => []]));
5456
$this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router);
5557

@@ -64,6 +66,8 @@ public function testWrappedKernelIsHandledForRequestsWhereRateLimitingIsDisabled
6466
{
6567
$request = Request::create('/', 'GET');
6668

69+
$this->auth->shouldReceive('check')->once()->andReturn(false);
70+
6771
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => ['unauthenticated' => ['limit' => 0]]]));
6872
$this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router);
6973

@@ -74,17 +78,34 @@ public function testWrappedKernelIsHandledForRequestsWhereRateLimitingIsDisabled
7478
}
7579

7680

81+
public function testAuthenticatedConfigurationIsUsedForAuthenticatedRequest()
82+
{
83+
$request = Request::create('/', 'GET');
84+
85+
$this->auth->shouldReceive('check')->once()->andReturn(true);
86+
87+
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => ['authenticated' => ['limit' => 0]]]));
88+
$this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router);
89+
90+
$this->router->shouldReceive('requestTargettingApi')->once()->with($request)->andReturn(true);
91+
92+
$this->app->shouldReceive('handle')->once()->with($request, HttpKernelInterface::MASTER_REQUEST, true)->andReturn(new Response('test'));
93+
$this->assertEquals('test', $this->middleware->handle($request)->getContent());
94+
}
95+
96+
7797
public function testWrappedKernelIsHandledWhenRateLimitHasNotBeenExceeded()
7898
{
7999
$request = Request::create('/', 'GET');
80100

101+
$this->auth->shouldReceive('check')->once()->andReturn(false);
102+
81103
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => ['unauthenticated' => ['limit' => 1, 'reset' => 1]]]));
82104
$this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router);
83105
$this->container->shouldReceive('make')->once()->with('cache')->andReturn($this->cache);
84106

85107
$this->router->shouldReceive('requestTargettingApi')->once()->with($request)->andReturn(true);
86108

87-
88109
$ip = $request->getClientIp();
89110

90111
$this->cache->shouldReceive('add')->once()->with('dingo:api:requests:'.$ip, 0, 1);
@@ -105,6 +126,8 @@ public function testForbiddenResponseIsReturnedWhenRateLimitIsExceeded()
105126
{
106127
$request = Request::create('/', 'GET');
107128

129+
$this->auth->shouldReceive('check')->once()->andReturn(false);
130+
108131
$this->container->shouldReceive('make')->once()->with('config')->andReturn(m::mock(['get' => ['unauthenticated' => ['limit' => 1, 'reset' => 1]]]));
109132
$this->container->shouldReceive('make')->once()->with('router')->andReturn($this->router);
110133
$this->container->shouldReceive('make')->once()->with('cache')->andReturn($this->cache);

0 commit comments

Comments
 (0)