2016-07-19 11:19:05 -05:00
|
|
|
<?php
|
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
2017-03-25 03:44:34 -05:00
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 03:44:34 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-07-19 11:19:05 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
|
|
|
|
class CrossDomainMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var CrossDomainMiddleware
|
|
|
|
*/
|
|
|
|
protected $middleware;
|
2017-03-25 03:44:34 -05:00
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $delegate;
|
2016-07-19 11:19:05 -05:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->middleware = new CrossDomainMiddleware();
|
2017-03-25 03:44:34 -05:00
|
|
|
$this->delegate = $this->prophesize(DelegateInterface::class);
|
2016-07-19 11:19:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2016-07-19 13:20:18 -05:00
|
|
|
public function nonCrossDomainRequestsAreNotAffected()
|
2016-07-19 11:19:05 -05:00
|
|
|
{
|
2016-07-19 13:20:18 -05:00
|
|
|
$originalResponse = new Response();
|
2017-03-25 03:44:34 -05:00
|
|
|
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
|
|
|
|
|
|
|
$response = $this->middleware->process(ServerRequestFactory::fromGlobals(), $this->delegate->reveal());
|
2016-07-19 13:20:18 -05:00
|
|
|
$this->assertSame($originalResponse, $response);
|
|
|
|
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
|
|
|
|
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function anyRequestIncludesTheAllowAccessHeader()
|
|
|
|
{
|
|
|
|
$originalResponse = new Response();
|
2017-03-25 03:44:34 -05:00
|
|
|
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
|
|
|
|
|
|
|
$response = $this->middleware->process(
|
2016-07-19 13:20:18 -05:00
|
|
|
ServerRequestFactory::fromGlobals()->withHeader('Origin', 'local'),
|
2017-03-25 03:44:34 -05:00
|
|
|
$this->delegate->reveal()
|
2016-07-19 11:19:05 -05:00
|
|
|
);
|
2016-07-19 13:20:18 -05:00
|
|
|
$this->assertNotSame($originalResponse, $response);
|
2016-07-19 11:19:05 -05:00
|
|
|
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
|
|
|
|
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function optionsRequestIncludesMoreHeaders()
|
|
|
|
{
|
2016-07-19 13:20:18 -05:00
|
|
|
$originalResponse = new Response();
|
2017-03-25 03:44:34 -05:00
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('OPTIONS')->withHeader('Origin', 'local');
|
|
|
|
$this->delegate->process(Argument::any())->willReturn($originalResponse)->shouldbeCalledTimes(1);
|
2016-07-19 11:19:05 -05:00
|
|
|
|
2017-03-25 03:44:34 -05:00
|
|
|
$response = $this->middleware->process($request, $this->delegate->reveal());
|
2016-07-19 13:20:18 -05:00
|
|
|
$this->assertNotSame($originalResponse, $response);
|
2016-07-19 11:19:05 -05:00
|
|
|
|
|
|
|
$headers = $response->getHeaders();
|
|
|
|
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
|
|
|
|
$this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
|
|
|
|
}
|
|
|
|
}
|