Commit ef5afef7 by Qiang Xue

Fixed test break.

parent ec8de608
......@@ -118,7 +118,7 @@ class Response extends \yii\base\Response
511 => 'Network Authentication Required',
);
private $_statusCode = 200;
private $_statusCode;
/**
* @var HeaderCollection
*/
......@@ -199,6 +199,14 @@ class Response extends \yii\base\Response
$this->sendContent();
}
public function reset()
{
$this->_headers = null;
$this->_statusCode = null;
$this->statusText = null;
$this->content = null;
}
/**
* Sends the response headers to the client
*/
......@@ -207,7 +215,10 @@ class Response extends \yii\base\Response
if (headers_sent()) {
return;
}
header("HTTP/{$this->version} " . $this->getStatusCode() . " {$this->statusText}");
$statusCode = $this->getStatusCode();
if ($statusCode !== null) {
header("HTTP/{$this->version} $statusCode {$this->statusText}");
}
if ($this->_headers) {
$headers = $this->getHeaders();
foreach ($headers as $name => $values) {
......@@ -334,10 +345,10 @@ class Response extends \yii\base\Response
ob_start();
Yii::$app->end(0, false);
ob_end_clean();
echo $content;
$this->content = $content;
exit(0);
} else {
echo $content;
$this->content = $content;
}
}
......
<?php
namespace yii\web;
use yiiunit\framework\web\ResponseTest;
/**
* Mock PHP header function to check for sent headers
* @param string $string
* @param bool $replace
* @param int $httpResponseCode
*/
function header($string, $replace = true, $httpResponseCode = null) {
ResponseTest::$headers[] = $string;
// TODO implement replace
if ($httpResponseCode !== null) {
ResponseTest::$httpResponseCode = $httpResponseCode;
}
}
namespace yiiunit\framework\web;
use Yii;
use yii\helpers\StringHelper;
use yii\web\Response;
class ResponseTest extends \yiiunit\TestCase
{
public static $headers = array();
public static $httpResponseCode = 200;
public $response;
protected function setUp()
{
parent::setUp();
$this->mockApplication();
$this->reset();
}
protected function reset()
{
static::$headers = array();
static::$httpResponseCode = 200;
$this->response = new Response;
}
public function rightRanges()
......@@ -60,14 +35,15 @@ class ResponseTest extends \yiiunit\TestCase
{
$content = $this->generateTestFileContent();
$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
$sent = $this->runSendFile('testFile.txt', $content, null);
$this->assertEquals($expectedFile, $sent);
$this->assertTrue(in_array('HTTP/1.1 206 Partial Content', static::$headers));
$this->assertTrue(in_array('Accept-Ranges: bytes', static::$headers));
$this->assertArrayHasKey('Content-Range: bytes ' . $expectedHeader . '/' . StringHelper::strlen($content), array_flip(static::$headers));
$this->assertTrue(in_array('Content-Type: text/plain', static::$headers));
$this->assertTrue(in_array('Content-Length: ' . $length, static::$headers));
$this->response->sendFile('testFile.txt', $content, null, false);
$this->assertEquals($expectedFile, $this->response->content);
$this->assertEquals(206, $this->response->statusCode);
$headers = $this->response->headers;
$this->assertEquals("bytes", $headers->get('Accept-Ranges'));
$this->assertEquals("bytes " . $expectedHeader . '/' . StringHelper::strlen($content), $headers->get('Content-Range'));
$this->assertEquals('text/plain', $headers->get('Content-Type'));
$this->assertEquals("$length", $headers->get('Content-Length'));
}
public function wrongRanges()
......@@ -91,21 +67,11 @@ class ResponseTest extends \yiiunit\TestCase
$content = $this->generateTestFileContent();
$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
$this->runSendFile('testFile.txt', $content, null);
$this->response->sendFile('testFile.txt', $content, null, false);
}
protected function generateTestFileContent()
{
return '12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?';
}
protected function runSendFile($fileName, $content, $mimeType)
{
ob_start();
ob_implicit_flush(false);
$response = new Response();
$response->sendFile($fileName, $content, $mimeType, false);
$file = ob_get_clean();
return $file;
}
}
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