Files
camelia-controller/tests/Unit/Security/RateLimitTest.php
T
dgarcia 341d1bdc2c feat: initial commit of camelia/controllers
Framework-agnostic REST controller toolkit in vanilla PHP.

- Router with path parameters, 404 and 405 with an Allow header.
- Immutable Request, JsonRequest and Response; nothing is printed.
- CrudController over a ResourceGateway port the application implements.
- Composable Middleware/Pipeline chain.
- Security middlewares: BearerAuth, RequireRole, RateLimit, Cors.
- Transport middlewares: Logging, ContentNegotiation.
- Nine typed HTTP exceptions with snake_case JSON bodies.
- Declares PHP >=8.3 and no dependency on an ORM: ports are local.
- MIT license.
2026-11-15 10:14:27 -03:00

114 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Camelia\Controllers\Tests\Unit\Security;
use Camelia\Controllers\JsonRequest;
use Camelia\Controllers\Request;
use Camelia\Controllers\Response;
use Camelia\Controllers\Security\RateLimit;
use PHPUnit\Framework\TestCase;
final class RateLimitTest extends TestCase
{
private int $now = 1000;
private static function passThrough(Request $request): Response
{
return new Response(['ok' => true], 200, ['X-Custom' => 'kept']);
}
private function limiter(int $limit = 3, int $window = 60): RateLimit
{
return new RateLimit(limit: $limit, windowSeconds: $window, clock: fn(): int => $this->now);
}
public function testAllowsUpToLimit(): void
{
$limiter = $this->limiter(3);
for ($i = 1; $i <= 3; ++$i) {
self::assertSame(
200,
$limiter->process(new JsonRequest(), self::passThrough(...))->status(),
);
}
}
public function testExceedingLimitIs429(): void
{
$limiter = $this->limiter(2);
$limiter->process(new JsonRequest(), self::passThrough(...));
$limiter->process(new JsonRequest(), self::passThrough(...));
$response = $limiter->process(new JsonRequest(), self::passThrough(...));
self::assertSame(429, $response->status());
self::assertSame('too_many_requests', $response->body()['code']);
self::assertSame('60', $response->headers()['Retry-After']);
}
public function testHeadersPresentOnSuccess(): void
{
$response = $this->limiter(10)->process(new JsonRequest(), self::passThrough(...));
self::assertSame('10', $response->headers()['X-RateLimit-Limit']);
self::assertSame('9', $response->headers()['X-RateLimit-Remaining']);
self::assertSame('1060', $response->headers()['X-RateLimit-Reset']);
}
public function testKeepsHandlerHeaders(): void
{
$response = $this->limiter(10)->process(new JsonRequest(), self::passThrough(...));
self::assertSame('kept', $response->headers()['X-Custom']);
}
public function testWindowResetsAfterExpiry(): void
{
$limiter = $this->limiter(1);
self::assertSame(
200,
$limiter->process(new JsonRequest(), self::passThrough(...))->status(),
);
self::assertSame(
429,
$limiter->process(new JsonRequest(), self::passThrough(...))->status(),
);
$this->now += 61;
self::assertSame(
200,
$limiter->process(new JsonRequest(), self::passThrough(...))->status(),
);
}
public function testBucketsAreSeparatedByClientAddress(): void
{
$limiter = $this->limiter(1);
$first = (new JsonRequest())->withAttribute('client_ip', '10.0.0.1');
$second = (new JsonRequest())->withAttribute('client_ip', '10.0.0.2');
self::assertSame(200, $limiter->process($first, self::passThrough(...))->status());
self::assertSame(200, $limiter->process($second, self::passThrough(...))->status());
self::assertSame(429, $limiter->process($first, self::passThrough(...))->status());
}
public function testResetInReportsRemainingWindow(): void
{
$limiter = $this->limiter(1);
$request = new JsonRequest();
self::assertSame(0, $limiter->resetIn($request));
$limiter->process($request, self::passThrough(...));
self::assertSame(60, $limiter->resetIn($request));
$this->now += 20;
self::assertSame(40, $limiter->resetIn($request));
}
}