Merge pull request #40 from creative-commoners/pulls/1.0/fix-for-empty-post-body

FIX Return string directly when no body content is provided to put/post methods
This commit is contained in:
Dylan Wagstaff 2017-11-17 14:10:39 +13:00 committed by GitHub
commit 4bdd071354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 5 deletions

View File

@ -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)
{

View File

@ -193,6 +193,19 @@ class RestfulServerTest extends SapphireTest
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');