2016-08-21 14:52:15 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-21 14:52:15 +03:00
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 11:22:00 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2016-08-21 14:52:15 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
|
|
|
|
use Zend\Diactoros\Response;
|
2018-12-26 01:01:30 +03:00
|
|
|
use Zend\Diactoros\ServerRequest;
|
2016-08-21 14:52:15 +03:00
|
|
|
use Zend\Diactoros\Stream;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-10-28 10:34:02 +03:00
|
|
|
use function array_shift;
|
2016-08-21 14:52:15 +03:00
|
|
|
|
|
|
|
class BodyParserMiddlewareTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var BodyParserMiddleware */
|
2016-08-21 14:52:15 +03:00
|
|
|
private $middleware;
|
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
|
|
|
$this->middleware = new BodyParserMiddleware();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-02-16 23:24:32 +03:00
|
|
|
* @dataProvider provideIgnoredRequestMethods
|
2016-08-21 14:52:15 +03:00
|
|
|
*/
|
2019-02-16 23:24:32 +03:00
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
2019-02-16 23:24:32 +03:00
|
|
|
$request = (new ServerRequest())->withMethod($method);
|
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request);
|
|
|
|
}
|
2016-08-21 14:52:15 +03:00
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
public function provideIgnoredRequestMethods(): iterable
|
|
|
|
{
|
2019-02-17 22:28:34 +03:00
|
|
|
yield 'GET' => ['GET'];
|
|
|
|
yield 'HEAD' => ['HEAD'];
|
|
|
|
yield 'OPTIONS' => ['OPTIONS'];
|
2019-02-16 23:24:32 +03:00
|
|
|
}
|
2017-03-25 11:22:00 +03:00
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
/** @test */
|
|
|
|
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
|
|
|
|
{
|
|
|
|
$request = (new ServerRequest())->withParsedBody(['foo' => 'bar'])->withMethod('POST');
|
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request);
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
private function assertHandlingRequestJustFallsBackToNext(ServerRequestInterface $request): void
|
|
|
|
{
|
|
|
|
$nextHandler = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$handle = $nextHandler->handle($request)->willReturn(new Response());
|
|
|
|
|
|
|
|
$this->middleware->process($request, $nextHandler->reveal());
|
|
|
|
|
|
|
|
$handle->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function jsonRequestsAreJsonDecoded(): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
2017-03-25 11:22:00 +03:00
|
|
|
$test = $this;
|
2016-08-21 14:52:15 +03:00
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('{"foo": "bar", "bar": ["one", 5]}');
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withMethod('PUT')
|
|
|
|
->withBody($body)
|
|
|
|
->withHeader('content-type', 'application/json');
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
|
2017-03-25 11:22:00 +03: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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
|
2019-02-16 23:24:32 +03:00
|
|
|
/** @test */
|
|
|
|
public function regularRequestsAreUrlDecoded(): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
2017-03-25 11:22:00 +03:00
|
|
|
$test = $this;
|
2016-08-21 14:52:15 +03:00
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('foo=bar&bar[]=one&bar[]=5');
|
2018-12-26 01:01:30 +03:00
|
|
|
$request = (new ServerRequest())->withMethod('PUT')
|
|
|
|
->withBody($body);
|
2018-03-26 20:02:41 +03:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will(
|
2017-03-25 11:22:00 +03: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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
2018-11-11 15:18:21 +03:00
|
|
|
$process->shouldHaveBeenCalledOnce();
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
}
|