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

119 lines
3.7 KiB
PHP
Raw Normal View History

<?php
2019-10-05 10:26:10 -05:00
2017-10-12 03:13:20 -05:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface;
2018-03-26 12:02:41 -05:00
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Stream;
use function array_shift;
class BodyParserMiddlewareTest extends TestCase
{
/** @var BodyParserMiddleware */
private $middleware;
2019-02-16 03:53:45 -06:00
public function setUp(): void
{
$this->middleware = new BodyParserMiddleware();
}
/**
* @test
2019-02-16 14:24:32 -06:00
* @dataProvider provideIgnoredRequestMethods
*/
2019-02-16 14:24:32 -06:00
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
{
2019-02-16 14:24:32 -06:00
$request = (new ServerRequest())->withMethod($method);
$this->assertHandlingRequestJustFallsBackToNext($request);
}
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
}
2019-02-16 14:24:32 -06:00
/** @test */
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
{
$request = (new ServerRequest())->withParsedBody(['foo' => 'bar'])->withMethod('POST');
$this->assertHandlingRequestJustFallsBackToNext($request);
}
2019-02-16 14:24:32 -06: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
{
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('{"foo": "bar", "bar": ["one", 5]}');
$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(
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 06:18:21 -06:00
$process->shouldHaveBeenCalledOnce();
}
2019-02-16 14:24:32 -06:00
/** @test */
public function regularRequestsAreUrlDecoded(): void
{
$test = $this;
$body = new Stream('php://temp', 'wr');
$body->write('foo=bar&bar[]=one&bar[]=5');
$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(
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 06:18:21 -06:00
$process->shouldHaveBeenCalledOnce();
}
}