Use Controller::join_links() in RestfulService

At the moment, `RestfulService` duplicates functionality of
`Controller::join_links` (badly) and it means that one MUST use a base
URL with a trailing slash for the URL to be constructed properly

Plus tests for `RestfulService::getAbsoluteRequestURL()`

API Deprecating `RestfulService::constructURL()`

This function isn't used in core so we should remove it
This commit is contained in:
Daniel Hensby 2014-05-07 19:43:54 +01:00
parent d01083cfce
commit 7617f08ad3
2 changed files with 66 additions and 11 deletions

View File

@ -131,7 +131,8 @@ class RestfulService extends ViewableData {
}
protected function constructURL(){
return "$this->baseURL" . ($this->queryString ? "?$this->queryString" : "");
Deprecation::notice('3.2', 'constructURL is deprecated, please use `getAbsoluteRequestURL` instead');
return Controller::join_links($this->baseURL, '?' . $this->queryString);
}
/**
@ -377,15 +378,8 @@ class RestfulService extends ViewableData {
* Returns a full request url
* @param string
*/
public function getAbsoluteRequestURL($subURL) {
$url = $this->baseURL . $subURL; // Url for the request
if($this->queryString) {
if(strpos($url, '?') !== false) {
$url .= '&' . $this->queryString;
} else {
$url .= '?' . $this->queryString;
}
}
public function getAbsoluteRequestURL($subURL = '') {
$url = Controller::join_links($this->baseURL, $subURL, '?' . $this->queryString);
return str_replace(' ', '%20', $url); // Encode spaces
}

View File

@ -24,7 +24,68 @@ class RestfulServiceTest extends SapphireTest {
if ($this->member_unique_identifier_field) {
Member::config()->unique_identifier_field = $this->member_unique_identifier_field;
}
}
}
/**
* 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'));
}
public function testSpecialCharacters() {
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());