-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Freddie\Hub\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use React\Http\Message\Response; | ||
|
||
final class CorsMiddleware | ||
{ | ||
public function __construct( | ||
private readonly ?string $corsOrigin = '*', | ||
) { | ||
} | ||
|
||
public function __invoke( | ||
ServerRequestInterface $request, | ||
callable $next | ||
): ResponseInterface { | ||
$response = $next($request); | ||
|
||
$corsOrigin = $this->corsOrigin ?? $this->getOrigin($request); | ||
|
||
return $response->withAddedHeader('Access-Control-Allow-Origin', $corsOrigin) | ||
->withAddedHeader('Access-Control-Allow-Headers', '*') | ||
->withAddedHeader('Access-Control-Allow-Methods', '*') | ||
->withAddedHeader('Access-Control-Allow-Credentials', 'true') | ||
->withStatus(Response::STATUS_OK); | ||
} | ||
|
||
private function getOrigin(ServerRequestInterface $request): string | ||
{ | ||
return $request->getHeaderLine('Origin') ?: 'null'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Freddie\Tests\Unit\Hub\Middleware; | ||
|
||
use FrameworkX\App; | ||
use Freddie\Hub\Middleware\CorsMiddleware; | ||
use React\Http\Message\Response; | ||
use React\Http\Message\ServerRequest; | ||
|
||
use function expect; | ||
use function Freddie\Tests\handle; | ||
use function it; | ||
|
||
it('decorates response with CORS mechanism', function () { | ||
// Given | ||
$corsOrigins = 'http://testdomain.local'; | ||
$expectedResponse = new Response(200); | ||
$app = new App(new CorsMiddleware($corsOrigins), fn () => $expectedResponse); | ||
$request = new ServerRequest('POST', './well-known/mercure'); | ||
|
||
// When | ||
$response = handle($app, $request); | ||
|
||
// Then | ||
expect($response->getHeaderLine('Access-Control-Allow-Origin'))->toBe($corsOrigins) | ||
->and($response->getHeaderLine('Access-Control-Allow-Methods'))->toBe('*') | ||
->and($response->getHeaderLine('Access-Control-Allow-Headers'))->toBe('*') | ||
->and($response->getHeaderLine('Access-Control-Allow-Credentials'))->toBe('true'); | ||
}); | ||
|
||
it('decorates response with CORS mechanism with default corsOrigin setup', function () { | ||
// Given | ||
$corsOrigins = null; | ||
$expectedResponse = new Response(400); | ||
$app = new App(new CorsMiddleware($corsOrigins), fn () => $expectedResponse); | ||
$request = new ServerRequest('POST', './well-known/mercure'); | ||
|
||
// When | ||
$response = handle($app, $request); | ||
|
||
// Then | ||
expect($response->getHeaderLine('Access-Control-Allow-Origin'))->toBe('null') | ||
->and($response->getHeaderLine('Access-Control-Allow-Methods'))->toBe('*') | ||
->and($response->getHeaderLine('Access-Control-Allow-Headers'))->toBe('*') | ||
->and($response->getHeaderLine('Access-Control-Allow-Credentials'))->toBe('true'); | ||
}); |