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.
98 lines
3.1 KiB
PHP
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'));
|
|
}
|
|
}
|