Added additional headers
parent
23c0fae36f
commit
fd37c9d60e
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Middleware;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AddHeaders implements MiddlewareInterface
|
||||
{
|
||||
/** @var Config */
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
*/
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an incoming server request and setting the locale if required
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$response = $handler->handle($request);
|
||||
if (!$this->config->get('add_headers', true)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$headers = $this->config->get('headers', []);
|
||||
|
||||
foreach ($headers as $name => $value) {
|
||||
$response = $response->withAddedHeader($name, $value);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Middleware;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Http\Response;
|
||||
use Engelsystem\Middleware\AddHeaders;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class AddHeadersTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Middleware\AddHeaders::__construct
|
||||
* @covers \Engelsystem\Middleware\AddHeaders::process
|
||||
*/
|
||||
public function testRegister()
|
||||
{
|
||||
/** @var ServerRequestInterface|MockObject $request */
|
||||
$request = $this->getMockForAbstractClass(ServerRequestInterface::class);
|
||||
/** @var RequestHandlerInterface|MockObject $handler */
|
||||
$handler = $this->getMockForAbstractClass(RequestHandlerInterface::class);
|
||||
$response = new Response();
|
||||
|
||||
$handler->expects($this->atLeastOnce())
|
||||
->method('handle')
|
||||
->willReturn($response);
|
||||
|
||||
$config = new Config(['add_headers' => false]);
|
||||
|
||||
$middleware = new AddHeaders($config);
|
||||
$this->assertEquals($response, $middleware->process($request, $handler));
|
||||
|
||||
$config->set('add_headers', true);
|
||||
$config->set('headers', ['Foo-Header' => 'bar!']);
|
||||
$return = $middleware->process($request, $handler);
|
||||
|
||||
$this->assertNotEquals($response, $return);
|
||||
$this->assertArraySubset(['Foo-Header' => ['bar!']], $return->getHeaders());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue