@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SimulationRateLimitTest extends TestCase
|
||||
{
|
||||
public function test_public_calculation_endpoint_limits_repeated_requests_per_anonymous_token(): void
|
||||
{
|
||||
$token = Str::random(48);
|
||||
$limiterKey = 'simulation-calculation:anonymous:'.hash('sha256', $token);
|
||||
RateLimiter::clear($limiterKey);
|
||||
|
||||
try {
|
||||
for ($attempt = 0; $attempt < 10; $attempt++) {
|
||||
$this->postJson('/api/v1/simulations', [], [
|
||||
'X-Simulation-Token' => $token,
|
||||
])->assertUnprocessable();
|
||||
}
|
||||
|
||||
$response = $this->postJson('/api/v1/simulations', [], [
|
||||
'X-Simulation-Token' => $token,
|
||||
])->assertTooManyRequests()->assertHeader('Retry-After');
|
||||
$this->assertStringContainsString('demasiados', (string) $response->json('message'));
|
||||
} finally {
|
||||
RateLimiter::clear($limiterKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user