BUGFIX: HTTP::setGetVar() returns a relative URL if a relative URL is passed, to make behaviour closer to 2.3

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101392 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-03-21 21:07:22 +00:00
parent 5804a20a18
commit 5dfadd0e9c
2 changed files with 18 additions and 20 deletions

View File

@ -94,8 +94,12 @@ class HTTP {
public static function setGetVar($varname, $varvalue, $currentURL = null) {
$uri = $currentURL ? $currentURL : Director::makeRelative($_SERVER['REQUEST_URI']);
$isRelative = false;
// We need absolute URLs for parse_url()
if(Director::is_relative_url($uri)) $uri = Director::absoluteBaseURL() . $uri;
if(Director::is_relative_url($uri)) {
$uri = Director::absoluteBaseURL() . $uri;
$isRelative = true;
}
// try to parse uri
$parts = parse_url($uri);
@ -128,8 +132,9 @@ class HTTP {
? '#' . $parts['fragment']
: ''
);
return $newUri;
if($isRelative) return Director::makeRelative($newUri);
else return $newUri;
}
static function RAW_setGetVar($varname, $varvalue, $currentURL = null) {

View File

@ -53,9 +53,7 @@ class HTTPTest extends SapphireTest {
// TODO This should test the absolute URL, but we can't get it reliably
// with port and auth URI parts.
$expectedBasePath = Director::baseURL();
foreach(array($expectedPath, 'foo=bar') as $e) {
foreach(array(Director::makeRelative($expectedPath), 'foo=bar') as $e) {
$this->assertContains(
$e,
HTTP::setGetVar('foo', 'bar'),
@ -63,21 +61,16 @@ class HTTPTest extends SapphireTest {
);
}
foreach(array($expectedBasePath, '/relative/url?foo=bar') as $e) {
$this->assertContains(
$e,
HTTP::setGetVar('foo', 'bar', 'relative/url'),
'Relative URL without slash prefix returns URL with absolute base'
);
}
$this->assertEquals(
'relative/url?foo=bar',
HTTP::setGetVar('foo', 'bar', 'relative/url'),
'Relative URL without existing query params');
foreach(array($expectedBasePath, '/relative/url?baz=buz&foo=bar') as $e) {
$this->assertContains(
$e,
HTTP::setGetVar('foo', 'bar', '/relative/url?baz=buz'),
'Relative URL with existing query params, and new added key'
);
}
$this->assertEquals(
'relative/url?baz=buz&foo=bar',
HTTP::setGetVar('foo', 'bar', '/relative/url?baz=buz'),
'Relative URL with existing query params, and new added key'
);
$this->assertEquals(
'http://test.com/?foo=new&buz=baz',