ThrottleRequestsTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Illuminate\Tests\Integration\Http;
  3. use Illuminate\Cache\RateLimiter;
  4. use Illuminate\Cache\RateLimiting\GlobalLimit;
  5. use Illuminate\Container\Container;
  6. use Illuminate\Http\Exceptions\ThrottleRequestsException;
  7. use Illuminate\Routing\Middleware\ThrottleRequests;
  8. use Illuminate\Support\Carbon;
  9. use Illuminate\Support\Facades\Route;
  10. use Orchestra\Testbench\TestCase;
  11. use Throwable;
  12. class ThrottleRequestsTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. parent::tearDown();
  17. Carbon::setTestNow(null);
  18. }
  19. public function getEnvironmentSetUp($app)
  20. {
  21. $app['config']->set('hashing', ['driver' => 'bcrypt']);
  22. }
  23. public function testLockOpensImmediatelyAfterDecay()
  24. {
  25. Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));
  26. Route::get('/', function () {
  27. return 'yes';
  28. })->middleware(ThrottleRequests::class.':2,1');
  29. $response = $this->withoutExceptionHandling()->get('/');
  30. $this->assertSame('yes', $response->getContent());
  31. $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
  32. $this->assertEquals(1, $response->headers->get('X-RateLimit-Remaining'));
  33. $response = $this->withoutExceptionHandling()->get('/');
  34. $this->assertSame('yes', $response->getContent());
  35. $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
  36. $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));
  37. Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58));
  38. try {
  39. $this->withoutExceptionHandling()->get('/');
  40. } catch (Throwable $e) {
  41. $this->assertInstanceOf(ThrottleRequestsException::class, $e);
  42. $this->assertEquals(429, $e->getStatusCode());
  43. $this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']);
  44. $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']);
  45. $this->assertEquals(2, $e->getHeaders()['Retry-After']);
  46. $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
  47. }
  48. }
  49. public function testLimitingUsingNamedLimiter()
  50. {
  51. $rateLimiter = Container::getInstance()->make(RateLimiter::class);
  52. $rateLimiter->for('test', function ($request) {
  53. return new GlobalLimit(2, 1);
  54. });
  55. Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 0));
  56. Route::get('/', function () {
  57. return 'yes';
  58. })->middleware(ThrottleRequests::class.':test');
  59. $response = $this->withoutExceptionHandling()->get('/');
  60. $this->assertSame('yes', $response->getContent());
  61. $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
  62. $this->assertEquals(1, $response->headers->get('X-RateLimit-Remaining'));
  63. $response = $this->withoutExceptionHandling()->get('/');
  64. $this->assertSame('yes', $response->getContent());
  65. $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
  66. $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));
  67. Carbon::setTestNow(Carbon::create(2018, 1, 1, 0, 0, 58));
  68. try {
  69. $this->withoutExceptionHandling()->get('/');
  70. } catch (Throwable $e) {
  71. $this->assertInstanceOf(ThrottleRequestsException::class, $e);
  72. $this->assertEquals(429, $e->getStatusCode());
  73. $this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']);
  74. $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']);
  75. $this->assertEquals(2, $e->getHeaders()['Retry-After']);
  76. $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
  77. }
  78. }
  79. }