ResponseTest.php 2.49 KB
Newer Older
1 2 3 4
<?php

namespace yiiunit\framework\web;

Qiang Xue committed
5
use Yii;
6
use yii\helpers\StringHelper;
7

Qiang Xue committed
8
class MockResponse extends \yii\web\Response
9 10 11 12 13 14
{
	public function send()
	{
		// does nothing to allow testing
	}
}
15 16 17

class ResponseTest extends \yiiunit\TestCase
{
18
	/**
Qiang Xue committed
19
	 * @var MockResponse
20
	 */
Qiang Xue committed
21
	public $response;
22 23 24 25

	protected function setUp()
	{
		parent::setUp();
Qiang Xue committed
26
		$this->mockApplication();
Qiang Xue committed
27
		$this->response = new MockResponse;
28 29
	}

30
	public function rightRanges()
31 32 33 34 35 36 37 38 39 40 41
	{
		// TODO test more cases for range requests and check for rfc compatibility
		// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
		return array(
			array('0-5', '0-5', 6, '12ёж'),
			array('2-', '2-66', 65, 'ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?'),
			array('-12', '55-66', 12, '(ёжик)=?'),
		);
	}

	/**
42
	 * @dataProvider rightRanges
43
	 */
44
	public function testSendFileRanges($rangeHeader, $expectedHeader, $length, $expectedContent)
45
	{
46 47
		$dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
		$fullContent = file_get_contents($dataFile);
48
		$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
49 50 51
		ob_start();
		$this->response->sendFile($dataFile);
		$content = ob_get_clean();
Qiang Xue committed
52

53
		$this->assertEquals($expectedContent, $content);
Qiang Xue committed
54 55 56
		$this->assertEquals(206, $this->response->statusCode);
		$headers = $this->response->headers;
		$this->assertEquals("bytes", $headers->get('Accept-Ranges'));
57
		$this->assertEquals("bytes " . $expectedHeader . '/' . StringHelper::strlen($fullContent), $headers->get('Content-Range'));
Qiang Xue committed
58 59
		$this->assertEquals('text/plain', $headers->get('Content-Type'));
		$this->assertEquals("$length", $headers->get('Content-Length'));
60 61
	}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	public function wrongRanges()
	{
		// TODO test more cases for range requests and check for rfc compatibility
		// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
		return array(
			array('1-2,3-5,6-10'),	// multiple range request not supported
			array('5-1'),			// last-byte-pos value is less than its first-byte-pos value
			array('-100000'),		// last-byte-pos bigger then content length
			array('10000-'),			// first-byte-pos bigger then content length
		);
	}

	/**
	 * @dataProvider wrongRanges
	 */
	public function testSendFileWrongRanges($rangeHeader)
	{
Qiang Xue committed
79
		$this->setExpectedException('yii\web\HttpException');
80

81
		$dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
82
		$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
83
		$this->response->sendFile($dataFile);
84 85
	}

86 87 88 89
	protected function generateTestFileContent()
	{
		return '12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?';
	}
90
}