mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Allow use of RestfulService->setQueryString() and test to support it. Patch from ticket #2973. Thanks to simon_w!
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64596 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
a51d7184a3
commit
6195772779
@ -65,6 +65,14 @@ class RestfulService extends ViewableData {
|
||||
*/
|
||||
public function request($subURL = '', $method = "GET", $data = null, $headers = null) {
|
||||
$url = $this->baseURL . $subURL; //url for the request
|
||||
if($this->queryString) {
|
||||
if(strpos($url, '?') !== false) {
|
||||
$url .= '&' . $this->queryString;
|
||||
} else {
|
||||
$url .= '?' . $this->queryString;
|
||||
}
|
||||
}
|
||||
$url = str_replace(' ', '%20', $url); // spaces should be encoded
|
||||
$method = strtoupper($method);
|
||||
|
||||
assert(in_array($method, array('GET','POST','PUT','DELETE','HEAD','OPTIONS')));
|
||||
@ -95,10 +103,15 @@ class RestfulService extends ViewableData {
|
||||
if($this->customHeaders) {
|
||||
$headers = array_merge((array)$this->customHeaders, (array)$headers);
|
||||
}
|
||||
|
||||
|
||||
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
if($this->authUsername) curl_setopt($ch, CURLOPT_USERPWD, "$this->authUsername:$this->authPassword");
|
||||
|
||||
|
||||
if($method == 'POST') {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
}
|
||||
|
||||
$responseBody = curl_exec($ch);
|
||||
|
||||
if($responseBody === false) {
|
||||
@ -320,4 +333,4 @@ class RestfulService_Response extends HTTPResponse {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
91
tests/api/RestfulServiceTest.php
Normal file
91
tests/api/RestfulServiceTest.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
class RestfulServiceTest extends SapphireTest {
|
||||
function testGetData() {
|
||||
$connection = new RestfulService(Director::absoluteBaseURL());
|
||||
$test1params = array(
|
||||
'test1a' => 4352655636.76543, // number test
|
||||
'test1b' => '$&+,/:;=?@#"\'%', // special char test. These should all get encoded
|
||||
'test1c' => 'And now for a string test' // string test
|
||||
);
|
||||
$connection->setQueryString($test1params);
|
||||
$test1 = $connection->request('RestfulServiceTest_Controller')->getBody();
|
||||
foreach ($test1params as $key => $value) {
|
||||
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $test1);
|
||||
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $test1);
|
||||
}
|
||||
$connection->setQueryString(array());
|
||||
$test2params = array(
|
||||
'test2a' => 767545678.76887, // number test
|
||||
'test2b' => '%\'"@?=;:/,$', // special character checks
|
||||
'test2c' => 'And now for a string test', // string test
|
||||
);
|
||||
$test2suburl = 'RestfulServiceTest_Controller/?';
|
||||
foreach ($test2params as $key=>$value) {
|
||||
$test2suburl .= "$key=$value&";
|
||||
}
|
||||
$test2suburl = substr($test2suburl, 0, -1);
|
||||
$test2 = $connection->request($test2suburl)->getBody();
|
||||
foreach ($test2params as $key => $value) {
|
||||
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $test2);
|
||||
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $test2);
|
||||
}
|
||||
$test3params = array_merge($test1params, $test2params); // We want to check using setQueryString() and hard coded
|
||||
$connection->setQueryString($test1params);
|
||||
$test3 = $connection->request($test2suburl)->getBody();
|
||||
foreach ($test3params as $key => $value) {
|
||||
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $test3);
|
||||
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $test3);
|
||||
}
|
||||
}
|
||||
|
||||
function testPostData() {
|
||||
$connection = new RestfulService(Director::absoluteBaseURL());
|
||||
$test1params = array(
|
||||
'test1a' => mktime(),
|
||||
'test1b' => mt_rand(),
|
||||
'test1c' => 'And now for a string test'
|
||||
);
|
||||
$test1 = $connection->request('RestfulServiceTest_Controller', 'POST', $test1params)->getBody();
|
||||
foreach ($test1params as $key => $value) {
|
||||
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $test1);
|
||||
$this->assertContains("<post_item name=\"$key\">$value</post_item>", $test1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class RestfulServiceTest_Controller extends Controller {
|
||||
public function index() {
|
||||
ContentNegotiator::disable();
|
||||
$request_count = count($_REQUEST);
|
||||
$get_count = count($_GET);
|
||||
$post_count = count($_POST);
|
||||
$request = '';
|
||||
foreach ($_REQUEST as $key=>$value) {
|
||||
$request .= "\t\t<request_item name=\"$key\">$value</request_item>\n";
|
||||
}
|
||||
$get = '';
|
||||
foreach ($_GET as $key => $value) {
|
||||
$get .= "\t\t<get_item name=\"$key\">$value</get_item>\n";
|
||||
}
|
||||
$post = '';
|
||||
foreach ($_POST as $key => $value) {
|
||||
$post .= "\t\t<post_item name=\"$key\">$value</post_item>\n";
|
||||
}
|
||||
$out = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<test>
|
||||
<request count="$request_count">
|
||||
$request </request>
|
||||
<get count="$get_count">
|
||||
$get </get>
|
||||
<post count="$post_count">
|
||||
$post </post>
|
||||
</test>
|
||||
XML;
|
||||
header('Content-type: text/xml');
|
||||
echo $out;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user