mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
e8e3ea6680
commit
b6aef41d1c
@ -149,11 +149,20 @@ class RestfulService extends ViewableData {
|
|||||||
// Add authentication
|
// Add authentication
|
||||||
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
|
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') {
|
if($method == 'POST') {
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
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
|
// Apply proxy settings
|
||||||
if(is_array($this->proxy)) {
|
if(is_array($this->proxy)) {
|
||||||
|
@ -60,6 +60,13 @@ class RestfulServiceTest extends SapphireTest {
|
|||||||
$this->assertContains("<post_item name=\"$key\">$value</post_item>", $responseBody);
|
$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() {
|
function testConnectionDoesntCacheWithDifferentUrl() {
|
||||||
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
||||||
@ -143,6 +150,7 @@ class RestfulServiceTest_Controller extends Controller {
|
|||||||
foreach ($this->request->postVars() as $key => $value) {
|
foreach ($this->request->postVars() as $key => $value) {
|
||||||
$post .= "\t\t<post_item name=\"$key\">$value</post_item>\n";
|
$post .= "\t\t<post_item name=\"$key\">$value</post_item>\n";
|
||||||
}
|
}
|
||||||
|
$body = $this->request->getBody();
|
||||||
|
|
||||||
$out = <<<XML
|
$out = <<<XML
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
@ -150,6 +158,7 @@ class RestfulServiceTest_Controller extends Controller {
|
|||||||
<request>$request</request>
|
<request>$request</request>
|
||||||
<get>$get</get>
|
<get>$get</get>
|
||||||
<post>$post</post>
|
<post>$post</post>
|
||||||
|
<body>$body</body>
|
||||||
</test>
|
</test>
|
||||||
XML;
|
XML;
|
||||||
$this->response->setBody($out);
|
$this->response->setBody($out);
|
||||||
@ -245,10 +254,14 @@ class RestfulServiceTest_MockRestfulService extends RestfulService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Custom for mock implementation: Use Director::test()
|
// Custom for mock implementation: Use Director::test()
|
||||||
$getVars = ($method == 'GET') ? $data : null;
|
$body = null;
|
||||||
$postVars = ($method == 'POST') ? $data : null;
|
$postVars = null;
|
||||||
$responseFromDirector = Director::test($url, $postVars, $this->session, $method, $getVars, $headers);
|
|
||||||
|
if($method!='POST') $body = $data;
|
||||||
|
else $postVars = $data;
|
||||||
|
|
||||||
|
$responseFromDirector = Director::test($url, $postVars, $this->session, $method, $body, $headers);
|
||||||
|
|
||||||
$response = new RestfulService_Response(
|
$response = new RestfulService_Response(
|
||||||
$responseFromDirector->getBody(),
|
$responseFromDirector->getBody(),
|
||||||
$responseFromDirector->getStatusCode()
|
$responseFromDirector->getStatusCode()
|
||||||
|
Loading…
Reference in New Issue
Block a user