mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-18 08:19:55 +03:00
Migrated CrossDomainMiddleware to psr-15 middleware
This commit is contained in:
parent
9bd18ee041
commit
cd47aae902
2 changed files with 30 additions and 43 deletions
module/Rest
|
@ -1,41 +1,26 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Shlinkio\Shlink\Rest\Middleware;
|
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\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Zend\Stratigility\MiddlewareInterface;
|
|
||||||
|
|
||||||
class CrossDomainMiddleware implements MiddlewareInterface
|
class CrossDomainMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Process an incoming request and/or response.
|
* Process an incoming server request and return a response, optionally delegating
|
||||||
*
|
* to the next middleware component to create the 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.
|
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param DelegateInterface $delegate
|
||||||
* @param null|callable $out
|
*
|
||||||
* @return null|Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
public function process(Request $request, DelegateInterface $delegate)
|
||||||
{
|
{
|
||||||
/** @var Response $response */
|
/** @var Response $response */
|
||||||
$response = $out($request, $response);
|
$response = $delegate->process($request);
|
||||||
if (! $request->hasHeader('Origin')) {
|
if (! $request->hasHeader('Origin')) {
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
@ -49,9 +34,9 @@ class CrossDomainMiddleware implements MiddlewareInterface
|
||||||
|
|
||||||
// Add OPTIONS-specific headers
|
// Add OPTIONS-specific headers
|
||||||
foreach ([
|
foreach ([
|
||||||
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', // TODO Should be based on path
|
'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS', // TODO Should be based on path
|
||||||
'Access-Control-Max-Age' => '1000',
|
'Access-Control-Max-Age' => '1000',
|
||||||
'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
|
'Access-Control-Allow-Headers' => $request->getHeaderLine('Access-Control-Request-Headers'),
|
||||||
] as $key => $value) {
|
] as $key => $value) {
|
||||||
$response = $response->withHeader($key, $value);
|
$response = $response->withHeader($key, $value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
||||||
|
|
||||||
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Argument;
|
||||||
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
||||||
use Zend\Diactoros\Response;
|
use Zend\Diactoros\Response;
|
||||||
use Zend\Diactoros\ServerRequestFactory;
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
|
@ -12,10 +15,15 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||||
* @var CrossDomainMiddleware
|
* @var CrossDomainMiddleware
|
||||||
*/
|
*/
|
||||||
protected $middleware;
|
protected $middleware;
|
||||||
|
/**
|
||||||
|
* @var ObjectProphecy
|
||||||
|
*/
|
||||||
|
protected $delegate;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->middleware = new CrossDomainMiddleware();
|
$this->middleware = new CrossDomainMiddleware();
|
||||||
|
$this->delegate = $this->prophesize(DelegateInterface::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,13 +32,9 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||||
public function nonCrossDomainRequestsAreNotAffected()
|
public function nonCrossDomainRequestsAreNotAffected()
|
||||||
{
|
{
|
||||||
$originalResponse = new Response();
|
$originalResponse = new Response();
|
||||||
$response = $this->middleware->__invoke(
|
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
||||||
ServerRequestFactory::fromGlobals(),
|
|
||||||
$originalResponse,
|
$response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal());
|
||||||
function ($req, $resp) {
|
|
||||||
return $resp;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
$this->assertSame($originalResponse, $response);
|
$this->assertSame($originalResponse, $response);
|
||||||
|
|
||||||
$headers = $response->getHeaders();
|
$headers = $response->getHeaders();
|
||||||
|
@ -44,12 +48,11 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||||
public function anyRequestIncludesTheAllowAccessHeader()
|
public function anyRequestIncludesTheAllowAccessHeader()
|
||||||
{
|
{
|
||||||
$originalResponse = new Response();
|
$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'),
|
ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'),
|
||||||
$originalResponse,
|
$this->delegate->reveal()
|
||||||
function ($req, $resp) {
|
|
||||||
return $resp;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
$this->assertNotSame($originalResponse, $response);
|
$this->assertNotSame($originalResponse, $response);
|
||||||
|
|
||||||
|
@ -64,11 +67,10 @@ class CrossDomainMiddlewareTest extends TestCase
|
||||||
public function optionsRequestIncludesMoreHeaders()
|
public function optionsRequestIncludesMoreHeaders()
|
||||||
{
|
{
|
||||||
$originalResponse = new Response();
|
$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) {
|
$response = $this->middleware->process($request, $this->delegate->reveal());
|
||||||
return $resp;
|
|
||||||
});
|
|
||||||
$this->assertNotSame($originalResponse, $response);
|
$this->assertNotSame($originalResponse, $response);
|
||||||
|
|
||||||
$headers = $response->getHeaders();
|
$headers = $response->getHeaders();
|
||||||
|
|
Loading…
Add table
Reference in a new issue