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

106 lines
3.5 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;
2020-01-01 14:11:53 -06:00
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Stream;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 14:34:18 -05:00
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
2018-03-26 12:02:41 -05:00
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Core\Exception\MalformedBodyException;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
class BodyParserMiddlewareTest extends TestCase
{
private BodyParserMiddleware $middleware;
protected 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
{
$request = $this->createMock(ServerRequestInterface::class);
$request->method('getMethod')->willReturn($method);
$request->method('getParsedBody')->willReturn([]);
$this->assertHandlingRequestJustFallsBackToNext($request);
2019-02-16 14:24:32 -06: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
}
2019-02-16 14:24:32 -06:00
/** @test */
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void
{
$request = $this->createMock(ServerRequestInterface::class);
$request->method('getMethod')->willReturn('POST');
$request->method('getParsedBody')->willReturn(['foo' => 'bar']);
$this->assertHandlingRequestJustFallsBackToNext($request);
}
private function assertHandlingRequestJustFallsBackToNext(MockObject & ServerRequestInterface $request): void
2019-02-16 14:24:32 -06:00
{
$request->expects($this->never())->method('getHeaderLine');
2019-02-16 14:24:32 -06: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
$this->middleware->process($request, $nextHandler);
2019-02-16 14:24:32 -06:00
}
/** @test */
public function jsonRequestsAreJsonDecoded(): void
{
$body = new Stream('php://temp', 'wr');
$body->write('{"foo": "bar", "bar": ["one", 5]}');
$request = (new ServerRequest())->withMethod('PUT')
->withBody($body);
$handler = $this->createMock(RequestHandlerInterface::class);
$handler->expects($this->once())->method('handle')->with(
$this->isInstanceOf(ServerRequestInterface::class),
)->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.');
$this->middleware->process($request, $handler);
}
}