From 3c94b057f0792ff87982823ad159b33ee86d57da Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 19 Sep 2017 18:04:17 +0200 Subject: [PATCH] Removed the ability to add custom parsers for custom body types --- .../RequestBodyParserMiddleware.php | 39 ++++---------- .../RequestBodyParserMiddlewareTest.php | 54 +++++++++---------- 2 files changed, 37 insertions(+), 56 deletions(-) diff --git a/src/Middleware/RequestBodyParserMiddleware.php b/src/Middleware/RequestBodyParserMiddleware.php index d5065118..80b520ca 100644 --- a/src/Middleware/RequestBodyParserMiddleware.php +++ b/src/Middleware/RequestBodyParserMiddleware.php @@ -6,41 +6,22 @@ final class RequestBodyParserMiddleware { - private $types = array(); - - public function __construct() - { - $this->addType('application/x-www-form-urlencoded', function (ServerRequestInterface $request) { - $ret = array(); - parse_str((string)$request->getBody(), $ret); - - return $request->withParsedBody($ret); - }); - } - - public function addType($type, $callback) - { - $this->types[$type] = $callback; - } - public function __invoke(ServerRequestInterface $request, $next) { $type = $request->getHeaderLine('Content-Type'); - if (!isset($this->types[$type])) { - return $next($request); - } - - try { - $parser = $this->types[$type]; - /** @var ServerRequestInterface $request */ - $request = $parser($request); - } catch (\Exception $e) { - return $next($request); - } catch (\Throwable $t) { - return $next($request); + if ($type === 'application/x-www-form-urlencoded') { + return $next($this->parseFormUrlencoded($request)); } return $next($request); } + + private function parseFormUrlencoded(ServerRequestInterface $request) + { + $ret = array(); + parse_str((string)$request->getBody(), $ret); + + return $request->withParsedBody($ret); + } } diff --git a/tests/Middleware/RequestBodyParserMiddlewareTest.php b/tests/Middleware/RequestBodyParserMiddlewareTest.php index 0ec0a4a4..a18d49dc 100644 --- a/tests/Middleware/RequestBodyParserMiddlewareTest.php +++ b/tests/Middleware/RequestBodyParserMiddlewareTest.php @@ -13,33 +13,6 @@ final class RequestBodyParserMiddlewareTest extends TestCase { - public function testParse() - { - $middleware = new RequestBodyParserMiddleware(); - $middleware->addType('react/http', function (ServerRequestInterface $request) { - return $request->withParsedBody('parsed'); - }); - - $request = new ServerRequest( - 200, - 'https://example.com/', - array( - 'Content-Type' => 'react/http', - ), - 'not yet parsed' - ); - - /** @var ServerRequestInterface $parsedRequest */ - $parsedRequest = $middleware( - $request, - function (ServerRequestInterface $request) { - return $request; - } - ); - - $this->assertSame('parsed', $parsedRequest->getParsedBody()); - $this->assertSame('not yet parsed', (string)$parsedRequest->getBody()); - } public function testFormUrlencodedParsing() { $middleware = new RequestBodyParserMiddleware(); @@ -83,4 +56,31 @@ function (ServerRequestInterface $request) { ); $this->assertSame('foo=bar&baz[]=cheese&bar[]=beer&bar[]=wine&market[fish]=salmon&market[meat][]=beef&market[meat][]=chicken&market[]=bazaar', (string)$parsedRequest->getBody()); } + + public function testDoesNotParseJsonByDefault() + { + $middleware = new RequestBodyParserMiddleware(); + $request = new ServerRequest( + 'POST', + 'https://example.com/', + array( + 'Content-Type' => 'application/json', + ), + '{"hello":"world"}' + ); + + /** @var ServerRequestInterface $parsedRequest */ + $parsedRequest = $middleware( + $request, + function (ServerRequestInterface $request) { + return $request; + } + ); + + $this->assertSame( + null, + $parsedRequest->getParsedBody() + ); + $this->assertSame('{"hello":"world"}', (string)$parsedRequest->getBody()); + } }