25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

141 lines
4.8 KiB

  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, ['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, ['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, ['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. public function testSendContentWithNonCallable()
  64. {
  65. $this->expectException('LogicException');
  66. $response = new StreamedResponse(null);
  67. $response->sendContent();
  68. }
  69. public function testSetContent()
  70. {
  71. $this->expectException('LogicException');
  72. $response = new StreamedResponse(function () { echo 'foo'; });
  73. $response->setContent('foo');
  74. }
  75. public function testGetContent()
  76. {
  77. $response = new StreamedResponse(function () { echo 'foo'; });
  78. $this->assertFalse($response->getContent());
  79. }
  80. public function testCreate()
  81. {
  82. $response = StreamedResponse::create(function () {}, 204);
  83. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response);
  84. $this->assertEquals(204, $response->getStatusCode());
  85. }
  86. public function testReturnThis()
  87. {
  88. $response = new StreamedResponse(function () {});
  89. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendContent());
  90. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendContent());
  91. $response = new StreamedResponse(function () {});
  92. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendHeaders());
  93. $this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response->sendHeaders());
  94. }
  95. public function testSetNotModified()
  96. {
  97. $response = new StreamedResponse(function () { echo 'foo'; });
  98. $modified = $response->setNotModified();
  99. $this->assertObjectHasAttribute('headers', $modified);
  100. $this->assertObjectHasAttribute('content', $modified);
  101. $this->assertObjectHasAttribute('version', $modified);
  102. $this->assertObjectHasAttribute('statusCode', $modified);
  103. $this->assertObjectHasAttribute('statusText', $modified);
  104. $this->assertObjectHasAttribute('charset', $modified);
  105. $this->assertEquals(304, $modified->getStatusCode());
  106. ob_start();
  107. $modified->sendContent();
  108. $string = ob_get_clean();
  109. $this->assertEmpty($string);
  110. }
  111. }