[Router] Fix TraceableUrlMatcher

This commit is contained in:
Dariusz Górecki 2013-02-07 17:16:00 +00:00
parent f8cf1eb1a2
commit 30b0c37b68
2 changed files with 17 additions and 3 deletions

View File

@ -70,6 +70,14 @@ class TraceableUrlMatcher extends UrlMatcher
continue;
}
// check host requirement
$hostMatches = array();
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
$this->addTrace(sprintf('Host "%s" does not match the required ("%s")', $route->getHost(), $this->context->getHost()), self::ROUTE_ALMOST_MATCHES, $name, $route);
return true;
}
// check HTTP method requirement
if ($req = $route->getRequirement('_method')) {
// HEAD and GET are equivalent as per RFC

View File

@ -24,21 +24,27 @@ class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
$coll->add('foo', new Route('/foo', array(), array('_method' => 'POST')));
$coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+')));
$coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+', '_method' => 'POST')));
$coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
$coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($coll, $context);
$traces = $matcher->getTraces('/babar');
$this->assertEquals(array(0, 0, 0), $this->getLevels($traces));
$this->assertEquals(array(0, 0, 0, 0, 0), $this->getLevels($traces));
$traces = $matcher->getTraces('/foo');
$this->assertEquals(array(1, 0, 0), $this->getLevels($traces));
$this->assertEquals(array(1, 0, 0, 2), $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/12');
$this->assertEquals(array(0, 2), $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/dd');
$this->assertEquals(array(0, 1, 1), $this->getLevels($traces));
$this->assertEquals(array(0, 1, 1, 0, 0), $this->getLevels($traces));
$traces = $matcher->getTraces('/foo1');
$this->assertEquals(array(0, 0, 0, 0, 2), $this->getLevels($traces));
$context->setMethod('POST');
$traces = $matcher->getTraces('/foo');