StreamedResponseTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\StreamedResponse;
  14. class StreamedResponseTest extends TestCase
  15. {
  16. public function testConstructor()
  17. {
  18. $response = new StreamedResponse(function () { echo 'foo'; }, 404, array('Content-Type' => 'text/plain'));
  19. $this->assertEquals(404, $response->getStatusCode());
  20. $this->assertEquals('text/plain', $response->headers->get('Content-Type'));
  21. }
  22. public function testPrepareWith11Protocol()
  23. {
  24. $response = new StreamedResponse(function () { echo 'foo'; });
  25. $request = Request::create('/');
  26. $request->server->set('SERVER_PROTOCOL', 'HTTP/1.1');
  27. $response->prepare($request);
  28. $this->assertEquals('1.1', $response->getProtocolVersion());
  29. $this->assertNotEquals('chunked', $response->headers->get('Transfer-Encoding'), 'Apache assumes responses with a Transfer-Encoding header set to chunked to already be encoded.');
  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. }
  40. public function testPrepareWithHeadRequest()
  41. {
  42. $response = new StreamedResponse(function () { echo 'foo'; });
  43. $request = Request::create('/', 'HEAD');
  44. $response->prepare($request);
  45. }
  46. public function testPrepareWithCacheHeaders()
  47. {
  48. $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
  49. $request = Request::create('/', 'GET');
  50. $response->prepare($request);
  51. $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
  52. }
  53. public function testSendContent()
  54. {
  55. $called = 0;
  56. $response = new StreamedResponse(function () use (&$called) { ++$called; });
  57. $response->sendContent();
  58. $this->assertEquals(1, $called);
  59. $response->sendContent();
  60. $this->assertEquals(1, $called);
  61. }
  62. /**
  63. * @expectedException \LogicException
  64. */
  65. public function testSendContentWithNonCallable()
  66. {
  67. $response = new StreamedResponse(null);
  68. $response->sendContent();
  69. }
  70. /**
  71. * @expectedException \LogicException
  72. */
  73. public function testSetContent()
  74. {
  75. $response = new StreamedResponse(function () { echo 'foo'; });
  76. $response->setContent('foo');
  77. }
  78. public function testGetContent()
  79. {
  80. $response = new StreamedResponse(function () { echo 'foo'; });
  81. $this->assertFalse($response->getContent());
  82. }
  83. public function testCreate()
  84. {
  85. $response = StreamedResponse::create(function () {}, 204);
  86. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  87. $this->assertEquals(204, $response->getStatusCode());
  88. }
  89. }