2016-08-21 06:52:15 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 06:52:15 -05:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Laminas\Diactoros\Stream;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 03:22:00 -05:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-12-01 03:58:48 -06:00
|
|
|
use Prophecy\Prophecy\ProphecyInterface;
|
2017-03-25 03:22:00 -05:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-03-26 12:02:41 -05:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-21 06:52:15 -05:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-10-28 02:34:02 -05:00
|
|
|
use function array_shift;
|
2016-08-21 06:52:15 -05:00
|
|
|
|
|
|
|
class BodyParserMiddlewareTest extends TestCase
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-29 15:48:40 -06:00
|
|
|
private BodyParserMiddleware $middleware;
|
2016-08-21 06:52:15 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-08-21 06:52:15 -05:00
|
|
|
{
|
|
|
|
$this->middleware = new BodyParserMiddleware();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-02-16 14:24:32 -06:00
|
|
|
* @dataProvider provideIgnoredRequestMethods
|
2016-08-21 06:52:15 -05:00
|
|
|
*/
|
2019-02-16 14:24:32 -06:00
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
|
2016-08-21 06:52:15 -05:00
|
|
|
{
|
2019-12-01 03:58:48 -06:00
|
|
|
$request = $this->prophesize(ServerRequestInterface::class);
|
|
|
|
$request->getMethod()->willReturn($method);
|
|
|
|
$request->getParsedBody()->willReturn([]);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertHandlingRequestJustFallsBackToNext($request);
|
2019-02-16 14:24:32 -06:00
|
|
|
}
|
2016-08-21 06:52:15 -05:00
|
|
|
|
2019-02-16 14:24:32 -06:00
|
|
|
public function provideIgnoredRequestMethods(): iterable
|
|
|
|
{
|
2019-02-17 13:28:34 -06:00
|
|
|
yield 'GET' => ['GET'];
|
|
|
|
yield 'HEAD' => ['HEAD'];
|
|
|
|
yield 'OPTIONS' => ['OPTIONS'];
|
2019-02-16 14:24:32 -06:00
|
|
|
}
|
2017-03-25 03:22:00 -05:00
|
|
|
|
2019-02-16 14:24:32 -06:00
|
|
|
/** @test */
|
|
|
|
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
|
|
|
|
{
|
2019-12-01 03:58:48 -06:00
|
|
|
$request = $this->prophesize(ServerRequestInterface::class);
|
|
|
|
$request->getMethod()->willReturn('POST');
|
|
|
|
$request->getParsedBody()->willReturn(['foo' => 'bar']);
|
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertHandlingRequestJustFallsBackToNext($request);
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:58:48 -06:00
|
|
|
private function assertHandlingRequestJustFallsBackToNext(ProphecyInterface $requestMock): void
|
2019-02-16 14:24:32 -06:00
|
|
|
{
|
2019-12-01 03:58:48 -06:00
|
|
|
$getContentType = $requestMock->getHeaderLine('Content-type')->willReturn('');
|
|
|
|
$request = $requestMock->reveal();
|
|
|
|
|
2019-02-16 14:24:32 -06:00
|
|
|
$nextHandler = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$handle = $nextHandler->handle($request)->willReturn(new Response());
|
|
|
|
|
|
|
|
$this->middleware->process($request, $nextHandler->reveal());
|
|
|
|
|
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
2019-12-01 03:58:48 -06:00
|
|
|
$getContentType->shouldNotHaveBeenCalled();
|
2019-02-16 14:24:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function jsonRequestsAreJsonDecoded(): void
|
2016-08-21 06:52:15 -05:00
|
|
|
{
|
2017-03-25 03:22:00 -05:00
|
|
|
$test = $this;
|
2016-08-21 06:52:15 -05:00
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('{"foo": "bar", "bar": ["one", 5]}');
|
2018-12-25 16:01:30 -06:00
|
|
|
$request = (new ServerRequest())->withMethod('PUT')
|
|
|
|
->withBody($body)
|
|
|
|
->withHeader('content-type', 'application/json');
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
|
2017-03-25 03:22:00 -05:00
|
|
|
function (array $args) use ($test) {
|
|
|
|
/** @var ServerRequestInterface $req */
|
|
|
|
$req = array_shift($args);
|
|
|
|
|
|
|
|
$test->assertEquals([
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => ['one', 5],
|
|
|
|
], $req->getParsedBody());
|
|
|
|
|
|
|
|
return new Response();
|
2020-01-01 13:48:31 -06:00
|
|
|
},
|
2017-03-25 03:22:00 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
2018-11-11 06:18:21 -06:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
|
2019-02-16 14:24:32 -06:00
|
|
|
/** @test */
|
|
|
|
public function regularRequestsAreUrlDecoded(): void
|
2016-08-21 06:52:15 -05:00
|
|
|
{
|
2017-03-25 03:22:00 -05:00
|
|
|
$test = $this;
|
2016-08-21 06:52:15 -05:00
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('foo=bar&bar[]=one&bar[]=5');
|
2018-12-25 16:01:30 -06:00
|
|
|
$request = (new ServerRequest())->withMethod('PUT')
|
|
|
|
->withBody($body);
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
|
2017-03-25 03:22:00 -05:00
|
|
|
function (array $args) use ($test) {
|
|
|
|
/** @var ServerRequestInterface $req */
|
|
|
|
$req = array_shift($args);
|
|
|
|
|
|
|
|
$test->assertEquals([
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => ['one', 5],
|
|
|
|
], $req->getParsedBody());
|
|
|
|
|
|
|
|
return new Response();
|
2020-01-01 13:48:31 -06:00
|
|
|
},
|
2017-03-25 03:22:00 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
2018-11-11 06:18:21 -06:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
}
|