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

This commit is contained in:
prophet777 2014-04-09 16:04:49 +02:00 committed by Fabien Potencier
parent 5e6c089b71
commit 787ae070ae
2 changed files with 45 additions and 3 deletions

View File

@ -45,24 +45,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.
*
@ -106,7 +124,7 @@ class RequestMatcher implements RequestMatcherInterface
/**
* 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 @@ class RequestMatcher implements RequestMatcherInterface
*/
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;
}

View File

@ -43,6 +43,25 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
);
}
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 @@ class RequestMatcherTest extends \PHPUnit_Framework_TestCase
array('.*\.example\.COM', true),
array('\.example\.COM$', true),
array('^.*\.example\.COM$', true),
array('.*\.sensio\.COM', false), );
array('.*\.sensio\.COM', false),
);
}
public function testPath()