From 11c9429fabd8b646356fc30c65e7bb7a4e7f8a17 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Sun, 26 Nov 2017 19:57:32 +0900 Subject: [PATCH] Support non pre-parsed PSR-7 request body Because PSR-7 specification only specify that `getParsedBody()` **may** return the parsed body for `application/json`, we cannot assume that it is always the case. So if the value returned parsed body is an empty array, it means we should try to parse it ourselves (`null` would mean no body at all according to spec). With this modification we try to used given parsed body, but fallback on trying to parse the body if necessary. This leave the door open to custom implementation of parsing if needed, while making it easier to use out of the box. --- src/Server/Helper.php | 4 ++++ tests/Server/StandardServerTest.php | 17 +++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Server/Helper.php b/src/Server/Helper.php index 1957fec..d855eff 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -499,6 +499,10 @@ class Helper Utils::printSafeJson($bodyParams) ); } + + if (empty($bodyParams)) { + $bodyParams = json_decode($request->getBody(), true); + } } else { $bodyParams = $request->getParsedBody(); diff --git a/tests/Server/StandardServerTest.php b/tests/Server/StandardServerTest.php index 6fa2ceb..2b1055e 100644 --- a/tests/Server/StandardServerTest.php +++ b/tests/Server/StandardServerTest.php @@ -47,15 +47,17 @@ class StandardServerTest extends TestCase 'query' => '{f1}' ]); - $request = $this->preparePsrRequest('application/json', $body); - $expected = [ 'data' => [ 'f1' => 'f1' ] ]; - $this->assertPsrRequestEquals($expected, $request); + $preParsedRequest = $this->preparePsrRequest('application/json', $body, true); + $this->assertPsrRequestEquals($expected, $preParsedRequest); + + $notPreParsedRequest = $this->preparePsrRequest('application/json', $body, false); + $this->assertPsrRequestEquals($expected, $notPreParsedRequest); } private function executePsrRequest($psrRequest) @@ -73,21 +75,20 @@ class StandardServerTest extends TestCase return $result; } - private function preparePsrRequest($contentType, $content, $method = 'POST') + private function preparePsrRequest($contentType, $content, $preParseBody) { $psrRequestBody = new PsrStreamStub(); $psrRequestBody->content = $content; $psrRequest = new PsrRequestStub(); $psrRequest->headers['content-type'] = [$contentType]; - $psrRequest->method = $method; + $psrRequest->method = 'POST'; $psrRequest->body = $psrRequestBody; - if ($contentType === 'application/json') { + if ($preParseBody && $contentType === 'application/json') { $parsedBody = json_decode($content, true); - $parsedBody = $parsedBody === false ? null : $parsedBody; } else { - $parsedBody = null; + $parsedBody = []; } $psrRequest->parsedBody = $parsedBody;