|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Argument; |
|
8
|
|
|
use Prophecy\Prophecy\MethodProphecy; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware; |
|
12
|
|
|
use Zend\Diactoros\Response; |
|
13
|
|
|
use Zend\Diactoros\ServerRequest; |
|
14
|
|
|
use Zend\Diactoros\Stream; |
|
15
|
|
|
use function array_shift; |
|
16
|
|
|
|
|
17
|
|
|
class BodyParserMiddlewareTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var BodyParserMiddleware */ |
|
20
|
|
|
private $middleware; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->middleware = new BodyParserMiddleware(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @test |
|
29
|
|
|
* @dataProvider provideIgnoredRequestMethods |
|
30
|
|
|
*/ |
|
31
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware(string $method): void |
|
32
|
|
|
{ |
|
33
|
|
|
$request = (new ServerRequest())->withMethod($method); |
|
34
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function provideIgnoredRequestMethods(): iterable |
|
38
|
|
|
{ |
|
39
|
|
|
yield 'with GET' => ['GET']; |
|
40
|
|
|
yield 'with HEAD' => ['HEAD']; |
|
41
|
|
|
yield 'with OPTIONS' => ['OPTIONS']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @test */ |
|
45
|
|
|
public function requestsWithNonEmptyBodyJustFallbackToNextMiddleware(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$request = (new ServerRequest())->withParsedBody(['foo' => 'bar'])->withMethod('POST'); |
|
48
|
|
|
$this->assertHandlingRequestJustFallsBackToNext($request); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function assertHandlingRequestJustFallsBackToNext(ServerRequestInterface $request): void |
|
52
|
|
|
{ |
|
53
|
|
|
$nextHandler = $this->prophesize(RequestHandlerInterface::class); |
|
54
|
|
|
$handle = $nextHandler->handle($request)->willReturn(new Response()); |
|
55
|
|
|
|
|
56
|
|
|
$this->middleware->process($request, $nextHandler->reveal()); |
|
57
|
|
|
|
|
58
|
|
|
$handle->shouldHaveBeenCalledOnce(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** @test */ |
|
62
|
|
|
public function jsonRequestsAreJsonDecoded(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$test = $this; |
|
65
|
|
|
$body = new Stream('php://temp', 'wr'); |
|
66
|
|
|
$body->write('{"foo": "bar", "bar": ["one", 5]}'); |
|
67
|
|
|
$request = (new ServerRequest())->withMethod('PUT') |
|
68
|
|
|
->withBody($body) |
|
69
|
|
|
->withHeader('content-type', 'application/json'); |
|
70
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class); |
|
71
|
|
|
/** @var MethodProphecy $process */ |
|
72
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will( |
|
73
|
|
|
function (array $args) use ($test) { |
|
74
|
|
|
/** @var ServerRequestInterface $req */ |
|
75
|
|
|
$req = array_shift($args); |
|
76
|
|
|
|
|
77
|
|
|
$test->assertEquals([ |
|
78
|
|
|
'foo' => 'bar', |
|
79
|
|
|
'bar' => ['one', 5], |
|
80
|
|
|
], $req->getParsedBody()); |
|
81
|
|
|
|
|
82
|
|
|
return new Response(); |
|
83
|
|
|
} |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$this->middleware->process($request, $delegate->reveal()); |
|
87
|
|
|
|
|
88
|
|
|
$process->shouldHaveBeenCalledOnce(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** @test */ |
|
92
|
|
|
public function regularRequestsAreUrlDecoded(): void |
|
93
|
|
|
{ |
|
94
|
|
|
$test = $this; |
|
95
|
|
|
$body = new Stream('php://temp', 'wr'); |
|
96
|
|
|
$body->write('foo=bar&bar[]=one&bar[]=5'); |
|
97
|
|
|
$request = (new ServerRequest())->withMethod('PUT') |
|
98
|
|
|
->withBody($body); |
|
99
|
|
|
$delegate = $this->prophesize(RequestHandlerInterface::class); |
|
100
|
|
|
/** @var MethodProphecy $process */ |
|
101
|
|
|
$process = $delegate->handle(Argument::type(ServerRequestInterface::class))->will( |
|
102
|
|
|
function (array $args) use ($test) { |
|
103
|
|
|
/** @var ServerRequestInterface $req */ |
|
104
|
|
|
$req = array_shift($args); |
|
105
|
|
|
|
|
106
|
|
|
$test->assertEquals([ |
|
107
|
|
|
'foo' => 'bar', |
|
108
|
|
|
'bar' => ['one', 5], |
|
109
|
|
|
], $req->getParsedBody()); |
|
110
|
|
|
|
|
111
|
|
|
return new Response(); |
|
112
|
|
|
} |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$this->middleware->process($request, $delegate->reveal()); |
|
116
|
|
|
|
|
117
|
|
|
$process->shouldHaveBeenCalledOnce(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|