Is there a way to disable rate limiting on every/individual routes in Laravel?
I'm trying to test an endpoint that receives a lot of requests, but randomly Laravel will start responding with { status: 429, responseText: 'Too Many Attempts.' }
{ status: 429, responseText: 'Too Many Attempts.' }
for a few hundred requests which makes testing a huge pain.
In app/Http/Kernel.php
Laravel has a default throttle limit for all api routes.
protected $middlewareGroups = [
...
'api' => [
'throttle:60,1',
],
];
Comment or increase it.
You can actually disable only a certain middleware in tests.
use Illuminate\Routing\Middleware\ThrottleRequests;
class YourTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
...
}
Assuming you are using the API routes then you can change the throttle in app/Http/Kernel.php or take it off entirely. If you need to throttle for the other routes you can register the middleware for them separately.
(example below: throttle - 60 attempts then locked out for 1 minute)
'api' => [
'throttle:60,1',
'bindings',
],
您可以使用cache:clear
命令清除包括速率限制在内的缓存,如下所示:
php artisan cache:clear
In Laravel 5.7
Dynamic Rate Limiting You may specify a dynamic request maximum based on an attribute of the authenticated User model. For example, if your User model contains a rate_limit attribute, you may pass the name of the attribute to the throttle middleware so that it is used to calculate the maximum request count:
Route::middleware('auth:api', 'throttle:rate_limit,1')->group(function () {
Route::get('/user', function () {
//
});
});
If you want to disable just for automated tests, you can use the WithoutMiddleware
trait on your tests.
use Illuminate\Foundation\Testing\WithoutMiddleware;
class YourTest extends TestCase {
use WithoutMiddleware;
...
Otherwise, just remove the 'throttle:60,1',
line from your Kernel file ( app/Http/Kernel.php
), and your problem will be solved.
A non-hacky way to increase the throttle in unit tests to avoid the dreaded 429:
$requestsPerMinute = ENV("REQUESTS_PER_MINUTE", 60);
Route::middleware(["auth:api", "throttle:$requestsPerMinute,1"])->group(function(){
// your routes
});
<server name="REQUESTS_PER_MINUTE" value="500"/>
You can add the following lines in your app/Http/Kernel.php
'api' => [
'throttle:120,1',
'bindings',
\App\Library\Cobalt\Http\Middleware\LogMiddleware::class,
],
If the problem persists try the artisan command php artisan cache:clear
If you are using Laravel 8.x or later you can use RateLimiter with the following steps:
In your app/Providers/RouteServiceProvider.php find below configureRateLimiting: protected function configureRateLimiting() {
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
// no limit throttle
RateLimiter::for('none', function (Request $request) {
return Limit::none();
});
}
In your app/web.php add 'throttle:none:
Route::group(['middleware' => ['auth', 'throttle:none']], function ($router) {
Route::post('test', 'TestController@test');
});
In my case, I just changed the default value of perMinute()
in `App\\Providers\\RouteServiceProvider.
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
$perMinute = env('APP_ENV') === 'testing' ? 1000 : 60;
return Limit::perMinute($perMinute)
->by(optional($request->user())->id ?: $request->ip());
});
}
You can remove or comment out the line (throttle:60,1)
from App\Http\Kernel.php.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.