StreamedResponseTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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'; }, 200, array('Content-Length' => '123'));
  43. $request = Request::create('/', 'HEAD');
  44. $response->prepare($request);
  45. $this->assertSame('123', $response->headers->get('Content-Length'));
  46. }
  47. public function testPrepareWithCacheHeaders()
  48. {
  49. $response = new StreamedResponse(function () { echo 'foo'; }, 200, array('Cache-Control' => 'max-age=600, public'));
  50. $request = Request::create('/', 'GET');
  51. $response->prepare($request);
  52. $this->assertEquals('max-age=600, public', $response->headers->get('Cache-Control'));
  53. }
  54. public function testSendContent()
  55. {
  56. $called = 0;
  57. $response = new StreamedResponse(function () use (&$called) { ++$called; });
  58. $response->sendContent();
  59. $this->assertEquals(1, $called);
  60. $response->sendContent();
  61. $this->assertEquals(1, $called);
  62. }
  63. /**
  64. * @expectedException \LogicException
  65. */
  66. public function testSendContentWithNonCallable()
  67. {
  68. $response = new StreamedResponse(null);
  69. $response->sendContent();
  70. }
  71. /**
  72. * @expectedException \LogicException
  73. */
  74. public function testSetContent()
  75. {
  76. $response = new StreamedResponse(function () { echo 'foo'; });
  77. $response->setContent('foo');
  78. }
  79. public function testGetContent()
  80. {
  81. $response = new StreamedResponse(function () { echo 'foo'; });
  82. $this->assertFalse($response->getContent());
  83. }
  84. public function testCreate()
  85. {
  86. $response = StreamedResponse::create(function () {}, 204);
  87. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  88. $this->assertEquals(204, $response->getStatusCode());
  89. }
  90. }