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