2016-07-19 11:19:05 -05:00
|
|
|
<?php
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 11:19:05 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
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;
|
2018-03-26 12:02:41 -05:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-07-19 11:19:05 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
2018-12-25 16:01:30 -06:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-07-19 11:19:05 -05:00
|
|
|
|
|
|
|
class CrossDomainMiddlewareTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var CrossDomainMiddleware */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $middleware;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $delegate;
|
2016-07-19 11:19:05 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-19 11:19:05 -05:00
|
|
|
{
|
|
|
|
$this->middleware = new CrossDomainMiddleware();
|
2018-03-26 12:02:41 -05:00
|
|
|
$this->delegate = $this->prophesize(RequestHandlerInterface::class);
|
2016-07-19 11:19:05 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06: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();
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
2017-03-25 03:44:34 -05:00
|
|
|
|
2018-12-25 16:01:30 -06:00
|
|
|
$response = $this->middleware->process(new ServerRequest(), $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);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-07-19 13:20:18 -05:00
|
|
|
public function anyRequestIncludesTheAllowAccessHeader()
|
|
|
|
{
|
|
|
|
$originalResponse = new Response();
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
2017-03-25 03:44:34 -05:00
|
|
|
|
|
|
|
$response = $this->middleware->process(
|
2018-12-25 16:01:30 -06:00
|
|
|
(new ServerRequest())->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);
|
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2016-07-19 11:19:05 -05:00
|
|
|
public function optionsRequestIncludesMoreHeaders()
|
|
|
|
{
|
2016-07-19 13:20:18 -05:00
|
|
|
$originalResponse = new Response();
|
2018-12-25 16:01:30 -06:00
|
|
|
$request = (new ServerRequest())->withMethod('OPTIONS')->withHeader('Origin', 'local');
|
2018-11-11 06:18:21 -06:00
|
|
|
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
|
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);
|
|
|
|
}
|
|
|
|
}
|