Skip to content

Commit 7e87eb1

Browse files
committed
fixed request format when forwarding a request
1 parent 1ad64ee commit 7e87eb1

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
13+
14+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
18+
19+
class ControllerTest extends TestCase
20+
{
21+
public function testForward()
22+
{
23+
$request = Request::create('/');
24+
$request->setLocale('fr');
25+
$request->setRequestFormat('xml');
26+
27+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
28+
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
29+
return new Response($request->getRequestFormat().'--'.$request->getLocale());
30+
}));
31+
32+
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
33+
$container->expects($this->at(0))->method('get')->will($this->returnValue($request));
34+
$container->expects($this->at(1))->method('get')->will($this->returnValue($kernel));
35+
36+
$controller = new Controller();
37+
$controller->setContainer($container);
38+
39+
$response = $controller->forward('a_controller');
40+
$this->assertEquals('xml--fr', $response->getContent());
41+
}
42+
}

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ public function duplicate(array $query = null, array $request = null, array $att
406406
$dup->method = null;
407407
$dup->format = null;
408408

409+
if (!$dup->get('_format')) {
410+
$dup->setRequestFormat($this->getRequestFormat());
411+
}
412+
409413
return $dup;
410414
}
411415

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function onKernelException(GetResponseForExceptionEvent $event)
5555
'_controller' => $this->controller,
5656
'exception' => FlattenException::create($exception),
5757
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
58-
'_format' => $request->getRequestFormat(),
5958
// keep for BC -- as $format can be an argument of the controller callable
6059
// see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
6160
// @deprecated in 2.4, to be removed in 3.0

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ public function provider()
111111
array($event, $event2)
112112
);
113113
}
114+
115+
public function testSubRequestFormat()
116+
{
117+
$listener = new ExceptionListener('foo', $this->getMock('Psr\Log\LoggerInterface'));
118+
119+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
120+
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
121+
return new Response($request->getRequestFormat());
122+
}));
123+
124+
$request = Request::create('/');
125+
$request->setRequestFormat('xml');
126+
127+
$event = new GetResponseForExceptionEvent($kernel, $request, 'foo', new \Exception('foo'));
128+
$listener->onKernelException($event);
129+
130+
$response = $event->getResponse();
131+
$this->assertEquals('xml', $response->getContent());
132+
}
114133
}
115134

116135
class TestLogger extends Logger implements DebugLoggerInterface

0 commit comments

Comments
 (0)