Files
camelia-controller/tests/Unit/Security/CorsTest.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

98 lines
3.1 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\Cors;
use PHPUnit\Framework\TestCase;
final class CorsTest extends TestCase
{
private static function passThrough(Request $request): Response
{
return new Response(['ok' => true]);
}
public function testPreflightIsAnsweredWithoutHandler(): void
{
$cors = new Cors();
$response = $cors->process(
new JsonRequest(method: 'OPTIONS', headers: ['origin' => 'https://app.test']),
static function (): Response {
self::fail('A preflight must not reach the handler.');
},
);
self::assertSame(204, $response->status());
self::assertSame('*', $response->headers()['Access-Control-Allow-Origin']);
self::assertStringContainsString(
'PATCH',
$response->headers()['Access-Control-Allow-Methods'],
);
}
public function testWildcardOriginIsAddedToResponse(): void
{
$response = (new Cors())->process(
new JsonRequest(headers: ['origin' => 'https://app.test']),
self::passThrough(...),
);
self::assertSame(200, $response->status());
self::assertSame('*', $response->headers()['Access-Control-Allow-Origin']);
self::assertSame('Origin', $response->headers()['Vary']);
}
public function testDisallowedOriginGetsNoAllowOrigin(): void
{
$cors = new Cors(allowedOrigins: ['https://app.test']);
$response = $cors->process(
new JsonRequest(headers: ['origin' => 'https://evil.test']),
self::passThrough(...),
);
self::assertFalse($response->hasHeader('Access-Control-Allow-Origin'));
}
public function testAllowedOriginIsEchoed(): void
{
$cors = new Cors(allowedOrigins: ['https://app.test']);
$response = $cors->process(
new JsonRequest(headers: ['origin' => 'https://app.test']),
self::passThrough(...),
);
self::assertSame('https://app.test', $response->headers()['Access-Control-Allow-Origin']);
}
public function testCredentialsWithWildcardEchoesOriginInstead(): void
{
$cors = new Cors(allowedOrigins: ['*'], allowCredentials: true);
$response = $cors->process(
new JsonRequest(headers: ['origin' => 'https://app.test']),
self::passThrough(...),
);
// `*` plus credentials is rejected by browsers, so the exact origin must be echoed.
self::assertSame('https://app.test', $response->headers()['Access-Control-Allow-Origin']);
self::assertSame('true', $response->headers()['Access-Control-Allow-Credentials']);
}
public function testCredentialsWithoutOriginSendsNoAllowOrigin(): void
{
$cors = new Cors(allowedOrigins: ['*'], allowCredentials: true);
$response = $cors->process(new JsonRequest(), self::passThrough(...));
self::assertFalse($response->hasHeader('Access-Control-Allow-Origin'));
}
}