From c71213673fff3a83471ade4535087c77d2d2f4d9 Mon Sep 17 00:00:00 2001 From: Ben Comeau Date: Thu, 1 Mar 2018 12:51:38 -0800 Subject: [PATCH 1/4] Check the instance of the thrown exception --- tests/Integration/Http/ThrottleRequestsTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Integration/Http/ThrottleRequestsTest.php b/tests/Integration/Http/ThrottleRequestsTest.php index 45c5b7e9c420..789bce851976 100644 --- a/tests/Integration/Http/ThrottleRequestsTest.php +++ b/tests/Integration/Http/ThrottleRequestsTest.php @@ -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 @@ -49,6 +50,7 @@ public function test_lock_opens_immediately_after_decay() try { $this->withoutExceptionHandling()->get('/'); } catch (Throwable $e) { + $this->assertTrue($e instanceof ThrottleRequestsException); $this->assertEquals(429, $e->getStatusCode()); $this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']); $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']); From 1cc5997b219c8f0ac74cf78405bdcd1113f31bb7 Mon Sep 17 00:00:00 2001 From: Ben Comeau Date: Thu, 1 Mar 2018 12:53:09 -0800 Subject: [PATCH 2/4] Extend HttpException with new ThrottleRequestsException class --- .../Exceptions/ThrottleRequestsException.php | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Illuminate/Http/Exceptions/ThrottleRequestsException.php diff --git a/src/Illuminate/Http/Exceptions/ThrottleRequestsException.php b/src/Illuminate/Http/Exceptions/ThrottleRequestsException.php new file mode 100644 index 000000000000..1640c3e67374 --- /dev/null +++ b/src/Illuminate/Http/Exceptions/ThrottleRequestsException.php @@ -0,0 +1,23 @@ + Date: Thu, 1 Mar 2018 12:53:30 -0800 Subject: [PATCH 3/4] Throw ThrottleRequestsException during throttling of requests --- src/Illuminate/Routing/Middleware/ThrottleRequests.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Routing/Middleware/ThrottleRequests.php b/src/Illuminate/Routing/Middleware/ThrottleRequests.php index 11419e58e0cb..c49f24c6f8cb 100644 --- a/src/Illuminate/Routing/Middleware/ThrottleRequests.php +++ b/src/Illuminate/Routing/Middleware/ThrottleRequests.php @@ -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 { @@ -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) { @@ -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) { @@ -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 ); } From 236a0e8feebd800b6bd49be90833236b0dc8e39f Mon Sep 17 00:00:00 2001 From: Ben Comeau Date: Fri, 2 Mar 2018 08:02:41 -0800 Subject: [PATCH 4/4] Update assertion to a more appropriate method --- tests/Integration/Http/ThrottleRequestsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Http/ThrottleRequestsTest.php b/tests/Integration/Http/ThrottleRequestsTest.php index 789bce851976..08660404df95 100644 --- a/tests/Integration/Http/ThrottleRequestsTest.php +++ b/tests/Integration/Http/ThrottleRequestsTest.php @@ -50,7 +50,7 @@ public function test_lock_opens_immediately_after_decay() try { $this->withoutExceptionHandling()->get('/'); } catch (Throwable $e) { - $this->assertTrue($e instanceof ThrottleRequestsException); + $this->assertInstanceOf(ThrottleRequestsException::class, $e); $this->assertEquals(429, $e->getStatusCode()); $this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']); $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']);