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')); } }