Skip to content
Permalink
Browse files

Removing legacy output buffer handling in Response::file().

This made it possible to correctly test file sending in IntegrationTestCase.

Fixes #7974
  • Loading branch information...
lorenzo committed Mar 5, 2016
1 parent 88e1bb8 commit 0c232eb82d146cd0bd1511d4359307b0ae61d6d9
@@ -1453,7 +1453,6 @@ public function file($path, array $options = [])
$this->header('Content-Length', $fileSize);
}
$this->_clearBuffer();
$this->_file = $file;
}
@@ -1520,6 +1519,8 @@ protected function _fileRange($file, $httpRange)
protected function _sendFile($file, $range)
{
$compress = $this->outputCompressed();
ob_implicit_flush(true);
$file->open('rb');
$end = $start = false;
@@ -1546,9 +1547,6 @@ protected function _sendFile($file, $range)
$bufferSize = $end - $offset + 1;
}
echo fread($file->handle, $bufferSize);
if (!$compress) {
$this->_flushBuffer();
}
}
$file->close();
return true;
@@ -1568,6 +1566,7 @@ protected function _isActive()
* Clears the contents of the topmost output buffer and discards them
*
* @return bool
* @deprecated 3.2.4 This function is not needed anymore
*/
protected function _clearBuffer()
{
@@ -1580,6 +1579,7 @@ protected function _clearBuffer()
* Flushes the contents of the output buffer
*
* @return void
* @deprecated 3.2.4 This function is not needed anymore
*/
protected function _flushBuffer()
{
@@ -27,6 +27,7 @@
use Cake\View\Helper\SecureFieldTokenTrait;
use Exception;
use PHPUnit_Exception;
use PHPUnit_Framework_Constraint_IsEqual;
/**
* A test case class intended to make integration tests of
@@ -878,11 +879,31 @@ public function assertCookie($expected, $name, $message = '')
public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
{
if (empty($this->_response)) {
$this->fail('Not response set, cannot assert cookies.');
$this->fail('No response set, cannot assert cookies.');
}
$result = $this->_response->cookie($name);
$this->_cookieEncriptionKey = $key;
$result['value'] = $this->_decrypt($result['value'], $encrypt);
$this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
}
/**
* Asserts that a file with the fiven name was sent in the response
*
* @param string $expected The file name that should be sent in the response
* @param string $message The failure message that will be appended to the generated message.
* @return void
*/
public function assertFileResponse($expected, $message = '')
{
if ($this->_response === null) {
$this->fail('No response set, cannot assert file.');
}
$actual = isset($this->_response->getFile()->path) ? $this->_response->getFile()->path : null;
if ($actual === null) {
$this->fail('No file was sent in this response');
}
$this->assertEquals($expected, $actual, $message);
}
}
@@ -446,7 +446,7 @@ public function testAssertHeader()
$this->assertHeader('Etag', 'abc123');
}
/**
* Test the header contains assertion.
*
@@ -537,4 +537,40 @@ public function testEventManagerReset2($prevEventManager)
{
$this->assertNotSame($prevEventManager, EventManager::instance());
}
/**
* Test sending file in requests.
*
* @return void
*/
public function testSendFile()
{
$this->get('/posts/file');
$this->assertFileResponse(TEST_APP . 'TestApp' . DS . 'Controller' . DS . 'PostsController.php');
}
/**
* Test that assertFile requires a response
*
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessage No response set, cannot assert file
* @return void
*/
public function testAssertFileNoReponse()
{
$this->assertFileResponse('foo');
}
/**
* Test that assertFile requires a file
*
* @expectedException PHPUnit_Framework_AssertionFailedError
* @expectedExceptionMessage No file was sent in this response
* @return void
*/
public function testAssertFileNoFile()
{
$this->get('/posts/get');
$this->assertFileResponse('foo');
}
}
@@ -81,4 +81,10 @@ public function securePost()
$this->response->body('Request was accepted');
return $this->response;
}
public function file()
{
$this->response->file(__FILE__);
return $this->response;
}
}

0 comments on commit 0c232eb

Please sign in to comment.
You can’t perform that action at this time.