2008-10-21 05:45:44 +02:00
|
|
|
<?php
|
|
|
|
|
2012-06-29 10:45:57 +02:00
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2008-10-21 05:45:44 +02:00
|
|
|
class RestfulServiceTest extends SapphireTest {
|
2009-06-24 05:05:33 +02:00
|
|
|
|
2012-06-29 10:45:57 +02:00
|
|
|
protected $member_unique_identifier_field = '';
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2012-06-29 10:45:57 +02:00
|
|
|
// backup the project unique identifier field
|
2013-03-21 19:48:54 +01:00
|
|
|
$this->member_unique_identifier_field = Member::config()->unique_identifier_field;
|
2012-06-29 10:45:57 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->unique_identifier_field = 'Email';
|
2012-06-29 10:45:57 +02:00
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2012-06-29 10:45:57 +02:00
|
|
|
parent::tearDown();
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
// set old Member::config()->unique_identifier_field value
|
2012-06-29 10:45:57 +02:00
|
|
|
if ($this->member_unique_identifier_field) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->unique_identifier_field = $this->member_unique_identifier_field;
|
2012-06-29 10:45:57 +02:00
|
|
|
}
|
2014-05-07 20:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check we can put slashes anywhere and it works
|
|
|
|
*/
|
|
|
|
public function testGetAbsoluteURLSlashes() {
|
|
|
|
$urls = array(
|
|
|
|
'/url/',
|
|
|
|
'url',
|
|
|
|
'/url',
|
|
|
|
'url/',
|
|
|
|
);
|
|
|
|
$restWithoutSlash = new RestfulService('http://example.com');
|
|
|
|
$restWithSlash = new RestfulService('http://example.com/');
|
|
|
|
foreach ($urls as $url) {
|
|
|
|
$url = ltrim($url, '/');
|
|
|
|
$this->assertEquals("http://example.com/$url", $restWithoutSlash->getAbsoluteRequestURL($url));
|
|
|
|
$this->assertEquals("http://example.com/$url", $restWithSlash->getAbsoluteRequestURL($url));
|
|
|
|
$this->assertEquals($restWithoutSlash->getAbsoluteRequestURL($url), $restWithSlash->getAbsoluteRequestURL($url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check we can add query strings all over the shop and it's ok
|
|
|
|
*/
|
|
|
|
public function testGetAbsoluteURLQueries() {
|
|
|
|
$restWithoutSlash = new RestfulService('http://example.com?b=query2');
|
|
|
|
$restWithSlash = new RestfulService('http://example.com/?b=query2');
|
|
|
|
$restWithQuery = new RestfulService('http://example.com/?b=query2');
|
|
|
|
$restWithQuery->setQueryString(array(
|
|
|
|
'c' => 'query3',
|
|
|
|
));
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2&a=query1', $restWithoutSlash->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2&a=query1', $restWithSlash->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2&a=query1&c=query3', $restWithQuery->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2', $restWithoutSlash->getAbsoluteRequestURL('url'));
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2', $restWithSlash->getAbsoluteRequestURL('url'));
|
|
|
|
$this->assertEquals('http://example.com/url?b=query2&c=query3', $restWithQuery->getAbsoluteRequestURL('url'));
|
|
|
|
|
|
|
|
$restWithoutSlash = new RestfulService('http://example.com');
|
|
|
|
$restWithSlash = new RestfulService('http://example.com/');
|
|
|
|
$restWithQuery = new RestfulService('http://example.com/');
|
|
|
|
$restWithQuery->setQueryString(array(
|
|
|
|
'c' => 'query3',
|
|
|
|
));
|
|
|
|
$this->assertEquals('http://example.com/url?a=query1', $restWithoutSlash->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
$this->assertEquals('http://example.com/url?a=query1', $restWithSlash->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
$this->assertEquals('http://example.com/url?a=query1&c=query3', $restWithQuery->getAbsoluteRequestURL('url?a=query1'));
|
|
|
|
|
|
|
|
$this->assertEquals('http://example.com/url', $restWithoutSlash->getAbsoluteRequestURL('url'));
|
|
|
|
$this->assertEquals('http://example.com/url', $restWithSlash->getAbsoluteRequestURL('url'));
|
|
|
|
$this->assertEquals('http://example.com/url?c=query3', $restWithQuery->getAbsoluteRequestURL('url'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check spaces are encoded
|
|
|
|
*/
|
|
|
|
public function testGetAbsoluteURLWithSpaces() {
|
|
|
|
$rest = new RestfulService('http://example.com');
|
|
|
|
$this->assertEquals('http://example.com/query%20with%20spaces', $rest->getAbsoluteRequestURL('query with spaces'));
|
|
|
|
}
|
2012-06-29 10:45:57 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSpecialCharacters() {
|
2009-06-24 05:05:33 +02:00
|
|
|
$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) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
2009-06-24 05:05:33 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetDataWithSetQueryString() {
|
2009-06-24 05:05:33 +02:00
|
|
|
$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) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
2009-06-24 05:05:33 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testGetDataWithUrlParameters() {
|
2009-06-24 05:05:33 +02:00
|
|
|
$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) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->assertContains("<request_item name=\"$key\">$value</request_item>", $responseBody);
|
2009-06-24 05:05:33 +02:00
|
|
|
$this->assertContains("<get_item name=\"$key\">$value</get_item>", $responseBody);
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testPutData() {
|
2010-10-19 02:51:33 +02:00
|
|
|
$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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testConnectionDoesntCacheWithDifferentUrl() {
|
2009-06-24 05:05:33 +02:00
|
|
|
$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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testIncorrectData() {
|
2009-07-02 09:18:18 +02:00
|
|
|
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
|
2011-03-23 10:05:29 +01:00
|
|
|
$test1 = $connection->request('RestfulServiceTest_Controller/invalid');
|
2009-07-02 09:18:18 +02:00
|
|
|
$test1->xpath("\\fail");
|
|
|
|
}
|
2010-10-19 02:36:48 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHttpErrorWithoutCache() {
|
2010-12-20 00:04:44 +01:00
|
|
|
$connection = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
|
2011-03-23 10:05:29 +01:00
|
|
|
$response = $connection->request('RestfulServiceTest_Controller/httpErrorWithoutCache');
|
2010-12-20 00:04:44 +01:00
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
$this->assertEquals(400, $response->getStatusCode());
|
|
|
|
$this->assertFalse($response->getCachedBody());
|
|
|
|
$this->assertContains("<error>HTTP Error</error>", $response->getBody());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testHttpErrorWithCache() {
|
2011-03-27 21:14:03 +02:00
|
|
|
$subUrl = 'RestfulServiceTest_Controller/httpErrorWithCache';
|
2011-03-23 10:05:29 +01:00
|
|
|
$connection = new RestfulServiceTest_MockErrorService(Director::absoluteBaseURL(), 0);
|
2010-10-19 02:36:48 +02:00
|
|
|
$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
|
2013-01-30 18:10:37 +01:00
|
|
|
*
|
|
|
|
* @todo Generate the cachepath without hardcoding the cache data
|
2010-10-19 02:36:48 +02:00
|
|
|
*/
|
|
|
|
private function createFakeCachedResponse($connection, $subUrl) {
|
|
|
|
$fullUrl = $connection->getAbsoluteRequestURL($subUrl);
|
2013-01-30 18:10:37 +01:00
|
|
|
//these are the defaul values that one would expect in the
|
|
|
|
$basicAuthStringMethod = new ReflectionMethod('RestfulServiceTest_MockErrorService', 'getBasicAuthString');
|
|
|
|
$basicAuthStringMethod->setAccessible(true);
|
|
|
|
$cachePathMethod = new ReflectionMethod('RestfulServiceTest_MockErrorService', 'getCachePath');
|
|
|
|
$cachePathMethod->setAccessible(true);
|
|
|
|
$cache_path = $cachePathMethod->invokeArgs($connection, array(array(
|
|
|
|
$fullUrl,
|
|
|
|
'GET',
|
|
|
|
null,
|
|
|
|
array(),
|
|
|
|
array(),
|
|
|
|
$basicAuthStringMethod->invoke($connection)
|
|
|
|
)));
|
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
$cacheResponse = new RestfulService_Response("Cache response body");
|
|
|
|
$store = serialize($cacheResponse);
|
|
|
|
file_put_contents($cache_path, $store);
|
|
|
|
}
|
2013-01-30 18:10:37 +01:00
|
|
|
|
|
|
|
public function testHttpHeaderParseing() {
|
|
|
|
$headers = "content-type: text/html; charset=UTF-8\r\n".
|
|
|
|
"Server: Funky/1.0\r\n".
|
2013-07-10 13:47:13 +02:00
|
|
|
"X-BB-ExampleMANycaPS: test\r\n".
|
2013-01-30 18:10:37 +01:00
|
|
|
"Set-Cookie: foo=bar\r\n".
|
|
|
|
"Set-Cookie: baz=quux\r\n".
|
|
|
|
"Set-Cookie: bar=foo\r\n";
|
|
|
|
$expected = array(
|
|
|
|
'Content-Type' => 'text/html; charset=UTF-8',
|
|
|
|
'Server' => 'Funky/1.0',
|
2013-07-10 13:47:13 +02:00
|
|
|
'X-BB-ExampleMANycaPS' => 'test',
|
2013-01-30 18:10:37 +01:00
|
|
|
'Set-Cookie' => array(
|
|
|
|
'foo=bar',
|
|
|
|
'baz=quux',
|
|
|
|
'bar=foo'
|
|
|
|
)
|
|
|
|
);
|
2013-01-31 17:41:24 +01:00
|
|
|
$headerFunction = new ReflectionMethod('RestfulService', 'parseRawHeaders');
|
2013-01-30 18:10:37 +01:00
|
|
|
$headerFunction->setAccessible(true);
|
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$headerFunction->invoke(
|
|
|
|
new RestfulService(Director::absoluteBaseURL(),0), $headers
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-09-20 04:20:35 +02:00
|
|
|
public function testExtractResponseRedirectionAndProxy() {
|
|
|
|
// This is an example of real raw response for a request via a proxy that gets redirected.
|
2013-09-20 06:26:39 +02:00
|
|
|
$rawHeaders =
|
2013-09-20 04:20:35 +02:00
|
|
|
"HTTP/1.0 200 Connection established\r\n" .
|
|
|
|
"\r\n" .
|
|
|
|
"HTTP/1.1 301 Moved Permanently\r\n" .
|
|
|
|
"Server: nginx\r\n" .
|
|
|
|
"Date: Fri, 20 Sep 2013 01:53:07 GMT\r\n" .
|
|
|
|
"Content-Type: text/html\r\n" .
|
|
|
|
"Content-Length: 178\r\n" .
|
|
|
|
"Connection: keep-alive\r\n" .
|
|
|
|
"Location: https://www.foobar.org.nz/\r\n" .
|
|
|
|
"\r\n" .
|
|
|
|
"HTTP/1.0 200 Connection established\r\n" .
|
|
|
|
"\r\n" .
|
|
|
|
"HTTP/1.1 200 OK\r\n" .
|
|
|
|
"Server: nginx\r\n" .
|
|
|
|
"Date: Fri, 20 Sep 2013 01:53:08 GMT\r\n" .
|
|
|
|
"Content-Type: text/html; charset=utf-8\r\n" .
|
|
|
|
"Transfer-Encoding: chunked\r\n" .
|
|
|
|
"Connection: keep-alive\r\n" .
|
|
|
|
"X-Frame-Options: SAMEORIGIN\r\n" .
|
|
|
|
"Cache-Control: no-cache, max-age=0, must-revalidate, no-transform\r\n" .
|
|
|
|
"Vary: Accept-Encoding\r\n" .
|
2013-09-20 06:26:39 +02:00
|
|
|
"\r\n"
|
|
|
|
;
|
2013-09-20 04:20:35 +02:00
|
|
|
|
|
|
|
$headerFunction = new ReflectionMethod('RestfulService', 'extractResponse');
|
|
|
|
$headerFunction->setAccessible(true);
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
$response = $headerFunction->invoke(
|
|
|
|
new RestfulService(Director::absoluteBaseURL(),0),
|
|
|
|
$ch,
|
2013-09-20 06:26:39 +02:00
|
|
|
$rawHeaders,
|
|
|
|
''
|
2013-09-20 04:20:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$response->getHeaders(),
|
|
|
|
array(
|
|
|
|
'Server' => "nginx",
|
|
|
|
'Date' => "Fri, 20 Sep 2013 01:53:08 GMT",
|
|
|
|
'Content-Type' => "text/html; charset=utf-8",
|
|
|
|
'Transfer-Encoding' => "chunked",
|
|
|
|
'Connection' => "keep-alive",
|
|
|
|
'X-Frame-Options' => "SAMEORIGIN",
|
|
|
|
'Cache-Control' => "no-cache, max-age=0, must-revalidate, no-transform",
|
|
|
|
'Vary' => "Accept-Encoding"
|
|
|
|
),
|
|
|
|
'Only last header is extracted and parsed.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testExtractResponseNoHead() {
|
|
|
|
$headerFunction = new ReflectionMethod('RestfulService', 'extractResponse');
|
|
|
|
$headerFunction->setAccessible(true);
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
$response = $headerFunction->invoke(
|
|
|
|
new RestfulService(Director::absoluteBaseURL(),0),
|
|
|
|
$ch,
|
2013-09-20 06:26:39 +02:00
|
|
|
'',
|
|
|
|
''
|
2013-09-20 04:20:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($response->getHeaders(), array(), 'Headers are correctly extracted.');
|
|
|
|
}
|
2008-10-21 05:45:44 +02:00
|
|
|
}
|
|
|
|
|
2011-02-13 23:14:51 +01:00
|
|
|
class RestfulServiceTest_Controller extends Controller implements TestOnly {
|
2012-06-29 10:45:57 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2012-06-29 10:45:57 +02:00
|
|
|
'index',
|
|
|
|
'httpErrorWithoutCache',
|
|
|
|
'httpErrorWithCache'
|
|
|
|
);
|
|
|
|
|
2010-10-19 03:26:09 +02:00
|
|
|
public function init() {
|
|
|
|
$this->basicAuthEnabled = false;
|
2012-06-29 10:45:57 +02:00
|
|
|
|
2010-10-19 03:26:09 +02:00
|
|
|
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');
|
2012-06-29 10:45:57 +02:00
|
|
|
|
2010-10-19 02:36:48 +02:00
|
|
|
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
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-06-24 05:05:33 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class RestfulServiceTest_MockRestfulService extends RestfulService {
|
|
|
|
|
|
|
|
public $session = null;
|
|
|
|
|
2011-10-29 01:07:58 +02:00
|
|
|
public function request($subURL = '', $method = "GET", $data = null, $headers = null, $curlOptions = array()) {
|
2009-06-24 05:05:33 +02:00
|
|
|
|
|
|
|
if(!$this->session) {
|
2014-06-19 00:38:26 +02:00
|
|
|
$this->session = Injector::inst()->create('Session', array());
|
2009-06-24 05:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
2011-03-23 10:05:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A mock service that returns a 400 error for requests.
|
|
|
|
*/
|
|
|
|
class RestfulServiceTest_MockErrorService extends RestfulService {
|
|
|
|
|
2011-10-29 01:07:58 +02:00
|
|
|
public function curlRequest($url, $method, $data = null, $headers = null, $curlOptions = array()) {
|
2011-03-23 10:05:29 +01:00
|
|
|
return new RestfulService_Response('<error>HTTP Error</error>', 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|