Skip to content

Commit

Permalink
Merge pull request #11087 from cakephp/issue-11084
Browse files Browse the repository at this point in the history
Fix response headers set in templates not be sent.
  • Loading branch information
markstory committed Aug 24, 2017
2 parents 91274dd + 3587231 commit e64e0a9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,8 @@ public function render($view = null, $layout = null)
}

$this->View = $this->createView();
$this->response->body($this->View->render($view, $layout));
$contents = $this->View->render($view, $layout);
$this->response = $this->View->response->withStringBody($contents);

return $this->response;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Http/ActionDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,16 @@ protected function _invoke(Controller $controller)
}

if (!$response && $controller->autoRender) {
$response = $controller->render();
} elseif (!$response) {
$response = $controller->response;
$controller->render();
}

$result = $controller->shutdownProcess();
if ($result instanceof Response) {
return $result;
}
if (!$response) {
$response = $controller->response;
}

return $response;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,25 @@ public function testRender()
$this->assertRegExp('/this is the test element/', (string)$result);
}

/**
* test view rendering changing response
*
* @return void
*/
public function testRenderViewChangesResponse()
{
$request = new ServerRequest('controller_posts/index');
$request->params['action'] = 'header';

$controller = new Controller($request, new Response());
$controller->viewBuilder()->templatePath('Posts');

$result = $controller->render('header');
$this->assertContains('header template', (string)$result);
$this->assertTrue($controller->response->hasHeader('X-view-template'));
$this->assertSame('yes', $controller->response->getHeaderLine('X-view-template'));
}

/**
* test that a component beforeRender can change the controller view class.
*
Expand Down
8 changes: 6 additions & 2 deletions tests/TestCase/Routing/DispatcherFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ public function testCreateDispatchWithFilters()
$response = $this->getMockBuilder('Cake\Http\Response')
->setMethods(['send'])
->getMock();

$response->expects($this->once())
->method('send')
->will($this->returnSelf());

DispatcherFactory::add('ControllerFactory');
DispatcherFactory::add('Append');

$dispatcher = DispatcherFactory::create();
$result = $dispatcher->dispatch($url, $response);
$this->assertNull($result);
$this->assertEquals('posts index appended content', $response->body());
$this->assertEquals('posts index appended content', $result->body());
}
}
4 changes: 3 additions & 1 deletion tests/TestCase/Routing/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ public function testDispatchBasic()
'pass' => ['extract'],
]
]);
$response = $this->getMockBuilder('Cake\Http\Response')->getMock();
$response = $this->getMockBuilder('Cake\Http\Response')
->setMethods(['send'])
->getMock();
$response->expects($this->once())
->method('send');

Expand Down
4 changes: 4 additions & 0 deletions tests/test_app/TestApp/Template/Posts/header.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
$this->response = $this->response->withHeader('X-view-template', 'yes');
?>
header template

0 comments on commit e64e0a9

Please sign in to comment.