Tests / PHP 8.4 (push) Failing after 1m26s
Drop support for PHP 8.3 and lower. The package now declares >=8.4, and CI runs only on 8.4. - composer.json: require php >=8.4. - mago.toml: php-version = "8.4", matching the minimum supported version. - phpstan.neon: phpVersion 80400. - Upgrade phpstan/phpstan to ^2.3: 1.x cannot parse `new Foo()->bar()`, which the 8.4 formatter now emits. - Narrow ArrayGateway::count() to int, matching what the fixture actually returns. - CI: single PHP 8.4 job using the php:8.4-cli container instead of setup-php, which failed with a TLS handshake timeout on the runner. BREAKING CHANGE: PHP 8.4 is now required.
114 lines
3.6 KiB
PHP
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));
|
|
}
|
|
}
|