[Routing] fixed validity check for hostname params in UrlGenerator

This commit is contained in:
Arnaud Le Blanc 2012-11-05 14:26:00 +01:00
parent a8ce6210b3
commit 8366b8ab27
2 changed files with 51 additions and 9 deletions

View File

@ -199,24 +199,31 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
} }
if ($hostnameTokens) { if ($hostnameTokens) {
$ghost = ''; $routeHost = '';
foreach ($hostnameTokens as $token) { foreach ($hostnameTokens as $token) {
if ('variable' === $token[0]) { if ('variable' === $token[0]) {
if (in_array($mergedParams[$token[3]], array(null, '', false), true)) { if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
// check requirement $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]);
if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]])); if ($this->strictRequirements) {
throw new InvalidParameterException($message);
} }
if ($this->logger) {
$this->logger->err($message);
}
return null;
} }
$ghost = $token[1].$mergedParams[$token[3]].$ghost; $routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
} elseif ('text' === $token[0]) { } elseif ('text' === $token[0]) {
$ghost = $token[1].$ghost; $routeHost = $token[1].$routeHost;
} }
} }
if ($ghost != $host) { if ($routeHost != $host) {
$host = $ghost; $host = $routeHost;
$absolute = true; $absolute = true;
} }
} }

View File

@ -398,6 +398,41 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true)); $this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true));
} }
/**
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterInHostname()
{
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
/**
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterInHostnameWhenParamHasADefaultValue()
{
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
/**
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterEqualsDefaultValueInHostname()
{
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
public function testUrlWithInvalidParameterInHostnameInNonStrictMode()
{
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', array('foo' => 'baz'), false));
}
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null) protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
{ {
$context = new RequestContext('/app.php'); $context = new RequestContext('/app.php');