mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
parent
d01083cfce
commit
7617f08ad3
@ -131,7 +131,8 @@ class RestfulService extends ViewableData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function constructURL(){
|
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
|
* Returns a full request url
|
||||||
* @param string
|
* @param string
|
||||||
*/
|
*/
|
||||||
public function getAbsoluteRequestURL($subURL) {
|
public function getAbsoluteRequestURL($subURL = '') {
|
||||||
$url = $this->baseURL . $subURL; // Url for the request
|
$url = Controller::join_links($this->baseURL, $subURL, '?' . $this->queryString);
|
||||||
if($this->queryString) {
|
|
||||||
if(strpos($url, '?') !== false) {
|
|
||||||
$url .= '&' . $this->queryString;
|
|
||||||
} else {
|
|
||||||
$url .= '?' . $this->queryString;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return str_replace(' ', '%20', $url); // Encode spaces
|
return str_replace(' ', '%20', $url); // Encode spaces
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,67 @@ class RestfulServiceTest extends SapphireTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
public function testSpecialCharacters() {
|
||||||
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
$service = new RestfulServiceTest_MockRestfulService(Director::absoluteBaseURL());
|
||||||
$url = 'RestfulServiceTest_Controller/';
|
$url = 'RestfulServiceTest_Controller/';
|
||||||
|
Loading…
Reference in New Issue
Block a user