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;
|
|
|
|
|
2020-01-01 23:11:53 +03:00
|
|
|
use Laminas\Diactoros\Response;
|
|
|
|
use Laminas\Diactoros\ServerRequest;
|
|
|
|
use Laminas\Diactoros\Stream;
|
2023-01-02 15:33:24 +03:00
|
|
|
use PHPUnit\Framework\Assert;
|
2023-02-09 22:42:18 +03:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-23 23:45:23 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-25 11:22:00 +03:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2018-03-26 20:02:41 +03:00
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2023-01-02 15:33:24 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\MalformedBodyException;
|
2016-08-21 14:52:15 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2016-08-21 14:52:15 +03:00
|
|
|
class BodyParserMiddlewareTest extends TestCase
|
|
|
|
{
|
2019-12-30 00:48:40 +03:00
|
|
|
private BodyParserMiddleware $middleware;
|
2016-08-21 14:52:15 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
|
|
|
$this->middleware = new BodyParserMiddleware();
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test, DataProvider('provideIgnoredRequestMethods')]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void
|
2016-08-21 14:52:15 +03:00
|
|
|
{
|
2022-10-23 23:45:23 +03:00
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
$request->method('getMethod')->willReturn($method);
|
|
|
|
$request->method('getParsedBody')->willReturn([]);
|
2019-12-01 12:58:48 +03:00
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request);
|
2019-02-16 23:24:32 +03:00
|
|
|
}
|
2016-08-21 14:52:15 +03:00
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideIgnoredRequestMethods(): iterable
|
2019-02-16 23:24:32 +03:00
|
|
|
{
|
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
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
|
|
|
|
{
|
2022-10-23 23:45:23 +03:00
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
$request->method('getMethod')->willReturn('POST');
|
|
|
|
$request->method('getParsedBody')->willReturn(['foo' => 'bar']);
|
2019-12-01 12:58:48 +03:00
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request);
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
private function assertHandlingRequestJustFallsBackToNext(MockObject & ServerRequestInterface $request): void
|
2019-02-16 23:24:32 +03:00
|
|
|
{
|
2022-10-23 23:45:23 +03:00
|
|
|
$request->expects($this->never())->method('getHeaderLine');
|
2019-02-16 23:24:32 +03:00
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
$nextHandler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$nextHandler->expects($this->once())->method('handle')->with($request)->willReturn(new Response());
|
2019-02-16 23:24:32 +03:00
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
$this->middleware->process($request, $nextHandler);
|
2019-02-16 23:24:32 +03:00
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2019-02-16 23:24:32 +03:00
|
|
|
public function jsonRequestsAreJsonDecoded(): void
|
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);
|
2022-10-23 23:45:23 +03:00
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$handler->expects($this->once())->method('handle')->with(
|
|
|
|
$this->isInstanceOf(ServerRequestInterface::class),
|
2023-01-02 15:33:24 +03:00
|
|
|
)->willReturnCallback(function (ServerRequestInterface $req) {
|
|
|
|
Assert::assertEquals([
|
|
|
|
'foo' => 'bar',
|
|
|
|
'bar' => ['one', 5],
|
|
|
|
], $req->getParsedBody());
|
|
|
|
|
|
|
|
return new Response();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->middleware->process($request, $handler);
|
|
|
|
}
|
|
|
|
|
2023-02-09 22:42:18 +03:00
|
|
|
#[Test]
|
2023-01-02 15:33:24 +03:00
|
|
|
public function invalidBodyResultsInException(): void
|
|
|
|
{
|
|
|
|
$body = new Stream('php://temp', 'wr');
|
|
|
|
$body->write('{"foo": "bar", "bar": ["one');
|
|
|
|
$request = (new ServerRequest())->withMethod('PUT')
|
|
|
|
->withBody($body);
|
|
|
|
|
|
|
|
$handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$handler->expects($this->never())->method('handle');
|
|
|
|
|
|
|
|
$this->expectException(MalformedBodyException::class);
|
|
|
|
$this->expectExceptionMessage('Provided request does not contain a valid JSON body.');
|
2017-03-25 11:22:00 +03:00
|
|
|
|
2022-10-23 23:45:23 +03:00
|
|
|
$this->middleware->process($request, $handler);
|
2016-08-21 14:52:15 +03:00
|
|
|
}
|
|
|
|
}
|