Skip to content

Commit

Permalink
Removing legacy output buffer handling in Response::file().
Browse files Browse the repository at this point in the history
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 0c232eb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Network/Response.php
Expand Up @@ -1453,7 +1453,6 @@ public function file($path, array $options = [])
$this->header('Content-Length', $fileSize);
}

$this->_clearBuffer();
$this->_file = $file;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()
{
Expand All @@ -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()
{
Expand Down
23 changes: 22 additions & 1 deletion src/TestSuite/IntegrationTestCase.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
38 changes: 37 additions & 1 deletion tests/TestCase/TestSuite/IntegrationTestCaseTest.php
Expand Up @@ -446,7 +446,7 @@ public function testAssertHeader()

$this->assertHeader('Etag', 'abc123');
}

/**
* Test the header contains assertion.
*
Expand Down Expand Up @@ -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');
}
}
6 changes: 6 additions & 0 deletions tests/test_app/TestApp/Controller/PostsController.php
Expand Up @@ -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.