4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\StreamedResponse;
18 class StreamedResponseTest extends TestCase
20 public function testConstructor()
22 $response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain'));
24 $this->assertEquals(404, $response->getStatusCode());
25 $this->assertEquals('text/plain', $response->headers->get('Content-Type'));
28 public function testPrepareWith11Protocol()
30 $response = new StreamedResponse(function () { echo 'foo'; });
31 $request = Request::create('/');
32 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
34 $response->prepare($request);
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.');
40 public function testPrepareWith10Protocol()
42 $response = new StreamedResponse(function () { echo 'foo'; });
43 $request = Request::create('/');
44 $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
46 $response->prepare($request);
48 $this->assertEquals('1.0', $response->getProtocolVersion());
49 $this->assertNull($response->headers->get('Transfer-Encoding'));
52 public function testPrepareWithHeadRequest()
54 $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Content-Length' => '123'));
55 $request = Request::create('/', 'HEAD');
57 $response->prepare($request);
59 $this->assertSame('123', $response->headers->get('Content-Length'));
62 public function testPrepareWithCacheHeaders()
64 $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
65 $request = Request::create('/', 'GET');
67 $response->prepare($request);
68 $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
71 public function testSendContent()
75 $response = new StreamedResponse(function () use (&$called) { ++$called; });
77 $response->sendContent();
78 $this->assertEquals(1, $called);
80 $response->sendContent();
81 $this->assertEquals(1, $called);
85 * @expectedException \LogicException
87 public function testSendContentWithNonCallable()
89 $response = new StreamedResponse(null);
90 $response->sendContent();
94 * @expectedException \LogicException
96 public function testSetCallbackNonCallable()
98 $response = new StreamedResponse(null);
99 $response->setCallback(null);
103 * @expectedException \LogicException
105 public function testSetContent()
107 $response = new StreamedResponse(function () { echo 'foo'; });
108 $response->setContent('foo');
111 public function testGetContent()
113 $response = new StreamedResponse(function () { echo 'foo'; });
114 $this->assertFalse($response->getContent());
117 public function testCreate()
119 $response = StreamedResponse::create(function () {}, 204);
121 $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
122 $this->assertEquals(204, $response->getStatusCode());