Skip to content

Commit ba244ae

Browse files
committed
Fix buildWithBaseURLPath, See #581
1 parent 3b0fbb8 commit ba244ae

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
@@ -546,6 +546,48 @@ public function testSetBaseURL()
546546
$this->assertEquals($expectedUrl2, Utils::getSelfURL());
547547
}
548548

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

0 commit comments

Comments
 (0)