Commit f14774df by Qiang Xue

Fixes issue #276: Enable Response::sendFile() to operate on resources

parent 9806563f
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
namespace yii\base; namespace yii\base;
use yii\web\Response;
/** /**
* HttpException represents an exception caused by an improper request of the end-user. * HttpException represents an exception caused by an improper request of the end-user.
* *
...@@ -44,8 +42,9 @@ class HttpException extends UserException ...@@ -44,8 +42,9 @@ class HttpException extends UserException
*/ */
public function getName() public function getName()
{ {
if (isset(Response::$httpStatuses[$this->statusCode])) { // use absolute namespaced class here because PHP will generate a mysterious error otherwise
return Response::$httpStatuses[$this->statusCode]; if (isset(\yii\web\Response::$httpStatuses[$this->statusCode])) {
return \yii\web\Response::$httpStatuses[$this->statusCode];
} else { } else {
return 'Error'; return 'Error';
} }
......
...@@ -104,6 +104,22 @@ class HeaderCollection extends Object implements \IteratorAggregate, \ArrayAcces ...@@ -104,6 +104,22 @@ class HeaderCollection extends Object implements \IteratorAggregate, \ArrayAcces
} }
/** /**
* Adds a new header only if it does not exist yet.
* If there is already a header with the same name, the new one will be ignored.
* @param string $name the name of the header
* @param string $value the value of the header
* @return HeaderCollection the collection object itself
*/
public function addDefault($name, $value)
{
$name = strtolower($name);
if (empty($this->_headers[$name])) {
$this->_headers[$name][] = $value;
}
return $this;
}
/**
* Returns a value indicating whether the named header exists. * Returns a value indicating whether the named header exists.
* @param string $name the name of the header * @param string $name the name of the header
* @return boolean whether the named header exists * @return boolean whether the named header exists
......
12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?
\ No newline at end of file
...@@ -4,10 +4,20 @@ namespace yiiunit\framework\web; ...@@ -4,10 +4,20 @@ namespace yiiunit\framework\web;
use Yii; use Yii;
use yii\helpers\StringHelper; use yii\helpers\StringHelper;
use yii\web\Response;
class Response extends \yii\web\Response
{
public function send()
{
// does nothing to allow testing
}
}
class ResponseTest extends \yiiunit\TestCase class ResponseTest extends \yiiunit\TestCase
{ {
/**
* @var Response
*/
public $response; public $response;
protected function setUp() protected function setUp()
...@@ -31,17 +41,20 @@ class ResponseTest extends \yiiunit\TestCase ...@@ -31,17 +41,20 @@ class ResponseTest extends \yiiunit\TestCase
/** /**
* @dataProvider rightRanges * @dataProvider rightRanges
*/ */
public function testSendFileRanges($rangeHeader, $expectedHeader, $length, $expectedFile) public function testSendFileRanges($rangeHeader, $expectedHeader, $length, $expectedContent)
{ {
$content = $this->generateTestFileContent(); $dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
$fullContent = file_get_contents($dataFile);
$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader; $_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
$this->response->sendFile('testFile.txt', $content, null, false); ob_start();
$this->response->sendFile($dataFile);
$content = ob_get_clean();
$this->assertEquals($expectedFile, $this->response->content); $this->assertEquals($expectedContent, $content);
$this->assertEquals(206, $this->response->statusCode); $this->assertEquals(206, $this->response->statusCode);
$headers = $this->response->headers; $headers = $this->response->headers;
$this->assertEquals("bytes", $headers->get('Accept-Ranges')); $this->assertEquals("bytes", $headers->get('Accept-Ranges'));
$this->assertEquals("bytes " . $expectedHeader . '/' . StringHelper::strlen($content), $headers->get('Content-Range')); $this->assertEquals("bytes " . $expectedHeader . '/' . StringHelper::strlen($fullContent), $headers->get('Content-Range'));
$this->assertEquals('text/plain', $headers->get('Content-Type')); $this->assertEquals('text/plain', $headers->get('Content-Type'));
$this->assertEquals("$length", $headers->get('Content-Length')); $this->assertEquals("$length", $headers->get('Content-Length'));
} }
...@@ -63,11 +76,11 @@ class ResponseTest extends \yiiunit\TestCase ...@@ -63,11 +76,11 @@ class ResponseTest extends \yiiunit\TestCase
*/ */
public function testSendFileWrongRanges($rangeHeader) public function testSendFileWrongRanges($rangeHeader)
{ {
$this->setExpectedException('yii\base\HttpException', 'Requested Range Not Satisfiable'); $this->setExpectedException('yii\base\HttpException');
$content = $this->generateTestFileContent(); $dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader; $_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
$this->response->sendFile('testFile.txt', $content, null, false); $this->response->sendFile($dataFile);
} }
protected function generateTestFileContent() protected function generateTestFileContent()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment