Permalink
Browse files

[Routing] fixed validity check for hostname params in UrlGenerator

  • Loading branch information...
arnaud-lb committed Nov 5, 2012
1 parent a8ce621 commit 8366b8ab2789edce0d21df035c02a258cebdce7d
@@ -199,24 +199,31 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
}
if ($hostnameTokens) {
- $ghost = '';
+ $routeHost = '';
foreach ($hostnameTokens as $token) {
if ('variable' === $token[0]) {
- if (in_array($mergedParams[$token[3]], array(null, '', false), true)) {
- // check requirement
- 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 (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
+ $message = 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]) {
- $ghost = $token[1].$ghost;
+ $routeHost = $token[1].$routeHost;
}
}
- if ($ghost != $host) {
- $host = $ghost;
+ if ($routeHost != $host) {
+ $host = $routeHost;
$absolute = true;
}
}
@@ -398,6 +398,41 @@ public function testWithHostnameSameAsContextAndAbsolute()
$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)
{
$context = new RequestContext('/app.php');

0 comments on commit 8366b8a

Please sign in to comment.