diff --git a/api/RestfulService.php b/api/RestfulService.php index 4866ff217..6d3b7ade1 100644 --- a/api/RestfulService.php +++ b/api/RestfulService.php @@ -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)) { diff --git a/tests/api/RestfulServiceTest.php b/tests/api/RestfulServiceTest.php index 5e62246d6..2ad7adef1 100644 --- a/tests/api/RestfulServiceTest.php +++ b/tests/api/RestfulServiceTest.php @@ -60,6 +60,13 @@ class RestfulServiceTest extends SapphireTest { $this->assertContains("$value", $responseBody); } } + + function testPutData() { + $service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0); + $data = 'testPutData'; + $responseBody = $service->request('RestfulServiceTest_Controller/', 'PUT', $data)->getBody(); + $this->assertContains("$data", $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$value\n"; } + $body = $this->request->getBody(); $out = << @@ -150,6 +158,7 @@ class RestfulServiceTest_Controller extends Controller { $request $get $post + $body 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()