Make CakeResponse::file() accept ranges even when download option is false.

This commit is contained in:
Marek Władysz 2014-10-25 19:49:56 +02:00
parent 4b5783c5fe
commit 090e85a5a4
2 changed files with 156 additions and 19 deletions

View file

@ -1382,18 +1382,17 @@ class CakeResponse {
$name = $options['name'];
}
$this->download($name);
$this->header('Accept-Ranges', 'bytes');
$this->header('Content-Transfer-Encoding', 'binary');
}
$httpRange = env('HTTP_RANGE');
if (isset($httpRange)) {
$this->_fileRange($file, $httpRange);
} else {
$this->header('Content-Length', $fileSize);
}
$this->header('Accept-Ranges', 'bytes');
$httpRange = env('HTTP_RANGE');
if (isset($httpRange)) {
$this->_fileRange($file, $httpRange);
} else {
$this->header('Content-Length', $fileSize);
}
$this->_clearBuffer();
$this->_file = $file;
}

View file

@ -1199,6 +1199,10 @@ class CakeResponseTest extends CakeTestCase {
->will($this->returnArgument(0));
$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(2))
->method('header')
->with('Content-Length', 38);
@ -1249,11 +1253,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(3))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(4))
->method('header')
@ -1314,11 +1318,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(3))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(4))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(5))
->method('header')
@ -1378,11 +1382,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(3))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(4))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(5))
->method('header')
@ -1432,6 +1436,10 @@ class CakeResponseTest extends CakeTestCase {
->with('ini')
->will($this->returnValue(false));
$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->never())
->method('download');
@ -1584,11 +1592,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(3))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(4))
->method('header')
@ -1639,11 +1647,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(3))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(4))
->method('header')
@ -1694,11 +1702,11 @@ class CakeResponseTest extends CakeTestCase {
$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');
->with('Content-Transfer-Encoding', 'binary');
$response->expects($this->at(3))
->method('header')
->with('Content-Transfer-Encoding', 'binary');
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(4))
->method('header')
@ -1715,6 +1723,136 @@ class CakeResponseTest extends CakeTestCase {
$result = $response->send();
}
/**
* testFileRangeOffsetsNoDownload method
*
* @dataProvider rangeProvider
* @return void
*/
public function testFileRangeOffsetsNoDownload($range, $length, $offsetResponse) {
$_SERVER['HTTP_RANGE'] = $range;
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'_sendHeader',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));
$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(2))
->method('header')
->with(array(
'Content-Length' => $length,
'Content-Range' => $offsetResponse,
));
$response->expects($this->any())
->method('_isActive')
->will($this->returnValue(true));
$response->file(
CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css',
array('download' => false)
);
ob_start();
$result = $response->send();
ob_get_clean();
}
/**
* testFileRangeNoDownload method
*
* @return void
*/
public function testFileRangeNoDownload() {
$_SERVER['HTTP_RANGE'] = 'bytes=8-25';
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'_sendHeader',
'_setContentType',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));
$response->expects($this->exactly(1))
->method('type')
->with('css')
->will($this->returnArgument(0));
$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(2))
->method('header')
->with(array(
'Content-Length' => 18,
'Content-Range' => 'bytes 8-25/38',
));
$response->expects($this->once())->method('_clearBuffer');
$response->expects($this->any())
->method('_isActive')
->will($this->returnValue(true));
$response->file(
CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css',
array('download' => false)
);
ob_start();
$result = $response->send();
$output = ob_get_clean();
$this->assertEquals(206, $response->statusCode());
$this->assertEquals("is the test asset ", $output);
$this->assertTrue($result !== false);
}
/**
* testFileRangeInvalidNoDownload method
*
* @return void
*/
public function testFileRangeInvalidNoDownload() {
$_SERVER['HTTP_RANGE'] = 'bytes=30-2';
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'_sendHeader',
'_setContentType',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));
$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(2))
->method('header')
->with(array(
'Content-Range' => 'bytes 0-37/38',
));
$response->file(
CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css',
array('download' => false)
);
$this->assertEquals(416, $response->statusCode());
$result = $response->send();
}
/**
* Test the location method.
*