8000 [HttpKernel] render_esi should behave in dev as it does in prod by bpaulin · Pull Request #15056 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] render_esi should behave in dev as it does in prod #15056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function __construct(SurrogateInterface $surrogate = null, FragmentRender
public function render($uri, Request $request, array $options = array())
{
if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
$options['is_fallback'] = true;

return $this->inlineStrategy->render($uri, $request, $options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function render($uri, Request $request, array $options = array())
// want that as we want to preserve objects (so we manually set Request attributes
// below instead)
$attributes = $reference->attributes;
if (isset($options['is_fallback']) && $options['is_fallback']) {
$this->checkNonScalar($attributes);
unset($options['is_fallback']);
}
$reference->attributes = array();

// The request format and locale might have been overridden by the user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function generateFragmentUri(ControllerReference $reference, Request $
return $request->getBaseUrl().$path;
}

private function checkNonScalar($values)
protected function checkNonScalar($values)
{
foreach ($values as $key => $value) {
if (is_array($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public function test()

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->once())->method('get')->will($this->returnValue($renderer));

$handler = new LazyLoadingFragmentHandler($container, false, $requestStack);
$handler->addRendererService('foo', &# 8000 39;foo');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class EsiFragmentRendererTest extends \PHPUnit_Framework_TestCase
{
public function testRenderFallbackToInlineStrategyIfNoRequest()
{
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true, array('is_fallback' => true)));
$strategy->render('/', Request::create('/'));
}

public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
{
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
$strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true, array('is_fallback' => true)));
$strategy->render('/', Request::create('/'));
}

Expand Down Expand Up @@ -90,12 +90,17 @@ public function testRenderAltControllerReferenceWithoutSignerThrowsException()
$strategy->render('/', $request, array('alt' => new ControllerReference('alt_contr 8000 oller')));
}

private function getInlineStrategy($called = false)
private function getInlineStrategy($called = false, $options = false)
{
$inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();

if ($called) {
$inline->expects($this->once())->method('render');
if ($options) {
$inline->expects($this->once())->method('render')->with($this->anything(), $this->anything(), $options);
}
else {
$inline->expects($this->once())->method('render');
}
}

return $inline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,44 @@ public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheController(
$this->assertEquals('bar', $response->getContent());
}

/**
* @expectedException \LogicException
*/
public function testRenderWithObjectsAsAttributesWhenIsFallBack()
{
$object = new \stdClass();

$subRequest = Request::create('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dmain_controller');
$subRequest->attributes->replace(array('object' => $object, '_format' => 'html', '_controller' => 'main_controller', '_locale' => 'en'));
$subRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
$subRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');

$strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($subRequest));

$strategy->render(new ControllerReference('main_controller', array('object' => $object), array()), Request::create('/'), array('is_fallback' => true));
}

/**
* @expectedException \LogicException
*/
public function testRenderWithObjectsAsAttributesPassedAsObjectsInTheControllerWhenIsFallBack()
{
$resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver', array('getController'));
$resolver
->expects($this->never())
->method('getController')
->will($this->returnValue(function (\stdClass $object, Bar $object1) {
return new Response($object1->getBar());
}))
;

$kernel = new HttpKernel(new EventDispatcher(), $resolver);
$renderer = new InlineFragmentRenderer($kernel);

$response = $renderer->render(new ControllerReference('main_controller', array('object' => new \stdClass(), 'object1' => new Bar()), array()), Request::create('/'), array('is_fallback' => true));
$this->assertEquals('bar', $response->getContent());
}

public function testRenderWithTrustedHeaderDisabled()
{
$trustedHeaderName = Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP);
Expand Down
0