StreamedResponseTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  30. public function testPrepareWith10Protocol()
  31. {
  32. $response = new StreamedResponse(function () { echo 'foo'; });
  33. $request = Request::create('/');
  34. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.0');
  35. $response->prepare($request);
  36. $this->assertEquals('1.0', $response->getProtocolVersion());
  37. $this->assertNull($response->headers->get('Transfer-Encoding'));
  38. }
  39. public function testPrepareWithHeadRequest()
  40. {
  41. $response = new StreamedResponse(function () { echo 'foo'; });
  42. $request = Request::create('/', 'HEAD');
  43. $response->prepare($request);
  44. }
  45. public function testPrepareWithCacheHeaders()
  46. {
  47. $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
  48. $request = Request::create('/', 'GET');
  49. $response->prepare($request);
  50. $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
  51. }
  52. public function testSendContent()
  53. {
  54. $called = 0;
  55. $response = new StreamedResponse(function () use (&$called) { ++$called; });
  56. $response->sendContent();
  57. $this->assertEquals(1, $called);
  58. $response->sendContent();
  59. $this->assertEquals(1, $called);
  60. }
  61. /**
  62. * @expectedException \LogicException
  63. */
  64. public function testSendContentWithNonCallable()
  65. {
  66. $response = new StreamedResponse(null);
  67. $response->sendContent();
  68. }
  69. /**
  70. * @expectedException \LogicException
  71. */
  72. public function testSetCallbackNonCallable()
  73. {
  74. $response = new StreamedResponse(null);
  75. $response->setCallback(null);
  76. }
  77. /**
  78. * @expectedException \LogicException
  79. */
  80. public function testSetContent()
  81. {
  82. $response = new StreamedResponse(function () { echo 'foo'; });
  83. $response->setContent('foo');
  84. }
  85. public function testGetContent()
  86. {
  87. $response = new StreamedResponse(function () { echo 'foo'; });
  88. $this->assertFalse($response->getContent());
  89. }
  90. public function testCreate()
  91. {
  92. $response = StreamedResponse::create(function () {}, 204);
  93. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  94. $this->assertEquals(204, $response->getStatusCode());
  95. }
  96. }