|
10 | 10 | use Illuminate\Foundation\Auth\User;
|
11 | 11 | use Illuminate\Foundation\Testing\RefreshDatabase;
|
12 | 12 | use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
| 13 | +use Illuminate\Http\Request; |
13 | 14 | use Illuminate\Routing\Exceptions\MissingRateLimiterException;
|
14 | 15 | use Illuminate\Routing\Middleware\ThrottleRequests;
|
15 | 16 | use Illuminate\Support\Carbon;
|
| 17 | +use Illuminate\Support\Facades\Cache; |
| 18 | +use Illuminate\Support\Facades\Date; |
| 19 | +use Illuminate\Support\Facades\RateLimiter as RateLimiterFacade; |
16 | 20 | use Illuminate\Support\Facades\Route;
|
17 | 21 | use Orchestra\Testbench\Attributes\WithConfig;
|
18 | 22 | use Orchestra\Testbench\Attributes\WithMigration;
|
19 | 23 | use Orchestra\Testbench\TestCase;
|
20 | 24 | use PHPUnit\Framework\Attributes\DataProvider;
|
| 25 | +use Symfony\Component\HttpFoundation\Response; |
21 | 26 | use Throwable;
|
22 | 27 |
|
23 | 28 | #[WithConfig('hashing.driver', 'bcrypt')]
|
@@ -410,6 +415,81 @@ public function testItFallbacksToUserAccessorWhenThereIsNoNamedLimiterWhenAuthen
|
410 | 415 | $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
|
411 | 416 | }
|
412 | 417 | }
|
| 418 | + |
| 419 | + public function testItCanThrottleBasedOnResponse() |
| 420 | + { |
| 421 | + RateLimiterFacade::for('throttle-not-found', function (Request $request) { |
| 422 | + return Limit::perMinute(1)->after(fn ($response) => $response->status() === 404); |
| 423 | + }); |
| 424 | + Route::get('/', fn () => match(request('status')) { |
| 425 | + '404' => abort(404), |
| 426 | + default => 'ok', |
| 427 | + })->middleware(ThrottleRequests::using('throttle-not-found')); |
| 428 | + |
| 429 | + $this->travelTo('2000-01-01 00:00:00'); |
| 430 | + $this->get('?status=404')->assertNotFound(); |
| 431 | + $this->get('?status=404')->assertTooManyRequests(); |
| 432 | + $this->get('?status=404')->assertTooManyRequests(); |
| 433 | + |
| 434 | + $this->travelTo('2000-01-01 00:00:59'); |
| 435 | + $this->get('?status=404')->assertTooManyRequests(); |
| 436 | + $this->get('?status=404')->assertTooManyRequests(); |
| 437 | + |
| 438 | + $this->travelTo('2000-01-01 00:01:00'); |
| 439 | + $this->get('?status=404')->assertNotFound(); |
| 440 | + $this->get('?status=404')->assertTooManyRequests(); |
| 441 | + $this->get('?status=404')->assertTooManyRequests(); |
| 442 | + |
| 443 | + $this->travelTo('2000-01-01 00:01:59'); |
| 444 | + $this->get('?status=404')->assertTooManyRequests(); |
| 445 | + $this->get('?status=404')->assertTooManyRequests(); |
| 446 | + |
| 447 | + $this->travelTo('2000-01-01 00:02:00'); |
| 448 | + $this->get('?status=404')->assertNotFound(); |
| 449 | + $this->get('?status=404')->assertTooManyRequests(); |
| 450 | + $this->get('?status=404')->assertTooManyRequests(); |
| 451 | + } |
| 452 | + |
| 453 | + public function testItDoesNotHitLimiterUntilResponseHasBeenGenerated() |
| 454 | + { |
| 455 | + ThrottleRequests::shouldHashKeys(false); |
| 456 | + RateLimiterFacade::for('throttle-not-found', function (Request $request) { |
| 457 | + return Limit::perMinute(1)->after(fn ($response) => $response->status() === 404); |
| 458 | + }); |
| 459 | + $duringRequest = null; |
| 460 | + Route::get('/', function () use (&$duringRequest) { |
| 461 | + $duringRequest = [ |
| 462 | + Cache::get('throttle-not-found:'), |
| 463 | + Cache::get('throttle-not-found::timer'), |
| 464 | + ]; |
| 465 | + |
| 466 | + abort(404); |
| 467 | + })->middleware(ThrottleRequests::using('throttle-not-found')); |
| 468 | + |
| 469 | + $this->travelTo('2000-01-01 00:00:00'); |
| 470 | + $this->get('?status=404')->assertNotFound(); |
| 471 | + |
| 472 | + $this->assertSame([null, null], $duringRequest); |
| 473 | + $this->assertSame([1, 946684860], [ |
| 474 | + Cache::get('throttle-not-found:'), |
| 475 | + Cache::get('throttle-not-found::timer'), |
| 476 | + ]); |
| 477 | + } |
| 478 | + |
| 479 | + public function testItReturnsConfiguredResponseWhenUsingAfterLimit(): void |
| 480 | + { |
| 481 | + ThrottleRequests::shouldHashKeys(false); |
| 482 | + RateLimiterFacade::for('throttle-not-found', function (Request $request) { |
| 483 | + return Limit::perMinute(1) |
| 484 | + ->after(fn ($response) => $response->status() === 404) |
| 485 | + ->response(fn () => response('ah ah ah', status: 429)); |
| 486 | + }); |
| 487 | + Route::get('/', fn () => abort(404))->middleware(ThrottleRequests::using('throttle-not-found')); |
| 488 | + |
| 489 | + $this->travelTo('2000-01-01 00:00:00'); |
| 490 | + $this->get('?status=404')->assertNotFound(); |
| 491 | + $this->get('?status=404')->assertTooManyRequests()->assertContent('ah ah ah'); |
| 492 | + } |
413 | 493 | }
|
414 | 494 |
|
415 | 495 | class UserWithAccessor extends User
|
|
0 commit comments