AbstractHttpMessageFactoryTest.php 8.2 KB

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