Skip to content

Commit

Permalink
Also allowing callables in Response::body() to return a string(ish)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo authored and ADmad committed Jul 7, 2015
1 parent c37930b commit 2cfde27
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Network/Response.php
Expand Up @@ -546,10 +546,10 @@ protected function _sendHeader($name, $value = null)
protected function _sendContent($content)
{
if (!is_string($content) && is_callable($content)) {
call_user_func($content);
} else {
echo $content;
$content = $content();
}

echo $content;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Network/ResponseTest.php
Expand Up @@ -331,6 +331,24 @@ public function testSendWithCallableBody()
$this->assertEquals('the response body', ob_get_clean());
}

/**
* Tests that the returned a string from a body callable is also sent
* as the response body
*
* @return void
*/
public function testSendWithCallableBodyWithReturn()
{
$response = $this->getMock('Cake\Network\Response', ['_sendHeader']);
$response->body(function () {
return 'the response body';
});

ob_start();
$response->send();
$this->assertEquals('the response body', ob_get_clean());
}

/**
* Tests the disableCache method
*
Expand Down

0 comments on commit 2cfde27

Please sign in to comment.