2008-10-21 05:45:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class RestfulServiceTest extends SapphireTest {
|
2009-06-24 05:05:33 +02:00
|
|
|
|
|
|
|
function testSpecialCharacters() {
|
|
|
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
|
|
|
$url = 'RestfulServiceTest_Controller/';
|
|
|
|
$params = array(
|
2008-10-21 05:45:44 +02:00
|
|
|
'test1a' => 4352655636.76543, // number test
|
2009-06-26 05:14:22 +02:00
|
|
|
'test1b' => '$&+,/:;=?@#%', // special char test. These should all get encoded
|
2008-10-21 05:45:44 +02:00
|
|
|
'test1c' => 'And now for a string test' // string test
|
|
|
|
);
|
2009-06-24 05:05:33 +02:00
|
|
|
$service->setQueryString($params);
|
|
|
|
$responseBody = $service->request($url)->getBody();
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
|
|
|
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $responseBody);
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
2009-06-24 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetDataWithSetQueryString() {
|
|
|
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
|
|
|
$url = 'RestfulServiceTest_Controller/';
|
|
|
|
$params = array(
|
|
|
|
'test1a' => 'val1a',
|
|
|
|
'test1b' => 'val1b'
|
2008-10-21 05:45:44 +02:00
|
|
|
);
|
2009-06-24 05:05:33 +02:00
|
|
|
$service->setQueryString($params);
|
|
|
|
$responseBody = $service->request($url)->getBody();
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
|
|
|
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $responseBody);
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
2009-06-24 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetDataWithUrlParameters() {
|
|
|
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
|
|
|
$url = 'RestfulServiceTest_Controller/';
|
|
|
|
$params = array(
|
|
|
|
'test1a' => 'val1a',
|
|
|
|
'test1b' => 'val1b'
|
|
|
|
);
|
|
|
|
$url .= '?' . http_build_query($params);
|
|
|
|
$responseBody = $service->request($url)->getBody();
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
|
|
|
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $responseBody);
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testPostData() {
|
2009-06-24 05:05:33 +02:00
|
|
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
|
|
|
|
$params = array(
|
|
|
|
'test1a' => 'val1a',
|
|
|
|
'test1b' => 'val1b'
|
2008-10-21 05:45:44 +02:00
|
|
|
);
|
2009-06-24 05:05:33 +02:00
|
|
|
$responseBody = $service->request('RestfulServiceTest_Controller/', 'POST', $params)->getBody();
|
|
|
|
foreach ($params as $key => $value) {
|
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
|
|
|
$this->assertContains("<post_item name=\"$key\">$value</post_item>", $responseBody);
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-19 02:51:33 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2009-06-24 05:05:33 +02:00
|
|
|
|
|
|
|
function testConnectionDoesntCacheWithDifferentUrl() {
|
|
|
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
|
|
|
$url = 'RestfulServiceTest_Controller/';
|
|
|
|
|
|
|
|
// First run
|
|
|
|
$params = array(
|
|
|
|
'test1a' => 'first run',
|
|
|
|
);
|
|
|
|
$service->setQueryString($params);
|
|
|
|
$responseBody = $service->request($url)->getBody();
|
|
|
|
$this->assertContains("<request_item name=\"test1a\">first run</request_item>", $responseBody);
|
|
|
|
|
|
|
|
// Second run
|
|
|
|
$params = array(
|
|
|
|
'test1a' => 'second run',
|
|
|
|
);
|
|
|
|
$service->setQueryString($params);
|
|
|
|
$responseBody = $service->request($url)->getBody();
|
|
|
|
$this->assertContains("<request_item name=\"test1a\">second run</request_item>", $responseBody);
|
|
|
|
}
|
|
|
|
|
2009-07-02 09:18:18 +02:00
|
|
|
/**
|
|
|
|
* @expectedException PHPUnit_Framework_Error
|
|
|
|
*/
|
|
|
|
function testIncorrectData() {
|
|
|
|
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
|
|
|
$test1 = $connection->request('RestfulServiceTest_Controller/invalid?usetestmanifest=1&flush=1');
|
|
|
|
$test1->xpath("\\fail");
|
|
|
|
}
|
2010-10-19 02:36:48 +02:00
|
|
|
|
|
|
|
function testHttpErrorWithoutCache() {
|
|
|
|
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
|
|
|
$response = $connection->request('RestfulServiceTest_Controller/httpErrorWithoutCache?usetestmanifest=1&flush=1');
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertFalse($response->getCachedBody());
|
|
|
|
$this->assertContains("<error>HTTP Error</error>", $response->getBody());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testHttpErrorWithCache() {
|
|
|
|
$subUrl = 'RestfulServiceTest_Controller/httpErrorWithCache?usetestmanifest=1&flush=1';
|
|
|
|
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
|
|
|
$this->createFakeCachedResponse($connection, $subUrl);
|
|
|
|
$response = $connection->request($subUrl);
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertEquals("Cache response body",$response->getCachedBody());
|
|
|
|
$this->assertContains("<error>HTTP Error</error>", $response->getBody());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simulate cached response file for testing error requests that are supposed to have cache files
|
|
|
|
*/
|
|
|
|
private function createFakeCachedResponse($connection, $subUrl) {
|
|
|
|
$fullUrl = $connection->getAbsoluteRequestURL($subUrl);
|
|
|
|
$cachedir = TEMP_FOLDER; // Default silverstripe cache
|
|
|
|
$cache_file = md5($fullUrl); // Encoded name of cache file
|
|
|
|
$cache_path = $cachedir."/xmlresponse_$cache_file";
|
|
|
|
$cacheResponse = new RestfulService_Response("Cache response body");
|
|
|
|
$store = serialize($cacheResponse);
|
|
|
|
file_put_contents($cache_path, $store);
|
|
|
|
}
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class RestfulServiceTest_Controller extends Controller {
|
2010-10-19 03:26:09 +02:00
|
|
|
public function init() {
|
|
|
|
$this->basicAuthEnabled = false;
|
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
2008-10-21 05:45:44 +02:00
|
|
|
public function index() {
|
|
|
|
$request = '';
|
2009-06-24 05:05:33 +02:00
|
|
|
foreach ($this->request->requestVars() as $key=>$value) {
|
2008-10-21 05:45:44 +02:00
|
|
|
$request .= "\t\t<request_item name=\"$key\">$value</request_item>\n";
|
|
|
|
}
|
|
|
|
$get = '';
|
2009-06-24 05:05:33 +02:00
|
|
|
foreach ($this->request->getVars() as $key => $value) {
|
2008-10-21 05:45:44 +02:00
|
|
|
$get .= "\t\t<get_item name=\"$key\">$value</get_item>\n";
|
|
|
|
}
|
|
|
|
$post = '';
|
2009-06-24 05:05:33 +02:00
|
|
|
foreach ($this->request->postVars() as $key => $value) {
|
2008-10-21 05:45:44 +02:00
|
|
|
$post .= "\t\t<post_item name=\"$key\">$value</post_item>\n";
|
|
|
|
}
|
2010-10-19 02:51:33 +02:00
|
|
|
$body = $this->request->getBody();
|
2009-06-24 05:05:33 +02:00
|
|
|
|
2008-10-21 05:45:44 +02:00
|
|
|
$out = <<<XML
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<test>
|
2009-06-24 05:05:33 +02:00
|
|
|
<request>$request</request>
|
|
|
|
<get>$get</get>
|
|
|
|
<post>$post</post>
|
2010-10-19 02:51:33 +02:00
|
|
|
<body>$body</body>
|
2008-10-21 05:45:44 +02:00
|
|
|
</test>
|
|
|
|
XML;
|
2009-06-24 05:05:33 +02:00
|
|
|
$this->response->setBody($out);
|
|
|
|
$this->response->addHeader('Content-type', 'text/xml');
|
|
|
|
|
|
|
|
return $this->response;
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
2009-07-02 09:18:18 +02:00
|
|
|
|
|
|
|
public function invalid() {
|
|
|
|
$out = <<<XML
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<test>
|
|
|
|
<fail><invalid>
|
|
|
|
</test>
|
|
|
|
XML;
|
|
|
|
header('Content-type: text/xml');
|
|
|
|
echo $out;
|
|
|
|
}
|
2010-10-19 02:36:48 +02:00
|
|
|
|
|
|
|
public function httpErrorWithoutCache() {
|
|
|
|
$out = <<<XML
|
|
|
|
<?xml version="1.0"?>
|
|
|
|
<test>
|
|
|
|
<error>HTTP Error</error>
|
|
|
|
</test>
|
|
|
|
XML;
|
|
|
|
|
|
|
|
$this->response->setBody($out);
|
|
|
|
$this->response->setStatusCode(400);
|
|
|
|
$this->response->addHeader('Content-type', 'text/xml');
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The body of this method is the same as self::httpErrorWithoutCache()
|
|
|
|
* but we need it for caching since caching using request url to determine path to cache file
|
|
|
|
*/
|
|
|
|
public function httpErrorWithCache() {
|
|
|
|
return $this->httpErrorWithoutCache();
|
|
|
|
}
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
|
2009-06-24 05:05:33 +02:00
|
|
|
/**
|
|
|
|
* Mock implementation of {@link RestfulService}, which uses {@link Director::test()}
|
|
|
|
* instead of direct curl system calls.
|
|
|
|
*
|
|
|
|
* @todo Less overloading of request()
|
|
|
|
* @todo Currently only works with relative (internal) URLs
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class RestfulServiceTest_MockRestfulService extends RestfulService {
|
|
|
|
|
|
|
|
public $session = null;
|
|
|
|
|
|
|
|
public function request($subURL = '', $method = "GET", $data = null, $headers = null) {
|
|
|
|
|
|
|
|
if(!$this->session) {
|
|
|
|
$this->session = new Session(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
$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); // Encode spaces
|
|
|
|
|
|
|
|
// Custom for mock implementation: Director::test() doesn't cope with absolute URLs
|
|
|
|
$url = Director::makeRelative($url);
|
|
|
|
|
|
|
|
$method = strtoupper($method);
|
|
|
|
|
|
|
|
assert(in_array($method, array('GET','POST','PUT','DELETE','HEAD','OPTIONS')));
|
|
|
|
|
|
|
|
// Add headers
|
|
|
|
if($this->customHeaders) {
|
|
|
|
$headers = array_merge((array)$this->customHeaders, (array)$headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add authentication
|
|
|
|
if($this->authUsername) {
|
|
|
|
$headers[] = "Authorization: Basic " . base64_encode(
|
|
|
|
$this->authUsername.':'.$this->authPassword
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom for mock implementation: Use Director::test()
|
2010-10-19 02:51:33 +02:00
|
|
|
$body = null;
|
|
|
|
$postVars = null;
|
|
|
|
|
|
|
|
if($method!='POST') $body = $data;
|
|
|
|
else $postVars = $data;
|
|
|
|
|
|
|
|
$responseFromDirector = Director::test($url, $postVars, $this->session, $method, $body, $headers);
|
|
|
|
|
2009-06-24 05:05:33 +02:00
|
|
|
$response = new RestfulService_Response(
|
|
|
|
$responseFromDirector->getBody(),
|
|
|
|
$responseFromDirector->getStatusCode()
|
|
|
|
);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
2009-07-02 09:18:18 +02:00
|
|
|
?>
|