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

103 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
2020-01-01 23:11:53 +03:00
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Stream;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
2020-11-02 13:50:19 +03:00
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ProphecyInterface;
use Psr\Http\Message\ServerRequestInterface;
2018-03-26 20:02:41 +03:00
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use function array_shift;
class BodyParserMiddlewareTest extends TestCase
{
2020-11-02 13:50:19 +03:00
use ProphecyTrait;
private BodyParserMiddleware $middleware;
protected function setUp(): void
{
$this->middleware = new BodyParserMiddleware();
}
/**
* @test
2019-02-16 23:24:32 +03:00
* @dataProvider provideIgnoredRequestMethods
*/
2019-02-16 23:24:32 +03:00
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
{
$request = $this->prophesize(ServerRequestInterface::class);
$request->getMethod()->willReturn($method);
$request->getParsedBody()->willReturn([]);
2020-10-04 01:35:14 +03:00
self::assertHandlingRequestJustFallsBackToNext($request);
2019-02-16 23:24:32 +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
}
2019-02-16 23:24:32 +03:00
/** @test */
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
{
$request = $this->prophesize(ServerRequestInterface::class);
$request->getMethod()->willReturn('POST');
$request->getParsedBody()->willReturn(['foo' => 'bar']);
2020-10-04 01:35:14 +03:00
self::assertHandlingRequestJustFallsBackToNext($request);
}
private function assertHandlingRequestJustFallsBackToNext(ProphecyInterface $requestMock): void
2019-02-16 23:24:32 +03:00
{
$getContentType = $requestMock->getHeaderLine('Content-type')->willReturn('');
$request = $requestMock->reveal();
2019-02-16 23:24:32 +03:00
$nextHandler = $this->prophesize(RequestHandlerInterface::class);
$handle = $nextHandler->handle($request)->willReturn(new Response());
$this->middleware->process($request, $nextHandler->reveal());
$handle->shouldHaveBeenCalledOnce();
$getContentType->shouldNotHaveBeenCalled();
2019-02-16 23:24:32 +03:00
}
/** @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);
2018-03-26 20:02:41 +03: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();
2020-01-01 22:48:31 +03:00
},
);
$this->middleware->process($request, $delegate->reveal());
2018-11-11 15:18:21 +03:00
$process->shouldHaveBeenCalledOnce();
}
}