Bläddra i källkod

[HttpFoundation] removed the hardcoded ^ and $ from the RequestMatcher

Fabien Potencier 14 år sedan
förälder
incheckning
a6e6cbbb27

+ 3 - 3
src/Symfony/Component/HttpFoundation/RequestMatcher.php

@@ -100,16 +100,16 @@ class RequestMatcher implements RequestMatcherInterface
         }
 
         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;
         }
 

+ 37 - 2
tests/Symfony/Tests/Component/HttpFoundation/RequestMatcherTest.php

@@ -50,8 +50,15 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
     {
         $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');
@@ -62,11 +69,39 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
     {
         $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));
+    }
 }
+