From c28689aa6ab8dcfe70a3af0c7046585cf6d03353 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 12 Nov 2015 10:47:21 +0200 Subject: [PATCH 1/2] Added test with should confirm that body can be string --- test/Controller/RestfulControllerTest.php | 17 +++++++++++ .../RestfulContentTypeTestController.php | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 test/Controller/TestAsset/RestfulContentTypeTestController.php diff --git a/test/Controller/RestfulControllerTest.php b/test/Controller/RestfulControllerTest.php index bff64325f..ec3faeb98 100644 --- a/test/Controller/RestfulControllerTest.php +++ b/test/Controller/RestfulControllerTest.php @@ -16,6 +16,7 @@ use Zend\Http\Response; use Zend\Mvc\MvcEvent; use Zend\Mvc\Router\RouteMatch; +use ZendTest\Mvc\Controller\TestAsset\RestfulContentTypeTestController; class RestfulControllerTest extends TestCase { @@ -76,6 +77,22 @@ public function testDispatchInvokesCreateMethodWhenNoActionPresentAndPostInvoked $this->assertEquals('create', $this->routeMatch->getParam('action')); } + public function testCanReceiveStringAsRequestContent() + { + $string = "any content"; + $this->request->setMethod('PUT'); + $this->request->setContent($string); + $this->routeMatch->setParam('id', $id = 1); + + $controller = new RestfulContentTypeTestController(); + $controller->setEvent($this->event); + $result = $controller->dispatch($this->request, $this->response); + + $this->assertEquals($id, $result['id']); + $this->assertEquals($string, $result['data']); + $this->assertEquals('update', $this->routeMatch->getParam('action')); + } + public function testDispatchInvokesUpdateMethodWhenNoActionPresentAndPutInvokedWithIdentifier() { $entity = ['name' => __FUNCTION__]; diff --git a/test/Controller/TestAsset/RestfulContentTypeTestController.php b/test/Controller/TestAsset/RestfulContentTypeTestController.php new file mode 100644 index 000000000..94890b823 --- /dev/null +++ b/test/Controller/TestAsset/RestfulContentTypeTestController.php @@ -0,0 +1,30 @@ + $id, + 'data' => $data + ]; + } +} From cdea8bde8a820870da3b782cfeeddf6e0be95b92 Mon Sep 17 00:00:00 2001 From: Vytautas Stankus Date: Thu, 12 Nov 2015 10:57:50 +0200 Subject: [PATCH 2/2] Allow non-urlencoded content as request body --- src/Controller/AbstractRestfulController.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Controller/AbstractRestfulController.php b/src/Controller/AbstractRestfulController.php index a090c1b82..85c89781c 100644 --- a/src/Controller/AbstractRestfulController.php +++ b/src/Controller/AbstractRestfulController.php @@ -560,8 +560,8 @@ protected function getIdentifier($routeMatch, $request) * * If the content-type indicates a JSON payload, the payload is immediately * decoded and the data returned. Otherwise, the data is passed to - * parse_str(). If that function returns a single-member array with a key - * of "0", the method assumes that we have non-urlencoded content and + * parse_str(). If that function returns a single-member array with a empty + * value, the method assumes that we have non-urlencoded content and * returns the raw content; otherwise, the array created is returned. * * @param mixed $request @@ -578,10 +578,9 @@ protected function processBodyContent($request) parse_str($content, $parsedParams); - // If parse_str fails to decode, or we have a single element with key - // 0, return the raw content. - if (!is_array($parsedParams) - || (1 == count($parsedParams) && isset($parsedParams[0])) + // If parse_str fails to decode, or we have a single element with empty value + if (!is_array($parsedParams) || empty($parsedParams) + || (1 == count($parsedParams) && '' === reset($parsedParams)) ) { return $content; }