|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Sunrise\Http\Router\Tests\Middleware; |
| 4 | + |
| 5 | +/** |
| 6 | + * Import classes |
| 7 | + */ |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Psr\Http\Server\MiddlewareInterface; |
| 10 | +use Sunrise\Http\Router\Exception\UndecodablePayloadException; |
| 11 | +use Sunrise\Http\Router\Middleware\JsonPayloadDecodingMiddleware; |
| 12 | +use Sunrise\Http\Router\Tests\Fixtures; |
| 13 | +use Sunrise\Http\ServerRequest\ServerRequestFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * JsonPayloadDecodingMiddlewareTest |
| 17 | + */ |
| 18 | +class JsonPayloadDecodingMiddlewareTest extends TestCase |
| 19 | +{ |
| 20 | + |
| 21 | + /** |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function testContract() : void |
| 25 | + { |
| 26 | + $this->assertInstanceOf(MiddlewareInterface::class, new JsonPayloadDecodingMiddleware()); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @param string $mediaType |
| 31 | + * |
| 32 | + * @return void |
| 33 | + * |
| 34 | + * @dataProvider supportedMediaTypeProvider |
| 35 | + */ |
| 36 | + public function testProcessWithSupportedMediaType(string $mediaType) : void |
| 37 | + { |
| 38 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/') |
| 39 | + ->withHeader('Content-Type', $mediaType); |
| 40 | + |
| 41 | + $request->getBody()->write('{"foo":"bar"}'); |
| 42 | + |
| 43 | + $handler = new Fixtures\Controllers\BlankController(); |
| 44 | + |
| 45 | + (new JsonPayloadDecodingMiddleware)->process($request, $handler); |
| 46 | + |
| 47 | + $this->assertSame(['foo' => 'bar'], $handler->getRequest()->getParsedBody()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param string $mediaType |
| 52 | + * |
| 53 | + * @return void |
| 54 | + * |
| 55 | + * @dataProvider unsupportedMediaTypeProvider |
| 56 | + */ |
| 57 | + public function testProcessWithUnsupportedMediaType(string $mediaType) : void |
| 58 | + { |
| 59 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/') |
| 60 | + ->withHeader('Content-Type', $mediaType); |
| 61 | + |
| 62 | + $request->getBody()->write('{"foo":"bar"}'); |
| 63 | + |
| 64 | + $handler = new Fixtures\Controllers\BlankController(); |
| 65 | + |
| 66 | + (new JsonPayloadDecodingMiddleware)->process($request, $handler); |
| 67 | + |
| 68 | + $this->assertNull($handler->getRequest()->getParsedBody()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return void |
| 73 | + */ |
| 74 | + public function testProcessWithoutMediaType() : void |
| 75 | + { |
| 76 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/'); |
| 77 | + $request->getBody()->write('{"foo":"bar"}'); |
| 78 | + |
| 79 | + $handler = new Fixtures\Controllers\BlankController(); |
| 80 | + |
| 81 | + (new JsonPayloadDecodingMiddleware)->process($request, $handler); |
| 82 | + |
| 83 | + $this->assertNull($handler->getRequest()->getParsedBody()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + public function testProcessWithInvalidPayload() : void |
| 90 | + { |
| 91 | + $request = (new ServerRequestFactory)->createServerRequest('GET', '/') |
| 92 | + ->withHeader('Content-Type', 'application/json'); |
| 93 | + |
| 94 | + $request->getBody()->write('!'); |
| 95 | + |
| 96 | + $handler = new Fixtures\Controllers\BlankController(); |
| 97 | + |
| 98 | + $this->expectException(UndecodablePayloadException::class); |
| 99 | + $this->expectExceptionMessage('Invalid Payload: Syntax error'); |
| 100 | + |
| 101 | + (new JsonPayloadDecodingMiddleware)->process($request, $handler); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return list<array{0: string}> |
| 106 | + */ |
| 107 | + public function supportedMediaTypeProvider() : array |
| 108 | + { |
| 109 | + return [ |
| 110 | + ['application/json'], |
| 111 | + ['application/json; foo=bar'], |
| 112 | + ['application/json ; foo=bar'], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return list<array{0: string}> |
| 118 | + */ |
| 119 | + public function unsupportedMediaTypeProvider() : array |
| 120 | + { |
| 121 | + return [ |
| 122 | + ['application/jsonx'], |
| 123 | + ['application/json+x'], |
| 124 | + ['application/jsonx; foo=bar'], |
| 125 | + ['application/json+x; foo=bar'], |
| 126 | + ['application/jsonx ; foo=bar'], |
| 127 | + ['application/json+x ; foo=bar'], |
| 128 | + ]; |
| 129 | + } |
| 130 | +} |
0 commit comments