ResponseTest.php 2.47 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 18
/**
 * @group web
 */
19 20
class ResponseTest extends \yiiunit\TestCase
{
21
	/**
Qiang Xue committed
22
	 * @var MockResponse
23
	 */
Qiang Xue committed
24
	public $response;
25 26 27 28

	protected function setUp()
	{
		parent::setUp();
Qiang Xue committed
29
		$this->mockApplication();
Qiang Xue committed
30
		$this->response = new MockResponse;
31 32
	}

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

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

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

65 66 67 68
	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
69 70 71 72 73 74
		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
		];
75 76 77 78 79 80 81
	}

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

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

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