AbstractHttpMessageFactoryTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\Bridge\PsrHttpMessage\Tests\Factory;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  13. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\StreamedResponse;
  19. /**
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. * @author Antonio J. García Lagar <aj@garcialagar.es>
  22. */
  23. abstract class AbstractHttpMessageFactoryTest extends TestCase
  24. {
  25. private $factory;
  26. private $tmpDir;
  27. abstract protected function buildHttpMessageFactory(): HttpMessageFactoryInterface;
  28. public function setUp(): void
  29. {
  30. $this->factory = $this->buildHttpMessageFactory();
  31. $this->tmpDir = sys_get_temp_dir();
  32. }
  33. public function testCreateRequest()
  34. {
  35. $stdClass = new \stdClass();
  36. $request = new Request(
  37. [
  38. 'bar' => ['baz' => '42'],
  39. 'foo' => '1',
  40. ],
  41. [
  42. 'twitter' => [
  43. '@dunglas' => 'Kévin Dunglas',
  44. '@coopTilleuls' => 'Les-Tilleuls.coop',
  45. ],
  46. 'baz' => '2',
  47. ],
  48. [
  49. 'a1' => $stdClass,
  50. 'a2' => ['foo' => 'bar'],
  51. ],
  52. [
  53. 'c1' => 'foo',
  54. 'c2' => ['c3' => 'bar'],
  55. ],
  56. [
  57. 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', UPLOAD_ERR_OK),
  58. 'foo' => ['f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', UPLOAD_ERR_OK)],
  59. ],
  60. [
  61. 'REQUEST_METHOD' => 'POST',
  62. 'HTTP_HOST' => 'dunglas.fr',
  63. 'HTTP_X_SYMFONY' => '2.8',
  64. 'REQUEST_URI' => '/testCreateRequest?bar[baz]=42&foo=1',
  65. 'QUERY_STRING' => 'bar[baz]=42&foo=1',
  66. ],
  67. 'Content'
  68. );
  69. $psrRequest = $this->factory->createRequest($request);
  70. $this->assertEquals('Content', $psrRequest->getBody()->__toString());
  71. $queryParams = $psrRequest->getQueryParams();
  72. $this->assertEquals('1', $queryParams['foo']);
  73. $this->assertEquals('42', $queryParams['bar']['baz']);
  74. $requestTarget = $psrRequest->getRequestTarget();
  75. $this->assertEquals('/testCreateRequest?bar[baz]=42&foo=1', urldecode($requestTarget));
  76. $parsedBody = $psrRequest->getParsedBody();
  77. $this->assertEquals('Kévin Dunglas', $parsedBody['twitter']['@dunglas']);
  78. $this->assertEquals('Les-Tilleuls.coop', $parsedBody['twitter']['@coopTilleuls']);
  79. $this->assertEquals('2', $parsedBody['baz']);
  80. $attributes = $psrRequest->getAttributes();
  81. $this->assertEquals($stdClass, $attributes['a1']);
  82. $this->assertEquals('bar', $attributes['a2']['foo']);
  83. $cookies = $psrRequest->getCookieParams();
  84. $this->assertEquals('foo', $cookies['c1']);
  85. $this->assertEquals('bar', $cookies['c2']['c3']);
  86. $uploadedFiles = $psrRequest->getUploadedFiles();
  87. $this->assertEquals('F1', $uploadedFiles['f1']->getStream()->__toString());
  88. $this->assertEquals('f1.txt', $uploadedFiles['f1']->getClientFilename());
  89. $this->assertEquals('text/plain', $uploadedFiles['f1']->getClientMediaType());
  90. $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['f1']->getError());
  91. $this->assertEquals('F2', $uploadedFiles['foo']['f2']->getStream()->__toString());
  92. $this->assertEquals('f2.txt', $uploadedFiles['foo']['f2']->getClientFilename());
  93. $this->assertEquals('text/plain', $uploadedFiles['foo']['f2']->getClientMediaType());
  94. $this->assertEquals(UPLOAD_ERR_OK, $uploadedFiles['foo']['f2']->getError());
  95. $serverParams = $psrRequest->getServerParams();
  96. $this->assertEquals('POST', $serverParams['REQUEST_METHOD']);
  97. $this->assertEquals('2.8', $serverParams['HTTP_X_SYMFONY']);
  98. $this->assertEquals('POST', $psrRequest->getMethod());
  99. $this->assertEquals(['2.8'], $psrRequest->getHeader('X-Symfony'));
  100. }
  101. public function testGetContentCanBeCalledAfterRequestCreation()
  102. {
  103. $header = ['HTTP_HOST' => 'dunglas.fr'];
  104. $request = new Request([], [], [], [], [], $header, 'Content');
  105. $psrRequest = $this->factory->createRequest($request);
  106. $this->assertEquals('Content', $psrRequest->getBody()->__toString());
  107. $this->assertEquals('Content', $request->getContent());
  108. }
  109. private function createUploadedFile($content, $originalName, $mimeType, $error)
  110. {
  111. $path = tempnam($this->tmpDir, uniqid());
  112. file_put_contents($path, $content);
  113. return new UploadedFile($path, $originalName, $mimeType, $error, true);
  114. }
  115. public function testCreateResponse()
  116. {
  117. $response = new Response(
  118. 'Response content.',
  119. 202,
  120. ['X-Symfony' => ['3.4']]
  121. );
  122. $response->headers->setCookie(new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT'), '/', null, false, true, false, 'lax'));
  123. $psrResponse = $this->factory->createResponse($response);
  124. $this->assertEquals('Response content.', $psrResponse->getBody()->__toString());
  125. $this->assertEquals(202, $psrResponse->getStatusCode());
  126. $this->assertEquals(['3.4'], $psrResponse->getHeader('X-Symfony'));
  127. $cookieHeader = $psrResponse->getHeader('Set-Cookie');
  128. $this->assertIsArray($cookieHeader);
  129. $this->assertCount(1, $cookieHeader);
  130. $this->assertRegExp('{city=Lille; expires=Wed, 13-Jan-2021 22:23:01 GMT;( max-age=\d+;)? path=/; httponly}i', $cookieHeader[0]);
  131. }
  132. public function testCreateResponseFromStreamed()
  133. {
  134. $response = new StreamedResponse(function () {
  135. echo "Line 1\n";
  136. flush();
  137. echo "Line 2\n";
  138. flush();
  139. });
  140. $psrResponse = $this->factory->createResponse($response);
  141. $this->assertEquals("Line 1\nLine 2\n", $psrResponse->getBody()->__toString());
  142. }
  143. public function testCreateResponseFromBinaryFile()
  144. {
  145. $path = tempnam($this->tmpDir, uniqid());
  146. file_put_contents($path, 'Binary');
  147. $response = new BinaryFileResponse($path);
  148. $psrResponse = $this->factory->createResponse($response);
  149. $this->assertEquals('Binary', $psrResponse->getBody()->__toString());
  150. }
  151. public function testUploadErrNoFile()
  152. {
  153. $file = new UploadedFile('', '', null, UPLOAD_ERR_NO_FILE, true);
  154. $this->assertEquals(0, $file->getSize());
  155. $this->assertEquals(UPLOAD_ERR_NO_FILE, $file->getError());
  156. $this->assertFalse($file->getSize(), 'SplFile::getSize() returns false on error');
  157. $request = new Request(
  158. [],
  159. [],
  160. [],
  161. [],
  162. [
  163. 'f1' => $file,
  164. 'f2' => ['name' => null, 'type' => null, 'tmp_name' => null, 'error' => UPLOAD_ERR_NO_FILE, 'size' => 0],
  165. ],
  166. [
  167. 'REQUEST_METHOD' => 'POST',
  168. 'HTTP_HOST' => 'dunglas.fr',
  169. 'HTTP_X_SYMFONY' => '2.8',
  170. ],
  171. 'Content'
  172. );
  173. $psrRequest = $this->factory->createRequest($request);
  174. $uploadedFiles = $psrRequest->getUploadedFiles();
  175. $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f1']->getError());
  176. $this->assertEquals(UPLOAD_ERR_NO_FILE, $uploadedFiles['f2']->getError());
  177. }
  178. }