mirror of
https://github.com/silverstripe/silverstripe-restfulserver
synced 2024-10-22 14:05:58 +02:00
FIX Return string directly when no body content is provided to put/post methods
This commit is contained in:
parent
4ba5bf5853
commit
b3fc6803fd
@ -405,7 +405,11 @@ class RestfulServer extends Controller
|
|||||||
return $this->unsupportedMediaType();
|
return $this->unsupportedMediaType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @var DataObject|string */
|
||||||
$obj = $this->updateDataObject($obj, $reqFormatter);
|
$obj = $this->updateDataObject($obj, $reqFormatter);
|
||||||
|
if (is_string($obj)) {
|
||||||
|
return $obj;
|
||||||
|
}
|
||||||
|
|
||||||
$this->getResponse()->setStatusCode(200); // Success
|
$this->getResponse()->setStatusCode(200); // Success
|
||||||
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
||||||
@ -470,7 +474,11 @@ class RestfulServer extends Controller
|
|||||||
|
|
||||||
$responseFormatter = $this->getResponseDataFormatter($className);
|
$responseFormatter = $this->getResponseDataFormatter($className);
|
||||||
|
|
||||||
|
/** @var DataObject|string $obj */
|
||||||
$obj = $this->updateDataObject($obj, $reqFormatter);
|
$obj = $this->updateDataObject($obj, $reqFormatter);
|
||||||
|
if (is_string($obj)) {
|
||||||
|
return $obj;
|
||||||
|
}
|
||||||
|
|
||||||
$this->getResponse()->setStatusCode(201); // Created
|
$this->getResponse()->setStatusCode(201); // Created
|
||||||
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
$this->getResponse()->addHeader('Content-Type', $responseFormatter->getOutputContentType());
|
||||||
@ -498,7 +506,7 @@ class RestfulServer extends Controller
|
|||||||
*
|
*
|
||||||
* @param DataObject $obj
|
* @param DataObject $obj
|
||||||
* @param DataFormatter $formatter
|
* @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)
|
protected function updateDataObject($obj, $formatter)
|
||||||
{
|
{
|
||||||
|
@ -188,18 +188,31 @@ class RestfulServerTest extends SapphireTest
|
|||||||
$response->getHeader('Location'),
|
$response->getHeader('Location'),
|
||||||
Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID'])
|
Controller::join_links(Director::absoluteBaseURL(), $url, $responseArr['ID'])
|
||||||
);
|
);
|
||||||
|
|
||||||
unset($_SERVER['PHP_AUTH_USER']);
|
unset($_SERVER['PHP_AUTH_USER']);
|
||||||
unset($_SERVER['PHP_AUTH_PW']);
|
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()
|
public function testPUTwithJSON()
|
||||||
{
|
{
|
||||||
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
$comment1 = $this->objFromFixture('RestfulServerTest_Comment', 'comment1');
|
||||||
|
|
||||||
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
|
||||||
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
$_SERVER['PHP_AUTH_PW'] = 'editor';
|
||||||
|
|
||||||
// by mimetype
|
// by mimetype
|
||||||
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
$url = "/api/v1/RestfulServerTest_Comment/" . $comment1->ID;
|
||||||
$body = '{"Comment":"updated"}';
|
$body = '{"Comment":"updated"}';
|
||||||
|
Loading…
Reference in New Issue
Block a user