Skip to content
Permalink
Browse files

Better support various ranges as described in RFC2616

Refs #3914
  • Loading branch information...
markstory committed Jul 18, 2013
1 parent 3f9e8e8 commit f725779a170444e05949401a9055132806729ae0
Showing with 79 additions and 0 deletions.
  1. +9 −0 lib/Cake/Network/CakeResponse.php
  2. +70 −0 lib/Cake/Test/Case/Network/CakeResponseTest.php
@@ -1302,6 +1302,15 @@ protected function _fileRange($file, $httpRange) {
$fileSize = $file->size();
$lastByte = $fileSize - 1;
if ($start === '') {
$start = $fileSize - $end;
$end = $lastByte;
}
if ($end === '') {
$end = $lastByte;
}
if ($start > $end || $end > $lastByte || $start > $lastByte) {
$this->statusCode(416);
$this->header(array(
@@ -1387,6 +1387,76 @@ public function testFileExtensionNotSet() {
$response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS . 'test_2.JPG');
}
/**
* A data provider for testing various ranges
*
* @return array
*/
public static function rangeProvider() {
return array(
// suffix-byte-range
array(
'bytes=-25', 25, 'bytes 13-37/38'
),
array(
'bytes=0-', 38, 'bytes 0-37/38'
),
array(
'bytes=10-', 28, 'bytes 10-37/38'
),
array(
'bytes=10-20', 11, 'bytes 10-20/38'
),
);
}
/**
* Test the various range offset types.
*
* @dataProvider rangeProvider
* @return void
*/
public function testFileRangeOffsets($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('Content-Disposition', 'attachment; filename="test_asset.css"');
$response->expects($this->at(2))
->method('header')
->with('Accept-Ranges', 'bytes');
$response->expects($this->at(3))
->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' => true)
);
ob_start();
$result = $response->send();
ob_get_clean();
}
/**
* Test fetching ranges from a file.
*

0 comments on commit f725779

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