diff --git a/api/FormEncodedDataFormatter.php b/api/FormEncodedDataFormatter.php new file mode 100644 index 000000000..2a9d9edfe --- /dev/null +++ b/api/FormEncodedDataFormatter.php @@ -0,0 +1,43 @@ + + * 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 + * + * + * @todo Format response form encoded as well - currently uses XMLDataFormatter + * + * @author Cam Spiers + * + * @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); + } + +} +?> \ No newline at end of file diff --git a/tests/api/RestfulServerTest.php b/tests/api/RestfulServerTest.php index 9e8b80a9f..c31074787 100644 --- a/tests/api/RestfulServerTest.php +++ b/tests/api/RestfulServerTest.php @@ -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']); }