1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\HttpFoundation\Tests; 13 14use PHPUnit\Framework\TestCase; 15use Symfony\Component\HttpFoundation\Request; 16use Symfony\Component\HttpFoundation\StreamedResponse; 17 18class StreamedResponseTest extends TestCase 19{ 20 public function testConstructor() 21 { 22 $response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain')); 23 24 $this->assertEquals(404, $response->getStatusCode()); 25 $this->assertEquals('text/plain', $response->headers->get('Content-Type')); 26 } 27 28 public function testPrepareWith11Protocol() 29 { 30 $response = new StreamedResponse(function () { echo 'foo'; }); 31 $request = Request::create('/'); 32 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1'); 33 34 $response->prepare($request); 35 36 $this->assertEquals('1.1', $response->getProtocolVersion()); 37 $this->assertNotEquals('chunked', $response->headers->get('Transfer-Encoding'), 'Apache assumes responses with a Transfer-Encoding header set to chunked to already be encoded.'); 38 } 39 40 public function testPrepareWith10Protocol() 41 { 42 $response = new StreamedResponse(function () { echo 'foo'; }); 43 $request = Request::create('/'); 44 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0'); 45 46 $response->prepare($request); 47 48 $this->assertEquals('1.0', $response->getProtocolVersion()); 49 $this->assertNull($response->headers->get('Transfer-Encoding')); 50 } 51 52 public function testPrepareWithHeadRequest() 53 { 54 $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Content-Length' => '123')); 55 $request = Request::create('/', 'HEAD'); 56 57 $response->prepare($request); 58 59 $this->assertSame('123', $response->headers->get('Content-Length')); 60 } 61 62 public function testPrepareWithCacheHeaders() 63 { 64 $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public')); 65 $request = Request::create('/', 'GET'); 66 67 $response->prepare($request); 68 $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control')); 69 } 70 71 public function testSendContent() 72 { 73 $called = 0; 74 75 $response = new StreamedResponse(function () use (&$called) { ++$called; }); 76 77 $response->sendContent(); 78 $this->assertEquals(1, $called); 79 80 $response->sendContent(); 81 $this->assertEquals(1, $called); 82 } 83 84 /** 85 * @expectedException \LogicException 86 */ 87 public function testSendContentWithNonCallable() 88 { 89 $response = new StreamedResponse(null); 90 $response->sendContent(); 91 } 92 93 /** 94 * @expectedException \LogicException 95 */ 96 public function testSetContent() 97 { 98 $response = new StreamedResponse(function () { echo 'foo'; }); 99 $response->setContent('foo'); 100 } 101 102 public function testGetContent() 103 { 104 $response = new StreamedResponse(function () { echo 'foo'; }); 105 $this->assertFalse($response->getContent()); 106 } 107 108 public function testCreate() 109 { 110 $response = StreamedResponse::create(function () {}, 204); 111 112 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response); 113 $this->assertEquals(204, $response->getStatusCode()); 114 } 115 116 public function testReturnThis() 117 { 118 $response = new StreamedResponse(function () {}); 119 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendContent()); 120 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendContent()); 121 122 $response = new StreamedResponse(function () {}); 123 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendHeaders()); 124 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendHeaders()); 125 } 126} 127