ENHANCEMENT: make RestfulService support PUT method.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@108942 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Mateusz Uzdowski 2010-08-05 03:49:49 +00:00 committed by Sam Minnee
parent e8e3ea6680
commit b6aef41d1c
2 changed files with 27 additions and 5 deletions

View File

@ -149,11 +149,20 @@ class RestfulService extends ViewableData {
// Add authentication
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
// Add fields to POST requests
// Add fields to POST and PUT requests
if($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if($method == 'PUT') {
$put = fopen("php://temp", 'r+');
fwrite($put, $data);
fseek($put, 0);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_INFILE, $put);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
}
// Apply proxy settings
if(is_array($this->proxy)) {

View File

@ -60,6 +60,13 @@ class RestfulServiceTest extends SapphireTest {
$this->assertContains("<post_item name=\"$key\">$value</post_item>", $responseBody);
}
}
function testPutData() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
$data = 'testPutData';
$responseBody = $service->request('RestfulServiceTest_Controller/', 'PUT', $data)->getBody();
$this->assertContains("<body>$data</body>", $responseBody);
}
function testConnectionDoesntCacheWithDifferentUrl() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
@ -143,6 +150,7 @@ class RestfulServiceTest_Controller extends Controller {
foreach ($this->request->postVars() as $key => $value) {
$post .= "\t\t<post_item name=\"$key\">$value</post_item>\n";
}
$body = $this->request->getBody();
$out = <<<XML
<?xml version="1.0"?>
@ -150,6 +158,7 @@ class RestfulServiceTest_Controller extends Controller {
<request>$request</request>
<get>$get</get>
<post>$post</post>
<body>$body</body>
</test>
XML;
$this->response->setBody($out);
@ -245,10 +254,14 @@ class RestfulServiceTest_MockRestfulService extends RestfulService {
}
// Custom for mock implementation: Use Director::test()
$getVars = ($method == 'GET') ? $data : null;
$postVars = ($method == 'POST') ? $data : null;
$responseFromDirector = Director::test($url, $postVars, $this->session, $method, $getVars, $headers);
$body = null;
$postVars = null;
if($method!='POST') $body = $data;
else $postVars = $data;
$responseFromDirector = Director::test($url, $postVars, $this->session, $method, $body, $headers);
$response = new RestfulService_Response(
$responseFromDirector->getBody(),
$responseFromDirector->getStatusCode()