StreamedResponseTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\StreamedResponse;
  13. class StreamedResponseTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain'));
  18. $this->assertEquals(404, $response->getStatusCode());
  19. $this->assertEquals('text/plain', $response->headers->get('Content-Type'));
  20. }
  21. public function testPrepareWith11Protocol()
  22. {
  23. $response = new StreamedResponse(function () { echo 'foo'; });
  24. $request = Request::create('/');
  25. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
  26. $response->prepare($request);
  27. $this->assertEquals('1.1', $response->getProtocolVersion());
  28. $this->assertNotEquals('chunked', $response->headers->get('Transfer-Encoding'), 'Apache assumes responses with a Transfer-Encoding header set to chunked to already be encoded.');
  29. $this->assertEquals('no-cache, private', $response->headers->get('Cache-Control'));
  30. }
  31. public function testPrepareWith10Protocol()
  32. {
  33. $response = new StreamedResponse(function () { echo 'foo'; });
  34. $request = Request::create('/');
  35. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
  36. $response->prepare($request);
  37. $this->assertEquals('1.0', $response->getProtocolVersion());
  38. $this->assertNull($response->headers->get('Transfer-Encoding'));
  39. $this->assertEquals('no-cache, private', $response->headers->get('Cache-Control'));
  40. }
  41. public function testPrepareWithHeadRequest()
  42. {
  43. $response = new StreamedResponse(function () { echo 'foo'; });
  44. $request = Request::create('/', 'HEAD');
  45. $response->prepare($request);
  46. }
  47. public function testSendContent()
  48. {
  49. $called = 0;
  50. $response = new StreamedResponse(function () use (&$called) { ++$called; });
  51. $response->sendContent();
  52. $this->assertEquals(1, $called);
  53. $response->sendContent();
  54. $this->assertEquals(1, $called);
  55. }
  56. /**
  57. * @expectedException \LogicException
  58. */
  59. public function testSendContentWithNonCallable()
  60. {
  61. $response = new StreamedResponse(null);
  62. $response->sendContent();
  63. }
  64. /**
  65. * @expectedException \LogicException
  66. */
  67. public function testSetCallbackNonCallable()
  68. {
  69. $response = new StreamedResponse(null);
  70. $response->setCallback(null);
  71. }
  72. /**
  73. * @expectedException \LogicException
  74. */
  75. public function testSetContent()
  76. {
  77. $response = new StreamedResponse(function () { echo 'foo'; });
  78. $response->setContent('foo');
  79. }
  80. public function testGetContent()
  81. {
  82. $response = new StreamedResponse(function () { echo 'foo'; });
  83. $this->assertFalse($response->getContent());
  84. }
  85. public function testCreate()
  86. {
  87. $response = StreamedResponse::create(function () {}, 204);
  88. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  89. $this->assertEquals(204, $response->getStatusCode());
  90. }
  91. }