Skip to content

Commit cf20af5

Browse files
committed
Fix buildWithBaseURLPath, See #581
1 parent 0c195d5 commit cf20af5

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/Saml2/Utils.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,11 @@ protected static function buildWithBaseURLPath($info)
722722
if (!empty($baseURLPath)) {
723723
$result = $baseURLPath;
724724
if (!empty($info)) {
725-
// Remove base path from the path info.
726-
$extractedInfo = str_replace($baseURLPath, '', $info);
725+
$extractedInfo = $info;
726+
if ($baseURLPath != '/') {
727+
// Remove base path from the path info.
728+
$extractedInfo = str_replace($baseURLPath, '', $info);
729+
}
727730
// Remove starting and ending slash.
728731
$extractedInfo = trim($extractedInfo, '/');
729732
if (!empty($extractedInfo)) {

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,48 @@ public function testSetBaseURL()
545545
$this->assertEquals($expectedUrl2, Utils::getSelfURL());
546546
}
547547

548+
/**
549+
* @covers OneLogin\Saml2\Utils::setBaseURL
550+
*/
551+
public function testSetBaseURL2()
552+
{
553+
$_SERVER['HTTP_HOST'] = 'sp.example.com';
554+
$_SERVER['HTTPS'] = 'https';
555+
$_SERVER['REQUEST_URI'] = null;
556+
$_SERVER['QUERY_STRING'] = null;
557+
$_SERVER['SCRIPT_NAME'] = '/';
558+
unset($_SERVER['PATH_INFO']);
559+
560+
Utils::setBaseURL('https://sp.example.com');
561+
$this->assertEquals("https://sp.example.com/", Utils::getSelfURLNoQuery());
562+
$this->assertEquals("https://sp.example.com/", Utils::getSelfRoutedURLNoQuery());
563+
$this->assertEquals("https://sp.example.com/", Utils::getSelfURL());
564+
$this->assertEquals('/', Utils::getBaseURLPath());
565+
566+
$_SERVER['REQUEST_URI'] = '/example1/path/route.php?x=test';
567+
$_SERVER['QUERY_STRING'] = '?x=test';
568+
$_SERVER['SCRIPT_NAME'] = '/example1/path/route.php';
569+
$this->assertEquals("https://sp.example.com/example1/path/route.php", Utils::getSelfURLNoQuery());
570+
$this->assertEquals("https://sp.example.com/example1/path/route.php", Utils::getSelfRoutedURLNoQuery());
571+
$this->assertEquals("https://sp.example.com/example1/path/route.php?x=test", Utils::getSelfURL());
572+
$this->assertEquals('/', Utils::getBaseURLPath());
573+
574+
Utils::setBaseURLPath('/example1/path/');
575+
$this->assertEquals("https://sp.example.com/example1/path/route.php", Utils::getSelfURLNoQuery());
576+
$this->assertEquals("https://sp.example.com/example1/path/route.php", Utils::getSelfRoutedURLNoQuery());
577+
$this->assertEquals("https://sp.example.com/example1/path/route.php?x=test", Utils::getSelfURL());
578+
$this->assertEquals('/example1/path/', Utils::getBaseURLPath());
579+
580+
$_SERVER['REQUEST_URI'] = '/example1/path/route/?x=test';
581+
$_SERVER['QUERY_STRING'] = '?x=test';
582+
$_SERVER['SCRIPT_NAME'] = '/example1/path/route';
583+
$this->assertEquals("https://sp.example.com/example1/path/route", Utils::getSelfURLNoQuery());
584+
$this->assertEquals("https://sp.example.com/example1/path/route", Utils::getSelfRoutedURLNoQuery());
585+
$this->assertEquals("https://sp.example.com/example1/path/route/?x=test", Utils::getSelfURL());
586+
$this->assertEquals('/example1/path/', Utils::getBaseURLPath());
587+
588+
}
589+
548590
/**
549591
* Tests the getSelfURLhost method of the Utils
550592
*

0 commit comments

Comments
 (0)