Permalink
Browse files

[HttpFoundation] Add a way to match a specific scheme in RequestMatcher

  • Loading branch information...
1 parent 5e6c089 commit 787ae070aeb92f54f3acaae5d314578a6848bed0 @ProPheT777 ProPheT777 committed with fabpot Apr 9, 2014
@@ -46,24 +46,42 @@ class RequestMatcher implements RequestMatcherInterface
private $attributes = array();
/**
+ * @var string[]
+ */
+ private $schemes = array();
+
+ /**
* @param string|null $path
* @param string|null $host
* @param string|string[]|null $methods
* @param string|string[]|null $ips
* @param array $attributes
+ * @param string|string[]|null $schemes
*/
- public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array())
+ public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = array(), $schemes = null)
{
$this->matchPath($path);
$this->matchHost($host);
$this->matchMethod($methods);
$this->matchIps($ips);
+ $this->matchScheme($schemes);
+
foreach ($attributes as $k => $v) {
$this->matchAttribute($k, $v);
}
}
/**
+ * Adds a check for the HTTP scheme.
+ *
+ * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
+ */
+ public function matchScheme($scheme)
+ {
+ $this->schemes = array_map('strtolower', (array) $scheme);
+ }
+
+ /**
* Adds a check for the URL host name.
*
* @param string $regexp A Regexp
@@ -106,7 +124,7 @@ public function matchIps($ips)
/**
* Adds a check for the HTTP method.
*
- * @param string|string[]|null $method An HTTP method or an array of HTTP methods
+ * @param string|string[] $method An HTTP method or an array of HTTP methods
*/
public function matchMethod($method)
{
@@ -131,6 +149,10 @@ public function matchAttribute($key, $regexp)
*/
public function matches(Request $request)
{
+ if ($this->schemes && !in_array($request->getScheme(), $this->schemes)) {
+ return false;
+ }
+
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
return false;
}
@@ -43,6 +43,25 @@ public function testMethodFixtures()
);
}
+ public function testScheme()
+ {
+ $httpRequest = $request = $request = Request::create('');
+ $httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
+
+ $matcher = new RequestMatcher();
+ $matcher->matchScheme('https');
+ $this->assertFalse($matcher->matches($httpRequest));
+ $this->assertTrue($matcher->matches($httpsRequest));
+
+ $matcher->matchScheme('http');
+ $this->assertFalse($matcher->matches($httpsRequest));
+ $this->assertTrue($matcher->matches($httpRequest));
+
+ $matcher = new RequestMatcher();
+ $this->assertTrue($matcher->matches($httpsRequest));
+ $this->assertTrue($matcher->matches($httpRequest));
+ }
+
/**
* @dataProvider testHostFixture
*/
@@ -68,7 +87,8 @@ public function testHostFixture()
array('.*\.example\.COM', true),
array('\.example\.COM$', true),
array('^.*\.example\.COM$', true),
- array('.*\.sensio\.COM', false), );
+ array('.*\.sensio\.COM', false),
+ );
}
public function testPath()

0 comments on commit 787ae07

Please sign in to comment.