| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace Illuminate\Tests\Integration\Http;
- use Illuminate\Cache\RateLimiter;
- use Illuminate\Cache\RateLimiting\GlobalLimit;
- use Illuminate\Container\Container;
- use Illuminate\Http\Exceptions\ThrottleRequestsException;
- use Illuminate\Routing\Middleware\ThrottleRequests;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Route;
- use Orchestra\Testbench\TestCase;
- use Throwable;
- class ThrottleRequestsTest extends TestCase
- {
- protected function tearDown(): void
- {
- parent::tearDown();
- Carbon::setTestNow(null);
- }
- public function getEnvironmentSetUp($app)
- {
- $app['config']->set('hashing', ['driver' => 'bcrypt']);
- }
- public function testLockOpensImmediatelyAfterDecay()
- {
- Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));
- Route::get('/', function () {
- return 'yes';
- })->middleware(ThrottleRequests::class.':2,1');
- $response = $this->withoutExceptionHandling()->get('/');
- $this->assertSame('yes', $response->getContent());
- $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
- $this->assertEquals(1, $response->headers->get('X-RateLimit-Remaining'));
- $response = $this->withoutExceptionHandling()->get('/');
- $this->assertSame('yes', $response->getContent());
- $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
- $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));
- Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58));
- try {
- $this->withoutExceptionHandling()->get('/');
- } catch (Throwable $e) {
- $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']);
- $this->assertEquals(2, $e->getHeaders()['Retry-After']);
- $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
- }
- }
- public function testLimitingUsingNamedLimiter()
- {
- $rateLimiter = Container::getInstance()->make(RateLimiter::class);
- $rateLimiter->for('test', function ($request) {
- return new GlobalLimit(2, 1);
- });
- Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));
- Route::get('/', function () {
- return 'yes';
- })->middleware(ThrottleRequests::class.':test');
- $response = $this->withoutExceptionHandling()->get('/');
- $this->assertSame('yes', $response->getContent());
- $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
- $this->assertEquals(1, $response->headers->get('X-RateLimit-Remaining'));
- $response = $this->withoutExceptionHandling()->get('/');
- $this->assertSame('yes', $response->getContent());
- $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
- $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));
- Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58));
- try {
- $this->withoutExceptionHandling()->get('/');
- } catch (Throwable $e) {
- $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']);
- $this->assertEquals(2, $e->getHeaders()['Retry-After']);
- $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
- }
- }
- }
|