As I move forward in my Laravel project I have several tests for controllers and now I'm facing with this issue.
Some of my tests are failing with this message:
Expected response status code [200] but received 429.
Failed asserting that 200 is identical to 429.
I tried to solve with these methods:
withoutMiddleware() to TestCase.php:public function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
REQUESTS_PER_MINUTE to phpunit.xml:<phpunit>
<php>
...
<server name="REQUESTS_PER_MINUTE" value="500"/>
</php>
</phpunit>
server {
location ~ \.php$ {
limit_req zone=one burst=5;
}
}
limit_req_zone $binary_remote_addr zone=one:10m rate=100r/s;
Neither solution helped.
I don't want to change the Laravel's throttle settings only because of testing. I think here need to be a valid solution for this without changing the framework's settings.
Any suggestion how can I solve this issue?
My solution to this, although not the most elegant was to replace the ThrottleMiddleware with my own and checking the env for testing, effectively disabling laravel's throttling for for phpunit. :
<?php
namespace App\Http\Middleware;
use Illuminate\Routing\Middleware\ThrottleRequestsWithRedis;
use Closure;
class ThrottleRequests extends ThrottleRequestsWithRedis
{
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1, $prefix = '')
{
if (config('app.env') === 'testing') {
return $next($request);
}
return parent::handle($request, $next, $maxAttempts, $decayMinutes, $prefix);
}
}
You will then need to update your kernel.php to use your new class e.g.
protected $routeMiddleware = [
...
'throttle' => \App\Http\Middleware\ThrottleRequests::class,
...
];
I was currently facing this same issue testing with Laravel 9.
I solved it adding inside the 'configureRateLimiting()' method in 'RouteServiceProvider.php' the following code:
RateLimiter::for('test', function (Request $request) {
return Limit::perMinute(XX)->by($request->ip());
});
Replace the 'XX' with the max amount of API requests per minute you want to allow during testing.
After that run:
php artisan config:clear
And:
php artisan cache:clear
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With