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\HttpKernel\Tests\EventListener;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17 use Symfony\Component\HttpKernel\EventListener\FragmentListener;
18 use Symfony\Component\HttpKernel\HttpKernelInterface;
19 use Symfony\Component\HttpKernel\UriSigner;
21 class FragmentListenerTest extends TestCase
23 public function testOnlyTriggeredOnFragmentRoute()
25 $request = Request::create('http://example.com/foo?_path=foo%3Dbar%26_controller%3Dfoo');
27 $listener = new FragmentListener(new UriSigner('foo'));
28 $event = $this->createGetResponseEvent($request);
30 $expected = $request->attributes->all();
32 $listener->onKernelRequest($event);
34 $this->assertEquals($expected, $request->attributes->all());
35 $this->assertTrue($request->query->has('_path'));
38 public function testOnlyTriggeredIfControllerWasNotDefinedYet()
40 $request = Request::create('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo');
41 $request->attributes->set('_controller', 'bar');
43 $listener = new FragmentListener(new UriSigner('foo'));
44 $event = $this->createGetResponseEvent($request, HttpKernelInterface::SUB_REQUEST);
46 $expected = $request->attributes->all();
48 $listener->onKernelRequest($event);
50 $this->assertEquals($expected, $request->attributes->all());
54 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
56 public function testAccessDeniedWithNonSafeMethods()
58 $request = Request::create('http://example.com/_fragment', 'POST');
60 $listener = new FragmentListener(new UriSigner('foo'));
61 $event = $this->createGetResponseEvent($request);
63 $listener->onKernelRequest($event);
67 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
69 public function testAccessDeniedWithWrongSignature()
71 $request = Request::create('http://example.com/_fragment', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
73 $listener = new FragmentListener(new UriSigner('foo'));
74 $event = $this->createGetResponseEvent($request);
76 $listener->onKernelRequest($event);
79 public function testWithSignature()
81 $signer = new UriSigner('foo');
82 $request = Request::create($signer->sign('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
84 $listener = new FragmentListener($signer);
85 $event = $this->createGetResponseEvent($request);
87 $listener->onKernelRequest($event);
89 $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params'));
90 $this->assertFalse($request->query->has('_path'));
93 public function testRemovesPathWithControllerDefined()
95 $request = Request::create('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo');
97 $listener = new FragmentListener(new UriSigner('foo'));
98 $event = $this->createGetResponseEvent($request, HttpKernelInterface::SUB_REQUEST);
100 $listener->onKernelRequest($event);
102 $this->assertFalse($request->query->has('_path'));
105 public function testRemovesPathWithControllerNotDefined()
107 $signer = new UriSigner('foo');
108 $request = Request::create($signer->sign('http://example.com/_fragment?_path=foo%3Dbar'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
110 $listener = new FragmentListener($signer);
111 $event = $this->createGetResponseEvent($request);
113 $listener->onKernelRequest($event);
115 $this->assertFalse($request->query->has('_path'));
118 private function createGetResponseEvent(Request $request, $requestType = HttpKernelInterface::MASTER_REQUEST)
120 return new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, $requestType);