This commit is contained in:
2025-04-25 22:27:47 -04:00
parent ac99a78bdf
commit d5167074a0
17 changed files with 173 additions and 30 deletions

View File

@@ -16,9 +16,12 @@ class CorsMiddlewareTest extends Unit
public function testAllowsConfiguredOrigin(): void
{
Config::shouldReceive('get')
->with('CORS_ALLOWED_ORIGINS', 'https://example.com,https://another.com')
->with('cors.allowed_origins')
->andReturn('https://example.com,https://another.com');
Config::shouldReceive('get')->with('cors.allow_credentials')->andReturn(false);
Config::shouldReceive('get')->with('cors.max_age')->andReturn('');
$middleware = new CorsMiddleware();
$request = new ServerRequest('GET', '/')->withHeader('Origin', 'https://example.com');
$handler = $this->mockHandler(new Response(200));
@@ -31,7 +34,7 @@ class CorsMiddlewareTest extends Unit
public function testBlocksUnconfiguredOrigin(): void
{
Config::shouldReceive('get')
->with('CORS_ALLOWED_ORIGINS', 'https://example.com,https://another.com')
->with('cors.allowed_origins')
->andReturn('https://example.com,https://another.com');
$middleware = new CorsMiddleware();
@@ -40,14 +43,14 @@ class CorsMiddlewareTest extends Unit
$response = $middleware->process($request, $handler);
$this->assertEquals('null', $response->getHeaderLine('Access-Control-Allow-Origin'));
$this->assertEmpty($response->getHeaderLine('Access-Control-Allow-Origin'));
}
public function testHandlesOptionsRequest(): void
{
Config::shouldReceive('get')->with('CORS_ALLOWED_ORIGINS', '...')->andReturn('https://example.com');
Config::shouldReceive('get')->with('CORS_ALLOW_CREDENTIALS', 'bool')->andReturn(false);
Config::shouldReceive('get')->with('CORS_MAX_AGE')->andReturn('86400');
Config::shouldReceive('get')->with('cors.allowed_origins')->andReturn('https://example.com');
Config::shouldReceive('get')->with('cors.allow_credentials')->andReturn(false);
Config::shouldReceive('get')->with('cors.max_age')->andReturn('86400');
$middleware = new CorsMiddleware();
$request = new ServerRequest('OPTIONS', '/')->withHeader('Origin', 'https://example.com');
@@ -61,8 +64,13 @@ class CorsMiddlewareTest extends Unit
public function testAddsAllowCredentialsHeader(): void
{
Config::shouldReceive('get')->with('CORS_ALLOWED_ORIGINS', '...')->andReturn('https://example.com');
Config::shouldReceive('get')->with('CORS_ALLOW_CREDENTIALS', 'bool')->andReturn(true);
Config::shouldReceive('get')
->with('cors.allowed_origins')
->andReturn('https://example.com');
Config::shouldReceive('get')->with('cors.allowed_origins')->andReturn('https://example.com');
Config::shouldReceive('get')->with('cors.allow_credentials')->andReturn(true);
Config::shouldReceive('get')->with('cors.max_age')->andReturn('86400');
$middleware = new CorsMiddleware();
$request = new ServerRequest('GET', '/')->withHeader('Origin', 'https://example.com');
@@ -91,3 +99,4 @@ class CorsMiddlewareTest extends Unit
};
}
}