Skip to content

Commit

Permalink
[HttpFoundation] fixed ApacheRequest
Browse files Browse the repository at this point in the history
Pathinfo was incorrect when using mod_rewrite.
Added better test coverage.
  • Loading branch information
kriswallsmith committed Dec 21, 2011
1 parent 2af5cb5 commit 1b4aaa2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/ApacheRequest.php
Expand Up @@ -46,6 +46,6 @@ protected function prepareBaseUrl()
*/
protected function preparePathInfo()
{
return $this->server->get('PATH_INFO');
return $this->server->get('PATH_INFO') ?: substr($this->prepareRequestUri(), strlen($this->prepareBaseUrl())) ?: '/';
}
}
81 changes: 67 additions & 14 deletions tests/Symfony/Tests/Component/HttpFoundation/ApacheRequestTest.php
Expand Up @@ -15,25 +15,78 @@

class ApacheRequestTest extends \PHPUnit_Framework_TestCase
{
public function testGetBaseUrlDoesNotForceScriptName()
/**
* @dataProvider provideServerVars
*/
public function testUriMethods($server, $expectedRequestUri, $expectedBaseUrl, $expectedPathInfo)
{
$request = new ApacheRequest();
$request->server->replace(array(
'REQUEST_URI' => '/foo/bar',
'SCRIPT_NAME' => '/index.php',
));
$request->server->replace($server);

$this->assertEquals('', $request->getBaseUrl(), '->getBaseUrl() does not add the script name');
$this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
$this->assertEquals($expectedBaseUrl, $request->getBaseUrl(), '->getBaseUrl() is correct');
$this->assertEquals($expectedPathInfo, $request->getPathInfo(), '->getPathInfo() is correct');
}

public function testGetBaseUrlIncludesScriptName()
public function provideServerVars()
{
$request = new ApacheRequest();
$request->server->replace(array(
'REQUEST_URI' => '/index.php/foo/bar',
'SCRIPT_NAME' => '/index.php',
));

$this->assertEquals('/index.php', $request->getBaseUrl(), '->getBaseUrl() includes the script name');
return array(
array(
array(
'REQUEST_URI' => '/foo/app_dev.php/bar',
'SCRIPT_NAME' => '/foo/app_dev.php',
'PATH_INFO' => '/bar',
),
'/foo/app_dev.php/bar',
'/foo/app_dev.php',
'/bar'
),
array(
array(
'REQUEST_URI' => '/foo/bar',
'SCRIPT_NAME' => '/foo/app_dev.php',
),
'/foo/bar',
'/foo',
'/bar',
),
array(
array(
'REQUEST_URI' => '/app_dev.php/foo/bar',
'SCRIPT_NAME' => '/app_dev.php',
'PATH_INFO' => '/foo/bar',
),
'/app_dev.php/foo/bar',
'/app_dev.php',
'/foo/bar',
),
array(
array(
'REQUEST_URI' => '/foo/bar',
'SCRIPT_NAME' => '/app_dev.php',
),
'/foo/bar',
'',
'/foo/bar',
),
array(
array(
'REQUEST_URI' => '/app_dev.php',
'SCRIPT_NAME' => '/app_dev.php',
),
'/app_dev.php',
'/app_dev.php',
'/',
),
array(
array(
'REQUEST_URI' => '/',
'SCRIPT_NAME' => '/app_dev.php',
),
'/',
'',
'/',
),
);
}
}

0 comments on commit 1b4aaa2

Please sign in to comment.