shlink/module/Rest/test/Middleware/CrossDomainMiddlewareTest.php

119 lines
4.6 KiB
PHP
Raw Normal View History

2016-07-19 11:19:05 -05:00
<?php
2019-10-05 10:26:10 -05:00
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;
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;
use Zend\Diactoros\ServerRequest;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use function Zend\Stratigility\middleware;
2016-07-19 11:19:05 -05:00
class CrossDomainMiddlewareTest extends TestCase
{
/** @var CrossDomainMiddleware */
private $middleware;
/** @var ObjectProphecy */
private $handler;
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();
$this->handler = $this->prophesize(RequestHandlerInterface::class);
2016-07-19 11:19:05 -05:00
}
2019-02-17 13:28:34 -06:00
/** @test */
public function nonCrossDomainRequestsAreNotAffected(): void
2016-07-19 11:19:05 -05:00
{
$originalResponse = new Response();
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(new ServerRequest(), $this->handler->reveal());
$this->assertSame($originalResponse, $response);
$headers = $response->getHeaders();
$this->assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
$this->assertArrayNotHasKey('Access-Control-Expose-Headers', $headers);
$this->assertArrayNotHasKey('Access-Control-Allow-Methods', $headers);
$this->assertArrayNotHasKey('Access-Control-Max-Age', $headers);
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
}
2019-02-17 13:28:34 -06:00
/** @test */
public function anyRequestIncludesTheAllowAccessHeader(): void
{
$originalResponse = new Response();
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(
(new ServerRequest())->withHeader('Origin', 'local'),
$this->handler->reveal()
2016-07-19 11:19:05 -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-Expose-Headers', $headers);
$this->assertArrayNotHasKey('Access-Control-Allow-Methods', $headers);
$this->assertArrayNotHasKey('Access-Control-Max-Age', $headers);
2016-07-19 11:19:05 -05:00
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
}
2019-02-17 13:28:34 -06:00
/** @test */
public function optionsRequestIncludesMoreHeaders(): void
2016-07-19 11:19:05 -05:00
{
$originalResponse = new Response();
$request = (new ServerRequest())->withMethod('OPTIONS')->withHeader('Origin', 'local');
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
2016-07-19 11:19:05 -05:00
$response = $this->middleware->process($request, $this->handler->reveal());
$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-Expose-Headers', $headers);
$this->assertArrayHasKey('Access-Control-Allow-Methods', $headers);
$this->assertArrayHasKey('Access-Control-Max-Age', $headers);
2016-07-19 11:19:05 -05:00
$this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
}
/**
* @test
* @dataProvider provideRouteResults
*/
public function optionsRequestParsesRouteMatchToDetermineAllowedMethods(
?RouteResult $result,
string $expectedAllowedMethods
): void {
$originalResponse = new Response();
$request = (new ServerRequest())->withAttribute(RouteResult::class, $result)
->withMethod('OPTIONS')
->withHeader('Origin', 'local');
$this->handler->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process($request, $this->handler->reveal());
$this->assertEquals($response->getHeaderLine('Access-Control-Allow-Methods'), $expectedAllowedMethods);
}
public function provideRouteResults(): iterable
{
yield 'with no route result' => [null, 'GET,POST,PUT,PATCH,DELETE,OPTIONS'];
yield 'with failed route result' => [RouteResult::fromRouteFailure(['POST', 'GET']), 'POST,GET'];
yield 'with success route result' => [
RouteResult::fromRoute(
new Route('/', middleware(function () {
}), ['DELETE', 'PATCH', 'PUT'])
),
'DELETE,PATCH,PUT',
];
}
2016-07-19 11:19:05 -05:00
}