ENHANCEMENT RestfulServer now accepts form-encoded HTTP requests for PUT and POST with new FormEncodedDataFormatter class (#3792, thanks camspiers)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@73747 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-03-29 00:51:38 +00:00
parent 6a53df681f
commit 40910f9f71
2 changed files with 59 additions and 8 deletions

View File

@ -0,0 +1,43 @@
<?php
/**
* Accepts form encoded strings and converts them
* to a valid PHP array via {@link parse_str()}.
* Use together with {@link RESTfulServer} to submit
* data via POST or PUT.
*
* Example when using cURL on commandline:
* <code>
* curl -d "Name=This is a new record" http://host/api/v1/(DataObject)
* curl -X PUT -d "Name=This is an updated record" http://host/api/v1/(DataObject)/1
* </code>
*
* @todo Format response form encoded as well - currently uses XMLDataFormatter
*
* @author Cam Spiers <camspiers at gmail dot com>
*
* @package sapphire
* @subpackage formatters
*/
class FormEncodedDataFormatter extends XMLDataFormatter {
public function supportedExtensions() {
return array(
);
}
public function supportedMimeTypes() {
return array(
'application/x-www-form-urlencoded'
);
}
public function convertStringToArray($strData) {
$postArray = array();
parse_str($strData, $postArray);
return $postArray;
//TODO: It would be nice to implement this function in Convert.php
//return Convert::querystr2array($strData);
}
}
?>

View File

@ -94,16 +94,20 @@ class RestfulServerTest extends SapphireTest {
public function testPUTWithFormEncoded() {
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
$_SERVER['PHP_AUTH_PW'] = 'editor';
$url = "/api/v1/RestfulServerTest_Comment/1";
$data = array('Comment' => 'updated');
$response = Director::test($url, $data, null, 'PUT');
$body = 'Name=Updated Comment&Comment=updated';
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded'
);
$response = Director::test($url, null, null, 'PUT', $body, $headers);
$this->assertEquals($response->getStatusCode(), 200); // Success
// Assumption: XML is default output
$responseArr = Convert::xml2array($response->getBody());
$this->assertEquals($responseArr['ID'], 1);
$this->assertEquals($responseArr['Comment'], 'updated');
$this->assertEquals($responseArr['Name'], 'Updated Comment');
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
}
@ -111,16 +115,20 @@ class RestfulServerTest extends SapphireTest {
public function testPOSTWithFormEncoded() {
$_SERVER['PHP_AUTH_USER'] = 'editor@test.com';
$_SERVER['PHP_AUTH_PW'] = 'editor';
$url = "/api/v1/RestfulServerTest_Comment";
$data = array('Comment' => 'created');
$response = Director::test($url, $data, null, 'POST');
$body = 'Name=New Comment&Comment=created';
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded'
);
$response = Director::test($url, null, null, 'POST', $body, $headers);
$this->assertEquals($response->getStatusCode(), 201); // Created
// Assumption: XML is default output
$responseArr = Convert::xml2array($response->getBody());
$this->assertEquals($responseArr['ID'], 2);
$this->assertEquals($responseArr['Comment'], 'created');
$this->assertEquals($responseArr['Name'], 'New Comment');
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
}