mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API CHANGE: Separated the actual CURL request generation code from RestfulService::request() into RestfulService::curlRequest().
MINOR: Updated RestfulServiceTest to use a mock service rather than the actual site. This eliminates the need for ?usetestmanifesst.
This commit is contained in:
parent
2d4fe274a6
commit
eb1a3fe75a
@ -121,9 +121,47 @@ class RestfulService extends ViewableData {
|
||||
$response = unserialize($store);
|
||||
|
||||
} else {
|
||||
$response = $this->curlRequest($url, $method, $data, $headers, $curlOptions);
|
||||
|
||||
if(!$response->isError()) {
|
||||
// Serialise response object and write to cache
|
||||
$store = serialize($response);
|
||||
file_put_contents($cache_path, $store);
|
||||
}
|
||||
else {
|
||||
// In case of curl or/and http indicate error, populate response's cachedBody property
|
||||
// with cached response body with the cache file exists
|
||||
if (@file_exists($cache_path)) {
|
||||
$store = file_get_contents($cache_path);
|
||||
$cachedResponse = unserialize($store);
|
||||
|
||||
$response->setCachedBody($cachedResponse->getBody());
|
||||
}
|
||||
else {
|
||||
$response->setCachedBody(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually performs a remote service request using curl. This is used by
|
||||
* {@link RestfulService::request()}.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param array $data
|
||||
* @param array $headers
|
||||
* @param array $curlOptions
|
||||
* @return RestfulService_Response
|
||||
*/
|
||||
public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
|
||||
$ch = curl_init();
|
||||
$timeout = 5;
|
||||
$useragent = "SilverStripe/" . SapphireInfo::Version();
|
||||
$useragent = 'SilverStripe/' . SapphireInfo::Version();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
|
||||
@ -145,8 +183,7 @@ class RestfulService extends ViewableData {
|
||||
if($method == 'POST') {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
}
|
||||
else if($method == 'PUT') {
|
||||
} elseif($method == 'PUT') {
|
||||
$put = fopen("php://temp", 'r+');
|
||||
fwrite($put, $data);
|
||||
fseek($put, 0);
|
||||
@ -182,26 +219,6 @@ class RestfulService extends ViewableData {
|
||||
$response = new RestfulService_Response($responseBody, $statusCode);
|
||||
curl_close($ch);
|
||||
|
||||
if($curlError === '' && !$response->isError()) {
|
||||
// Serialise response object and write to cache
|
||||
$store = serialize($response);
|
||||
file_put_contents($cache_path, $store);
|
||||
}
|
||||
else {
|
||||
// In case of curl or/and http indicate error, populate response's cachedBody property
|
||||
// with cached response body with the cache file exists
|
||||
if (@file_exists($cache_path)) {
|
||||
$store = file_get_contents($cache_path);
|
||||
$cachedResponse = unserialize($store);
|
||||
|
||||
$response->setCachedBody($cachedResponse->getBody());
|
||||
}
|
||||
else {
|
||||
$response->setCachedBody(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
@ -94,13 +94,13 @@ class RestfulServiceTest extends SapphireTest {
|
||||
*/
|
||||
function testIncorrectData() {
|
||||
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
||||
$test1 = $connection->request('RestfulServiceTest_Controller/invalid?usetestmanifest=1&flush=1');
|
||||
$test1 = $connection->request('RestfulServiceTest_Controller/invalid');
|
||||
$test1->xpath("\\fail");
|
||||
}
|
||||
|
||||
function testHttpErrorWithoutCache() {
|
||||
$connection = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
|
||||
$response = $connection->request('RestfulServiceTest_Controller/httpErrorWithoutCache?usetestmanifest=1&flush=1');
|
||||
$response = $connection->request('RestfulServiceTest_Controller/httpErrorWithoutCache');
|
||||
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertFalse($response->getCachedBody());
|
||||
@ -110,7 +110,7 @@ class RestfulServiceTest extends SapphireTest {
|
||||
|
||||
function testHttpErrorWithCache() {
|
||||
$subUrl = 'RestfulServiceTest_Controller/httpErrorWithCache?usetestmanifest=1&flush=1';
|
||||
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
||||
$connection = new RestfulServiceTest_MockErrorService(Director::absoluteBaseURL(), 0);
|
||||
$this->createFakeCachedResponse($connection, $subUrl);
|
||||
$response = $connection->request($subUrl);
|
||||
|
||||
@ -272,3 +272,14 @@ class RestfulServiceTest_MockRestfulService extends RestfulService {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A mock service that returns a 400 error for requests.
|
||||
*/
|
||||
class RestfulServiceTest_MockErrorService extends RestfulService {
|
||||
|
||||
public function curlRequest() {
|
||||
return new RestfulService_Response('<error>HTTP Error</error>', 400);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user