Skip to content

Commit

Permalink
Make CakeResponse::file() accept ranges even when download option is …
Browse files Browse the repository at this point in the history
…false.
  • Loading branch information
Marek Władysz committed Oct 25, 2014
1 parent 4b5783c commit 090e85a
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 19 deletions.
13 changes: 6 additions & 7 deletions lib/Cake/Network/CakeResponse.php
Expand Up @@ -1382,18 +1382,17 @@ public function file($path, $options = array()) {
$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;
}
Expand Down
162 changes: 150 additions & 12 deletions lib/Cake/Test/Case/Network/CakeResponseTest.php
Expand Up @@ -1199,6 +1199,10 @@ public function testFile() {
->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);

Expand Down Expand Up @@ -1249,11 +1253,11 @@ public function testFileWithUnknownFileTypeGeneric() {

$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')
Expand Down Expand Up @@ -1314,11 +1318,11 @@ public function testFileWithUnknownFileTypeOpera() {

$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')
Expand Down Expand Up @@ -1378,11 +1382,11 @@ public function testFileWithUnknownFileTypeIE() {

$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')
Expand Down Expand Up @@ -1432,6 +1436,10 @@ public function testFileWithUnknownFileNoDownload() {
->with('ini')
->will($this->returnValue(false));

$response->expects($this->at(1))
->method('header')
->with('Accept-Ranges', 'bytes');

$response->expects($this->never())
->method('download');

Expand Down Expand Up @@ -1584,11 +1592,11 @@ public function testFileRangeOffsets($range, $length, $offsetResponse) {

$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')
Expand Down Expand Up @@ -1639,11 +1647,11 @@ public function testFileRange() {

$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')
Expand Down Expand Up @@ -1694,11 +1702,11 @@ public function testFileRangeInvalid() {

$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')
Expand All @@ -1715,6 +1723,136 @@ public function testFileRangeInvalid() {
$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.
*
Expand Down

0 comments on commit 090e85a

Please sign in to comment.