Skip to content

Commit

Permalink
[HttpFoundation] removed the hardcoded ^ and $ from the RequestMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 23, 2011
1 parent 76cf89b commit a6e6cbb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpFoundation/RequestMatcher.php
Expand Up @@ -100,16 +100,16 @@ public function matches(Request $request)
}

foreach ($this->attributes as $key => $pattern) {
if (!preg_match('#^'.$pattern.'$#', $request->attributes->get($key))) {
if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
return false;
}
}

if (null !== $this->path && !preg_match('#^'.$this->path.'$#', $request->getPathInfo())) {
if (null !== $this->path && !preg_match('#'.str_replace('#', '\\#', $this->path).'#', $request->getPathInfo())) {
return false;
}

if (null !== $this->host && !preg_match('#^'.$this->host.'$#', $request->getHost())) {
if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#', $request->getHost())) {
return false;
}

Expand Down
Expand Up @@ -50,8 +50,15 @@ public function testHost()
{
$matcher = new RequestMatcher();

$matcher->matchHost('.*\.example\.com');
$request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));

$matcher->matchHost('.*\.example\.com');
$this->assertTrue($matcher->matches($request));

$matcher->matchHost('\.example\.com$');
$this->assertTrue($matcher->matches($request));

$matcher->matchHost('^.*\.example\.com$');
$this->assertTrue($matcher->matches($request));

$matcher->matchMethod('.*\.sensio\.com');
Expand All @@ -62,11 +69,39 @@ public function testPath()
{
$matcher = new RequestMatcher();

$matcher->matchPath('/admin/.*');
$request = Request::create('/admin/foo');

$matcher->matchPath('/admin/.*');
$this->assertTrue($matcher->matches($request));

$matcher->matchPath('/admin');
$this->assertTrue($matcher->matches($request));

$matcher->matchPath('^/admin/.*$');
$this->assertTrue($matcher->matches($request));

$matcher->matchMethod('/blog/.*');
$this->assertFalse($matcher->matches($request));
}

public function testAttributes()
{
$matcher = new RequestMatcher();

$request = Request::create('/admin/foo');
$request->attributes->set('foo', 'foo_bar');

$matcher->matchAttribute('foo', 'foo_.*');
$this->assertTrue($matcher->matches($request));

$matcher->matchAttribute('foo', 'foo');
$this->assertTrue($matcher->matches($request));

$matcher->matchAttribute('foo', '^foo_bar$');
$this->assertTrue($matcher->matches($request));

$matcher->matchAttribute('foo', 'babar');
$this->assertFalse($matcher->matches($request));
}
}

0 comments on commit a6e6cbb

Please sign in to comment.