[HttpFoundation][Security] forward locale and format to subrequests

This commit is contained in:
Nicolas Grekas 2018-09-23 16:32:19 +02:00
parent 57a34131c6
commit 41eb1914f6
4 changed files with 35 additions and 1 deletions

View File

@ -1367,7 +1367,7 @@ class Request
* * _format request parameter
* * $default
*
* @param string $default The default format
* @param string|null $default The default format
*
* @return string The request format
*/

View File

@ -122,6 +122,13 @@ class InlineFragmentRenderer extends RoutableFragmentRenderer
$subRequest->setSession($session);
}
if ($request->get('_format')) {
$subRequest->attributes->set('_format', $request->get('_format'));
}
if ($request->getDefaultLocale() !== $request->getLocale()) {
$subRequest->setLocale($request->getLocale());
}
return $subRequest;
}

View File

@ -175,6 +175,26 @@ class InlineFragmentRendererTest extends TestCase
$this->assertEquals('Foo', ob_get_clean());
}
public function testLocaleAndFormatAreIsKeptInSubrequest()
{
$expectedSubRequest = Request::create('/');
$expectedSubRequest->attributes->set('_format', 'foo');
$expectedSubRequest->setLocale('fr');
if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
$expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
$expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
}
$expectedSubRequest->headers->set('forwarded', array('for="127.0.0.1";host="localhost";proto=http'));
$expectedSubRequest->server->set('HTTP_FORWARDED', 'for="127.0.0.1";host="localhost";proto=http');
$strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($expectedSubRequest));
$request = Request::create('/');
$request->attributes->set('_format', 'foo');
$request->setLocale('fr');
$strategy->render('/', $request);
}
public function testESIHeaderIsKeptInSubrequest()
{
$expectedSubRequest = Request::create('/');

View File

@ -91,6 +91,13 @@ class HttpUtils
$newRequest->attributes->set(Security::LAST_USERNAME, $request->attributes->get(Security::LAST_USERNAME));
}
if ($request->get('_format')) {
$newRequest->attributes->set('_format', $request->get('_format'));
}
if ($request->getDefaultLocale() !== $request->getLocale()) {
$newRequest->setLocale($request->getLocale());
}
return $newRequest;
}