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

73 lines
2.6 KiB
PHP
Raw Normal View History

2016-07-19 19:19:05 +03:00
<?php
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
2016-07-19 19:19:05 +03:00
namespace ShlinkioTest\Shlink\Rest\Middleware;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
2018-03-26 20:02:41 +03:00
use Psr\Http\Server\RequestHandlerInterface;
2016-07-19 19:19:05 +03:00
use Shlinkio\Shlink\Rest\Middleware\CrossDomainMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
2016-07-19 19:19:05 +03:00
class CrossDomainMiddlewareTest extends TestCase
{
/** @var CrossDomainMiddleware */
private $middleware;
/** @var ObjectProphecy */
private $delegate;
2016-07-19 19:19:05 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-07-19 19:19:05 +03:00
{
$this->middleware = new CrossDomainMiddleware();
2018-03-26 20:02:41 +03:00
$this->delegate = $this->prophesize(RequestHandlerInterface::class);
2016-07-19 19:19:05 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function nonCrossDomainRequestsAreNotAffected()
2016-07-19 19:19:05 +03:00
{
$originalResponse = new Response();
2018-11-11 15:18:21 +03:00
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(new ServerRequest(), $this->delegate->reveal());
$this->assertSame($originalResponse, $response);
$headers = $response->getHeaders();
$this->assertArrayNotHasKey('Access-Control-Allow-Origin', $headers);
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
}
2019-02-17 22:28:34 +03:00
/** @test */
public function anyRequestIncludesTheAllowAccessHeader()
{
$originalResponse = new Response();
2018-11-11 15:18:21 +03:00
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
$response = $this->middleware->process(
(new ServerRequest())->withHeader('Origin', 'local'),
$this->delegate->reveal()
2016-07-19 19:19:05 +03:00
);
$this->assertNotSame($originalResponse, $response);
2016-07-19 19:19:05 +03:00
$headers = $response->getHeaders();
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
$this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers);
}
2019-02-17 22:28:34 +03:00
/** @test */
2016-07-19 19:19:05 +03:00
public function optionsRequestIncludesMoreHeaders()
{
$originalResponse = new Response();
$request = (new ServerRequest())->withMethod('OPTIONS')->withHeader('Origin', 'local');
2018-11-11 15:18:21 +03:00
$this->delegate->handle(Argument::any())->willReturn($originalResponse)->shouldBeCalledOnce();
2016-07-19 19:19:05 +03:00
$response = $this->middleware->process($request, $this->delegate->reveal());
$this->assertNotSame($originalResponse, $response);
2016-07-19 19:19:05 +03:00
$headers = $response->getHeaders();
$this->assertArrayHasKey('Access-Control-Allow-Origin', $headers);
$this->assertArrayHasKey('Access-Control-Allow-Headers', $headers);
}
}