diff --git a/code/RestfulServer.php b/code/RestfulServer.php index 8b7644e..83ad871 100644 --- a/code/RestfulServer.php +++ b/code/RestfulServer.php @@ -405,7 +405,11 @@ class RestfulServer extends Controller return $this->unsupportedMediaType(); } + /** @var DataObject|string */ $obj = $this->updateDataObject($obj, $reqFormatter); + if (is_string($obj)) { + return $obj; + } $this->getResponse()->setStatusCode(200); // Success $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); @@ -470,7 +474,11 @@ class RestfulServer extends Controller $responseFormatter = $this->getResponseDataFormatter($className); + /** @var DataObject|string $obj */ $obj = $this->updateDataObject($obj, $reqFormatter); + if (is_string($obj)) { + return $obj; + } $this->getResponse()->setStatusCode(201); // Created $this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType()); @@ -498,7 +506,7 @@ class RestfulServer extends Controller * * @param DataObject $obj * @param DataFormatter $formatter - * @return DataObject The passed object + * @return DataObject|string The passed object, or "No Content" if incomplete input data is provided */ protected function updateDataObject($obj, $formatter) { diff --git a/tests/unit/RestfulServerTest.php b/tests/unit/RestfulServerTest.php index 54a9d77..b34a166 100644 --- a/tests/unit/RestfulServerTest.php +++ b/tests/unit/RestfulServerTest.php @@ -188,18 +188,31 @@ class RestfulServerTest extends SapphireTest $response->getHeader('Location'), Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID']) ); - + unset($_SERVER['PHP_AUTH_USER']); unset($_SERVER['PHP_AUTH_PW']); } - + + public function testPostWithoutBodyReturnsNoContent() + { + $_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; + $_SERVER['PHP_AUTH_PW'] = 'editor'; + + $url = '/api/v1/RestfulServerTest_Comment'; + $response = Director::test($url, null, null, 'POST'); + + $this->assertEquals('No Content', $response->getBody()); + + unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); + } + public function testPUTwithJSON() { $comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1'); - + $_SERVER['PHP_AUTH_USER'] = 'editor@test.com'; $_SERVER['PHP_AUTH_PW'] = 'editor'; - + // by mimetype $url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID; $body = '{"Comment":"updated"}';