member_unique_identifier_field = Member::config()->unique_identifier_field;
Member::config()->unique_identifier_field = 'Email';
parent::setUp();
}
public function tearDown() {
parent::tearDown();
// set old Member::config()->unique_identifier_field value
if ($this->member_unique_identifier_field) {
Member::config()->unique_identifier_field = $this->member_unique_identifier_field;
}
}
public function testSpecialCharacters() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
$url = 'RestfulServiceTest_Controller/';
$params = array(
'test1a' => 4352655636.76543, // number test
'test1b' => '$&+,/:;=?@#%', // special char test. These should all get encoded
'test1c' => 'And now for a string test' // string test
);
$service->setQueryString($params);
$responseBody = $service->request($url)->getBody();
foreach ($params as $key => $value) {
$this->assertContains("$value", $responseBody);
$this->assertContains("$value", $responseBody);
}
}
public function testGetDataWithSetQueryString() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
$url = 'RestfulServiceTest_Controller/';
$params = array(
'test1a' => 'val1a',
'test1b' => 'val1b'
);
$service->setQueryString($params);
$responseBody = $service->request($url)->getBody();
foreach ($params as $key => $value) {
$this->assertContains("$value", $responseBody);
$this->assertContains("$value", $responseBody);
}
}
public 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("$value", $responseBody);
$this->assertContains("$value", $responseBody);
}
}
public function testPostData() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
$params = array(
'test1a' => 'val1a',
'test1b' => 'val1b'
);
$responseBody = $service->request('RestfulServiceTest_Controller/', 'POST', $params)->getBody();
foreach ($params as $key => $value) {
$this->assertContains("$value", $responseBody);
$this->assertContains("$value", $responseBody);
}
}
public function testPutData() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
$data = 'testPutData';
$responseBody = $service->request('RestfulServiceTest_Controller/', 'PUT', $data)->getBody();
$this->assertContains("
$data", $responseBody);
}
public 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("first run", $responseBody);
// Second run
$params = array(
'test1a' => 'second run',
);
$service->setQueryString($params);
$responseBody = $service->request($url)->getBody();
$this->assertContains("second run", $responseBody);
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function testIncorrectData() {
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
$test1 = $connection->request('RestfulServiceTest_Controller/invalid');
$test1->xpath("\\fail");
}
public function testHttpErrorWithoutCache() {
$connection = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL(), 0);
$response = $connection->request('RestfulServiceTest_Controller/httpErrorWithoutCache');
$this->assertEquals(400, $response->getStatusCode());
$this->assertFalse($response->getCachedBody());
$this->assertContains("HTTP Error", $response->getBody());
}
public function testHttpErrorWithCache() {
$subUrl = 'RestfulServiceTest_Controller/httpErrorWithCache';
$connection = new RestfulServiceTest_MockErrorService(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("HTTP Error", $response->getBody());
}
/**
* Simulate cached response file for testing error requests that are supposed to have cache files
*
* @todo Generate the cachepath without hardcoding the cache data
*/
private function createFakeCachedResponse($connection, $subUrl) {
$fullUrl = $connection->getAbsoluteRequestURL($subUrl);
//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)
)));
$cacheResponse = new RestfulService_Response("Cache response body");
$store = serialize($cacheResponse);
file_put_contents($cache_path, $store);
}
public function testHttpHeaderParseing() {
$headers = "content-type: text/html; charset=UTF-8\r\n".
"Server: Funky/1.0\r\n".
"X-BB-ExampleMANycaPS: test\r\n".
"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',
'X-BB-ExampleMANycaPS' => 'test',
'Set-Cookie' => array(
'foo=bar',
'baz=quux',
'bar=foo'
)
);
$headerFunction = new ReflectionMethod('RestfulService', 'parseRawHeaders');
$headerFunction->setAccessible(true);
$this->assertEquals(
$expected,
$headerFunction->invoke(
new RestfulService(Director::absoluteBaseURL(),0), $headers
)
);
}
public function testExtractResponseRedirectionAndProxy() {
// This is an example of real raw response for a request via a proxy that gets redirected.
$rawResponse =
"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" .
"\r\n" .
"