4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\RequestMatcher;
16 use Symfony\Component\HttpFoundation\Request;
18 class RequestMatcherTest extends TestCase
21 * @dataProvider getMethodData
23 public function testMethod($requestMethod, $matcherMethod, $isMatch)
25 $matcher = new RequestMatcher();
26 $matcher->matchMethod($matcherMethod);
27 $request = Request::create('', $requestMethod);
28 $this->assertSame($isMatch, $matcher->matches($request));
30 $matcher = new RequestMatcher(null, null, $matcherMethod);
31 $request = Request::create('', $requestMethod);
32 $this->assertSame($isMatch, $matcher->matches($request));
35 public function getMethodData()
38 array('get', 'get', true),
39 array('get', array('get', 'post'), true),
40 array('get', 'post', false),
41 array('get', 'GET', true),
42 array('get', array('GET', 'POST'), true),
43 array('get', 'POST', false),
47 public function testScheme()
49 $httpRequest = $request = $request = Request::create('');
50 $httpsRequest = $request = $request = Request::create('', 'get', array(), array(), array(), array('HTTPS' => 'on'));
52 $matcher = new RequestMatcher();
53 $matcher->matchScheme('https');
54 $this->assertFalse($matcher->matches($httpRequest));
55 $this->assertTrue($matcher->matches($httpsRequest));
57 $matcher->matchScheme('http');
58 $this->assertFalse($matcher->matches($httpsRequest));
59 $this->assertTrue($matcher->matches($httpRequest));
61 $matcher = new RequestMatcher();
62 $this->assertTrue($matcher->matches($httpsRequest));
63 $this->assertTrue($matcher->matches($httpRequest));
67 * @dataProvider getHostData
69 public function testHost($pattern, $isMatch)
71 $matcher = new RequestMatcher();
72 $request = Request::create('', 'get', array(), array(), array(), array('HTTP_HOST' => 'foo.example.com'));
74 $matcher->matchHost($pattern);
75 $this->assertSame($isMatch, $matcher->matches($request));
77 $matcher = new RequestMatcher(null, $pattern);
78 $this->assertSame($isMatch, $matcher->matches($request));
81 public function getHostData()
84 array('.*\.example\.com', true),
85 array('\.example\.com$', true),
86 array('^.*\.example\.com$', true),
87 array('.*\.sensio\.com', false),
88 array('.*\.example\.COM', true),
89 array('\.example\.COM$', true),
90 array('^.*\.example\.COM$', true),
91 array('.*\.sensio\.COM', false),
95 public function testPath()
97 $matcher = new RequestMatcher();
99 $request = Request::create('/admin/foo');
101 $matcher->matchPath('/admin/.*');
102 $this->assertTrue($matcher->matches($request));
104 $matcher->matchPath('/admin');
105 $this->assertTrue($matcher->matches($request));
107 $matcher->matchPath('^/admin/.*$');
108 $this->assertTrue($matcher->matches($request));
110 $matcher->matchMethod('/blog/.*');
111 $this->assertFalse($matcher->matches($request));
114 public function testPathWithLocaleIsNotSupported()
116 $matcher = new RequestMatcher();
117 $request = Request::create('/en/login');
118 $request->setLocale('en');
120 $matcher->matchPath('^/{_locale}/login$');
121 $this->assertFalse($matcher->matches($request));
124 public function testPathWithEncodedCharacters()
126 $matcher = new RequestMatcher();
127 $request = Request::create('/admin/fo%20o');
128 $matcher->matchPath('^/admin/fo o*$');
129 $this->assertTrue($matcher->matches($request));
132 public function testAttributes()
134 $matcher = new RequestMatcher();
136 $request = Request::create('/admin/foo');
137 $request->attributes->set('foo', 'foo_bar');
139 $matcher->matchAttribute('foo', 'foo_.*');
140 $this->assertTrue($matcher->matches($request));
142 $matcher->matchAttribute('foo', 'foo');
143 $this->assertTrue($matcher->matches($request));
145 $matcher->matchAttribute('foo', '^foo_bar$');
146 $this->assertTrue($matcher->matches($request));
148 $matcher->matchAttribute('foo', 'babar');
149 $this->assertFalse($matcher->matches($request));