[Routing] fix handling of two starting slashes in the pattern

It would be confused with a network path e.g. '//domain/path' when generating a path so should be prevented.
This commit is contained in:
Tobias Schultze 2012-10-06 01:20:12 +02:00
parent 8062031a0a
commit 90145d2b71
3 changed files with 13 additions and 7 deletions

View File

@ -95,13 +95,9 @@ class Route implements \Serializable
*/
public function setPattern($pattern)
{
$this->pattern = trim($pattern);
// a route must start with a slash
if ('' === $this->pattern || '/' !== $this->pattern[0]) {
$this->pattern = '/'.$this->pattern;
}
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
$this->pattern = '/' . ltrim(trim($pattern), '/');
$this->compiled = null;
return $this;

View File

@ -214,6 +214,14 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
$routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
$this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
}
public function testPathWithTwoStartingSlashes()
{
$routes = $this->getRoutes('test', new Route('//path-and-not-domain'));
// this must not generate '//path-and-not-domain' because that would be a network path
$this->assertSame('/path-and-not-domain', $this->getGenerator($routes, array('BaseUrl' => ''))->generate('test'));
}
public function testNoTrailingSlashForMultipleOptionalParameters()
{

View File

@ -34,6 +34,8 @@ class RouteTest extends \PHPUnit_Framework_TestCase
$route->setPattern('bar');
$this->assertEquals('/bar', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
$this->assertEquals($route, $route->setPattern(''), '->setPattern() implements a fluent interface');
$route->setPattern('//path');
$this->assertEquals('/path', $route->getPattern(), '->setPattern() does not allow two slahes "//" at the beginning of the pattern as it would be confused with a network path when generating the path from the route');
}
public function testOptions()