Browse Source

Removing legacy output buffer handling in Response::file().
This made it possible to correctly test file sending in IntegrationTestCase.

Fixes #7974

Jose Lorenzo Rodriguez 10 years ago
parent
commit
0c232eb82d

+ 4 - 4
src/Network/Response.php

@@ -1453,7 +1453,6 @@ class Response
             $this->header('Content-Length', $fileSize);
             $this->header('Content-Length', $fileSize);
         }
         }
 
 
-        $this->_clearBuffer();
         $this->_file = $file;
         $this->_file = $file;
     }
     }
 
 
@@ -1520,6 +1519,8 @@ class Response
     protected function _sendFile($file, $range)
     protected function _sendFile($file, $range)
     {
     {
         $compress = $this->outputCompressed();
         $compress = $this->outputCompressed();
+        ob_implicit_flush(true);
+
         $file->open('rb');
         $file->open('rb');
 
 
         $end = $start = false;
         $end = $start = false;
@@ -1546,9 +1547,6 @@ class Response
                 $bufferSize = $end - $offset + 1;
                 $bufferSize = $end - $offset + 1;
             }
             }
             echo fread($file->handle, $bufferSize);
             echo fread($file->handle, $bufferSize);
-            if (!$compress) {
-                $this->_flushBuffer();
-            }
         }
         }
         $file->close();
         $file->close();
         return true;
         return true;
@@ -1568,6 +1566,7 @@ class Response
      * Clears the contents of the topmost output buffer and discards them
      * Clears the contents of the topmost output buffer and discards them
      *
      *
      * @return bool
      * @return bool
+     * @deprecated 3.2.4 This function is not needed anymore
      */
      */
     protected function _clearBuffer()
     protected function _clearBuffer()
     {
     {
@@ -1580,6 +1579,7 @@ class Response
      * Flushes the contents of the output buffer
      * Flushes the contents of the output buffer
      *
      *
      * @return void
      * @return void
+     * @deprecated 3.2.4 This function is not needed anymore
      */
      */
     protected function _flushBuffer()
     protected function _flushBuffer()
     {
     {

+ 22 - 1
src/TestSuite/IntegrationTestCase.php

@@ -27,6 +27,7 @@ use Cake\Utility\Text;
 use Cake\View\Helper\SecureFieldTokenTrait;
 use Cake\View\Helper\SecureFieldTokenTrait;
 use Exception;
 use Exception;
 use PHPUnit_Exception;
 use PHPUnit_Exception;
+use PHPUnit_Framework_Constraint_IsEqual;
 
 
 /**
 /**
  * A test case class intended to make integration tests of
  * A test case class intended to make integration tests of
@@ -878,11 +879,31 @@ abstract class IntegrationTestCase extends TestCase
     public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
     public function assertCookieEncrypted($expected, $name, $encrypt = 'aes', $key = null, $message = '')
     {
     {
         if (empty($this->_response)) {
         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);
         $result = $this->_response->cookie($name);
         $this->_cookieEncriptionKey = $key;
         $this->_cookieEncriptionKey = $key;
         $result['value'] = $this->_decrypt($result['value'], $encrypt);
         $result['value'] = $this->_decrypt($result['value'], $encrypt);
         $this->assertEquals($expected, $result['value'], 'Cookie data differs. ' . $message);
         $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);
+    }
 }
 }

+ 37 - 1
tests/TestCase/TestSuite/IntegrationTestCaseTest.php

@@ -446,7 +446,7 @@ class IntegrationTestCaseTest extends IntegrationTestCase
 
 
         $this->assertHeader('Etag', 'abc123');
         $this->assertHeader('Etag', 'abc123');
     }
     }
-    
+
     /**
     /**
      * Test the header contains assertion.
      * Test the header contains assertion.
      *
      *
@@ -537,4 +537,40 @@ class IntegrationTestCaseTest extends IntegrationTestCase
     {
     {
         $this->assertNotSame($prevEventManager, EventManager::instance());
         $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 - 0
tests/test_app/TestApp/Controller/PostsController.php

@@ -81,4 +81,10 @@ class PostsController extends AppController
         $this->response->body('Request was accepted');
         $this->response->body('Request was accepted');
         return $this->response;
         return $this->response;
     }
     }
+
+    public function file()
+    {
+        $this->response->file(__FILE__);
+        return $this->response;
+    }
 }
 }