2016-08-21 06:52:15 -05:00
|
|
|
<?php
|
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;
|
|
|
|
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 03:22:00 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\MethodProphecy;
|
|
|
|
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;
|
|
|
|
use Zend\Diactoros\Response;
|
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
|
|
|
use Zend\Diactoros\Stream;
|
|
|
|
|
|
|
|
class BodyParserMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var BodyParserMiddleware
|
|
|
|
*/
|
|
|
|
private $middleware;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->middleware = new BodyParserMiddleware();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
|
|
|
|
{
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('GET');
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 03:22:00 -05:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 12:02:41 -05:00
|
|
|
$process = $delegate->handle($request)->willReturn(new Response());
|
2016-08-21 06:52:15 -05:00
|
|
|
|
2017-03-25 03:22:00 -05:00
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function jsonRequestsAreJsonDecoded()
|
|
|
|
{
|
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]}');
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
|
|
|
|
->withBody($body)
|
|
|
|
->withHeader('content-type', 'application/json');
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 03:22:00 -05:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 12:02:41 -05:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function regularRequestsAreUrlDecoded()
|
|
|
|
{
|
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');
|
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
|
|
|
|
->withBody($body);
|
2018-03-26 12:02:41 -05:00
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class);
|
2017-03-25 03:22:00 -05:00
|
|
|
/** @var MethodProphecy $process */
|
2018-03-26 12:02:41 -05:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware->process($request, $delegate->reveal());
|
|
|
|
|
|
|
|
$process->shouldHaveBeenCalledTimes(1);
|
2016-08-21 06:52:15 -05:00
|
|
|
}
|
|
|
|
}
|