Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Throttle requests exception #23358

Merged
merged 4 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Illuminate/Http/Exceptions/ThrottleRequestsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Http\Exceptions;

use Exception;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ThrottleRequestsException extends HttpException
{
/**
* TooManyAttemptsException constructor.
*
* @param string|null $message
* @param \Exception|null $previous
* @param array $headers
* @param int $code
* @return void
*/
public function __construct($message = null, Exception $previous = null, array $headers = [], $code = 0)
{
parent::__construct(429, $message, $previous, $headers, $code);
}
}
10 changes: 5 additions & 5 deletions src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Cache\RateLimiter;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Http\Exceptions\ThrottleRequestsException;

class ThrottleRequests
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public function __construct(RateLimiter $limiter)
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @return mixed
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1)
{
Expand Down Expand Up @@ -107,7 +107,7 @@ protected function resolveRequestSignature($request)
*
* @param string $key
* @param int $maxAttempts
* @return \Symfony\Component\HttpKernel\Exception\HttpException
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function buildException($key, $maxAttempts)
{
Expand All @@ -119,8 +119,8 @@ protected function buildException($key, $maxAttempts)
$retryAfter
);

return new HttpException(
429, 'Too Many Attempts.', null, $headers
return new ThrottleRequestsException(
'Too Many Attempts.', null, $headers
);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/Http/ThrottleRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Http\Exceptions\ThrottleRequestsException;

/**
* @group integration
Expand Down Expand Up @@ -49,6 +50,7 @@ public function test_lock_opens_immediately_after_decay()
try {
$this->withoutExceptionHandling()->get('/');
} catch (Throwable $e) {
$this->assertTrue($e instanceof ThrottleRequestsException);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use assertInstanceOf instead

$this->assertEquals(429, $e->getStatusCode());
$this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']);
$this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']);
Expand Down