mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-25 18:30:23 -06:00
Migrated CrossDomainMiddleware to psr-15 middleware
This commit is contained in:
parent
9bd18ee041
commit
cd47aae902
@ -1,41 +1,26 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Middleware;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class CrossDomainMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* Process an incoming request and/or response.
|
||||
*
|
||||
* Accepts a server-side request and a response instance, and does
|
||||
* something with them.
|
||||
*
|
||||
* If the response is not complete and/or further processing would not
|
||||
* interfere with the work done in the middleware, or if the middleware
|
||||
* wants to delegate to another process, it can use the `$out` callable
|
||||
* if present.
|
||||
*
|
||||
* If the middleware does not return a value, execution of the current
|
||||
* request is considered complete, and the response instance provided will
|
||||
* be considered the response to return.
|
||||
*
|
||||
* Alternately, the middleware may return a response instance.
|
||||
*
|
||||
* Often, middleware will `return $out();`, with the assumption that a
|
||||
* later middleware will return a response.
|
||||
* Process an incoming server request and return a response, optionally delegating
|
||||
* to the next middleware component to create the response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param null|callable $out
|
||||
* @return null|Response
|
||||
* @param DelegateInterface $delegate
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
{
|
||||
/** @var Response $response */
|
||||
$response = $out($request, $response);
|
||||
$response = $delegate->process($request);
|
||||
if (! $request->hasHeader('Origin')) {
|
||||
return $response;
|
||||
}
|
||||
@ -49,9 +34,9 @@ class CrossDomainMiddleware implements MiddlewareInterface
|
||||
|
||||
// Add OPTIONS-specific headers
|
||||
foreach ([
|
||||
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', // TODO Should be based on path
|
||||
'Access-Control-Max-Age' => '1000',
|
||||
'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
|
||||
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', // TODO Should be based on path
|
||||
'Access-Control-Max-Age' => '1000',
|
||||
'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
|
||||
] as $key => $value) {
|
||||
$response = $response->withHeader($key, $value);
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
<?php
|
||||
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequestFactory;
|
||||
@ -12,10 +15,15 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||
* @var CrossDomainMiddleware
|
||||
*/
|
||||
protected $middleware;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $delegate;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->middleware = new CrossDomainMiddleware();
|
||||
$this->delegate = $this->prophesize(DelegateInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,13 +32,9 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||
public function nonCrossDomainRequestsAreNotAffected()
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$response = $this->middleware->__invoke(
|
||||
ServerRequestFactory::fromGlobals(),
|
||||
$originalResponse,
|
||||
function ($req, $resp) {
|
||||
return $resp;
|
||||
}
|
||||
);
|
||||
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
||||
|
||||
$response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal());
|
||||
$this->assertSame($originalResponse, $response);
|
||||
|
||||
$headers = $response->getHeaders();
|
||||
@ -44,12 +48,11 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||
public function anyRequestIncludesTheAllowAccessHeader()
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$response = $this->middleware->__invoke(
|
||||
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
||||
|
||||
$response = $this->middleware->process(
|
||||
ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'),
|
||||
$originalResponse,
|
||||
function ($req, $resp) {
|
||||
return $resp;
|
||||
}
|
||||
$this->delegate->reveal()
|
||||
);
|
||||
$this->assertNotSame($originalResponse, $response);
|
||||
|
||||
@ -64,11 +67,10 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||
public function optionsRequestIncludesMoreHeaders()
|
||||
{
|
||||
$originalResponse = new Response();
|
||||
$request = ServerRequestFactory::fromGlobals(['REQUEST_METHOD' => 'OPTIONS'])->withHeader('Origin', 'local');
|
||||
$request = ServerRequestFactory::fromGlobals()->withMethod('OPTIONS')->withHeader('Origin', 'local');
|
||||
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
||||
|
||||
$response = $this->middleware->__invoke($request, $originalResponse, function ($req, $resp) {
|
||||
return $resp;
|
||||
});
|
||||
$response = $this->middleware->process($request, $this->delegate->reveal());
|
||||
$this->assertNotSame($originalResponse, $response);
|
||||
|
||||
$headers = $response->getHeaders();
|
||||
|
Loading…
Reference in New Issue
Block a user