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

namespace yiiunit\framework\web;

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

8 9 10
/**
 * @group web
 */
11 12 13 14 15
class ResponseTest extends \yiiunit\TestCase
{
	protected function setUp()
	{
		parent::setUp();
Qiang Xue committed
16
		$this->mockApplication();
Qiang Xue committed
17
		$this->response = new \yii\web\Response;
18 19
	}

20
	public function rightRanges()
21 22 23
	{
		// TODO test more cases for range requests and check for rfc compatibility
		// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
Alexander Makarov committed
24 25 26 27 28
		return [
			['0-5', '0-5', 6, '12ёж'],
			['2-', '2-66', 65, 'ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?'],
			['-12', '55-66', 12, '(ёжик)=?'],
		];
29 30 31
	}

	/**
32
	 * @dataProvider rightRanges
33
	 */
34
	public function testSendFileRanges($rangeHeader, $expectedHeader, $length, $expectedContent)
35
	{
36 37
		$dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
		$fullContent = file_get_contents($dataFile);
38
		$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
39
		ob_start();
Qiang Xue committed
40
		$this->response->sendFile($dataFile)->send(	);
41
		$content = ob_get_clean();
Qiang Xue committed
42

43
		$this->assertEquals($expectedContent, $content);
Qiang Xue committed
44 45 46
		$this->assertEquals(206, $this->response->statusCode);
		$headers = $this->response->headers;
		$this->assertEquals("bytes", $headers->get('Accept-Ranges'));
47
		$this->assertEquals("bytes " . $expectedHeader . '/' . StringHelper::byteLength($fullContent), $headers->get('Content-Range'));
Qiang Xue committed
48 49
		$this->assertEquals('text/plain', $headers->get('Content-Type'));
		$this->assertEquals("$length", $headers->get('Content-Length'));
50 51
	}

52 53 54 55
	public function wrongRanges()
	{
		// TODO test more cases for range requests and check for rfc compatibility
		// http://www.w3.org/Protocols/rfc2616/rfc2616.txt
Alexander Makarov committed
56 57 58 59 60 61
		return [
			['1-2,3-5,6-10'],	// multiple range request not supported
			['5-1'],			// last-byte-pos value is less than its first-byte-pos value
			['-100000'],		// last-byte-pos bigger then content length
			['10000-'],			// first-byte-pos bigger then content length
		];
62 63 64 65 66 67 68
	}

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

71
		$dataFile = \Yii::getAlias('@yiiunit/data/web/data.txt');
72
		$_SERVER['HTTP_RANGE'] = 'bytes=' . $rangeHeader;
73
		$this->response->sendFile($dataFile);
74 75
	}

76 77 78 79
	protected function generateTestFileContent()
	{
		return '12ёжик3456798áèabcdefghijklmnopqrstuvwxyz!"§$%&/(ёжик)=?';
	}
80
}