|
1
|
|
|
<?php |
|
2
|
|
|
namespace ShlinkioTest\Shlink\Rest\Middleware; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
6
|
|
|
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware; |
|
7
|
|
|
use Zend\Diactoros\Response; |
|
8
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
9
|
|
|
use Zend\Diactoros\Stream; |
|
10
|
|
|
|
|
11
|
|
|
class BodyParserMiddlewareTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var BodyParserMiddleware |
|
15
|
|
|
*/ |
|
16
|
|
|
private $middleware; |
|
17
|
|
|
|
|
18
|
|
|
public function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->middleware = new BodyParserMiddleware(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @test |
|
25
|
|
|
*/ |
|
26
|
|
|
public function requestsFromOtherMethodsJustFallbackToNextMiddleware() |
|
27
|
|
|
{ |
|
28
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('GET'); |
|
29
|
|
|
$test = $this; |
|
30
|
|
|
$this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) { |
|
|
|
|
|
|
31
|
|
|
$test->assertSame($request, $req); |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
$request = $request->withMethod('POST'); |
|
35
|
|
|
$test = $this; |
|
36
|
|
|
$this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) { |
|
|
|
|
|
|
37
|
|
|
$test->assertSame($request, $req); |
|
38
|
|
|
}); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @test |
|
43
|
|
|
*/ |
|
44
|
|
View Code Duplication |
public function jsonRequestsAreJsonDecoded() |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$body = new Stream('php://temp', 'wr'); |
|
47
|
|
|
$body->write('{"foo": "bar", "bar": ["one", 5]}'); |
|
48
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT') |
|
49
|
|
|
->withBody($body) |
|
50
|
|
|
->withHeader('content-type', 'application/json'); |
|
51
|
|
|
$test = $this; |
|
52
|
|
|
$this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) { |
|
|
|
|
|
|
53
|
|
|
$test->assertNotSame($request, $req); |
|
54
|
|
|
$test->assertEquals([ |
|
55
|
|
|
'foo' => 'bar', |
|
56
|
|
|
'bar' => ['one', 5], |
|
57
|
|
|
], $req->getParsedBody()); |
|
58
|
|
|
}); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
public function regularRequestsAreUrlDecoded() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$body = new Stream('php://temp', 'wr'); |
|
67
|
|
|
$body->write('foo=bar&bar[]=one&bar[]=5'); |
|
68
|
|
|
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT') |
|
69
|
|
|
->withBody($body); |
|
70
|
|
|
$test = $this; |
|
71
|
|
|
$this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) { |
|
|
|
|
|
|
72
|
|
|
$test->assertNotSame($request, $req); |
|
73
|
|
|
$test->assertEquals([ |
|
74
|
|
|
'foo' => 'bar', |
|
75
|
|
|
'bar' => ['one', 5], |
|
76
|
|
|
], $req->getParsedBody()); |
|
77
|
|
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.