CovertTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Functional;
  11. use Nyholm\Psr7\Factory\Psr17Factory;
  12. use Nyholm\Psr7\Response as Psr7Response;
  13. use Nyholm\Psr7\ServerRequest as Psr7Request;
  14. use Nyholm\Psr7\Stream as Psr7Stream;
  15. use PHPUnit\Framework\TestCase;
  16. use Psr\Http\Message\ResponseInterface;
  17. use Psr\Http\Message\ServerRequestInterface;
  18. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  19. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  20. use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
  21. use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. use Symfony\Component\HttpFoundation\File\UploadedFile;
  24. use Symfony\Component\HttpFoundation\Request;
  25. use Symfony\Component\HttpFoundation\Response;
  26. /**
  27. * Test to convert a request/response back and forth to make sure we do not loose data.
  28. *
  29. * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  30. */
  31. class CovertTest extends TestCase
  32. {
  33. protected function setUp(): void
  34. {
  35. if (!class_exists(Psr7Request::class)) {
  36. $this->markTestSkipped('nyholm/psr7 is not installed.');
  37. }
  38. }
  39. /**
  40. * @dataProvider requestProvider
  41. *
  42. * @param Request|ServerRequestInterface $request
  43. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
  44. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
  45. */
  46. public function testConvertRequestMultipleTimes($request, $firstFactory, $secondFactory)
  47. {
  48. $temporaryRequest = $firstFactory->createRequest($request);
  49. $finalRequest = $secondFactory->createRequest($temporaryRequest);
  50. if ($finalRequest instanceof Request) {
  51. $this->assertEquals($request->getBasePath(), $finalRequest->getBasePath());
  52. $this->assertEquals($request->getBaseUrl(), $finalRequest->getBaseUrl());
  53. $this->assertEquals($request->getContent(), $finalRequest->getContent());
  54. $this->assertEquals($request->getEncodings(), $finalRequest->getEncodings());
  55. $this->assertEquals($request->getETags(), $finalRequest->getETags());
  56. $this->assertEquals($request->getHost(), $finalRequest->getHost());
  57. $this->assertEquals($request->getHttpHost(), $finalRequest->getHttpHost());
  58. $this->assertEquals($request->getMethod(), $finalRequest->getMethod());
  59. $this->assertEquals($request->getPassword(), $finalRequest->getPassword());
  60. $this->assertEquals($request->getPathInfo(), $finalRequest->getPathInfo());
  61. $this->assertEquals($request->getPort(), $finalRequest->getPort());
  62. $this->assertEquals($request->getProtocolVersion(), $finalRequest->getProtocolVersion());
  63. $this->assertEquals($request->getQueryString(), $finalRequest->getQueryString());
  64. $this->assertEquals($request->getRequestUri(), $finalRequest->getRequestUri());
  65. $this->assertEquals($request->getScheme(), $finalRequest->getScheme());
  66. $this->assertEquals($request->getSchemeAndHttpHost(), $finalRequest->getSchemeAndHttpHost());
  67. $this->assertEquals($request->getScriptName(), $finalRequest->getScriptName());
  68. $this->assertEquals($request->getUri(), $finalRequest->getUri());
  69. $this->assertEquals($request->getUser(), $finalRequest->getUser());
  70. $this->assertEquals($request->getUserInfo(), $finalRequest->getUserInfo());
  71. } elseif ($finalRequest instanceof ServerRequestInterface) {
  72. $strToLower = function ($arr) {
  73. foreach ($arr as $key => $value) {
  74. yield strtolower($key) => $value;
  75. }
  76. };
  77. $this->assertEquals($request->getAttributes(), $finalRequest->getAttributes());
  78. $this->assertEquals($request->getCookieParams(), $finalRequest->getCookieParams());
  79. $this->assertEquals((array) $request->getParsedBody(), (array) $finalRequest->getParsedBody());
  80. $this->assertEquals($request->getQueryParams(), $finalRequest->getQueryParams());
  81. // PSR7 does not define a "withServerParams" so this is impossible to implement without knowing the PSR7 implementation.
  82. //$this->assertEquals($request->getServerParams(), $finalRequest->getServerParams());
  83. $this->assertEquals($request->getUploadedFiles(), $finalRequest->getUploadedFiles());
  84. $this->assertEquals($request->getMethod(), $finalRequest->getMethod());
  85. $this->assertEquals($request->getRequestTarget(), $finalRequest->getRequestTarget());
  86. $this->assertEquals((string) $request->getUri(), (string) $finalRequest->getUri());
  87. $this->assertEquals((string) $request->getBody(), (string) $finalRequest->getBody());
  88. $this->assertEquals($strToLower($request->getHeaders()), $strToLower($finalRequest->getHeaders()));
  89. $this->assertEquals($request->getProtocolVersion(), $finalRequest->getProtocolVersion());
  90. } else {
  91. $this->fail('$finalRequest must be an instance of PSR7 or a HTTPFoundation request');
  92. }
  93. }
  94. public function requestProvider()
  95. {
  96. $sfRequest = new Request(
  97. [
  98. 'foo' => '1',
  99. 'bar' => ['baz' => '42'],
  100. ],
  101. [
  102. 'twitter' => [
  103. '@dunglas' => 'Kévin Dunglas',
  104. '@coopTilleuls' => 'Les-Tilleuls.coop',
  105. ],
  106. 'baz' => '2',
  107. ],
  108. [
  109. 'a2' => ['foo' => 'bar'],
  110. ],
  111. [
  112. 'c1' => 'foo',
  113. 'c2' => ['c3' => 'bar'],
  114. ],
  115. [
  116. 'f1' => $this->createUploadedFile('F1', 'f1.txt', 'text/plain', \UPLOAD_ERR_OK),
  117. 'foo' => ['f2' => $this->createUploadedFile('F2', 'f2.txt', 'text/plain', \UPLOAD_ERR_OK)],
  118. ],
  119. [
  120. 'REQUEST_METHOD' => 'POST',
  121. 'HTTP_HOST' => 'dunglas.fr',
  122. 'SERVER_NAME' => 'dunglas.fr',
  123. 'SERVER_PORT' => null,
  124. 'HTTP_X_SYMFONY' => '2.8',
  125. 'REQUEST_URI' => '/testCreateRequest?foo=1&bar%5Bbaz%5D=42',
  126. 'QUERY_STRING' => 'foo=1&bar%5Bbaz%5D=42',
  127. ],
  128. 'Content'
  129. );
  130. $psr7Requests = [
  131. (new Psr7Request('POST', 'http://tnyholm.se/foo/?bar=biz'))
  132. ->withQueryParams(['bar' => 'biz']),
  133. new Psr7Request('GET', 'https://hey-octave.com/'),
  134. new Psr7Request('GET', 'https://hey-octave.com:443/'),
  135. new Psr7Request('GET', 'https://hey-octave.com:4242/'),
  136. new Psr7Request('GET', 'http://hey-octave.com:80/'),
  137. ];
  138. $nyholmFactory = new Psr17Factory();
  139. $psr17Factory = new PsrHttpFactory($nyholmFactory, $nyholmFactory, $nyholmFactory, $nyholmFactory);
  140. $symfonyFactory = new HttpFoundationFactory();
  141. return array_merge([
  142. [$sfRequest, $psr17Factory, $symfonyFactory],
  143. ], array_map(function ($psr7Request) use ($symfonyFactory, $psr17Factory) {
  144. return [$psr7Request, $symfonyFactory, $psr17Factory];
  145. }, $psr7Requests));
  146. }
  147. /**
  148. * @dataProvider responseProvider
  149. *
  150. * @param Response|ResponseInterface $response
  151. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $firstFactory
  152. * @param HttpFoundationFactoryInterface|HttpMessageFactoryInterface $secondFactory
  153. */
  154. public function testConvertResponseMultipleTimes($response, $firstFactory, $secondFactory)
  155. {
  156. $temporaryResponse = $firstFactory->createResponse($response);
  157. $finalResponse = $secondFactory->createResponse($temporaryResponse);
  158. if ($finalResponse instanceof Response) {
  159. $this->assertEquals($response->getAge(), $finalResponse->getAge());
  160. $this->assertEquals($response->getCharset(), $finalResponse->getCharset());
  161. $this->assertEquals($response->getContent(), $finalResponse->getContent());
  162. $this->assertEquals($response->getDate(), $finalResponse->getDate());
  163. $this->assertEquals($response->getEtag(), $finalResponse->getEtag());
  164. $this->assertEquals($response->getExpires(), $finalResponse->getExpires());
  165. $this->assertEquals($response->getLastModified(), $finalResponse->getLastModified());
  166. $this->assertEquals($response->getMaxAge(), $finalResponse->getMaxAge());
  167. $this->assertEquals($response->getProtocolVersion(), $finalResponse->getProtocolVersion());
  168. $this->assertEquals($response->getStatusCode(), $finalResponse->getStatusCode());
  169. $this->assertEquals($response->getTtl(), $finalResponse->getTtl());
  170. } elseif ($finalResponse instanceof ResponseInterface) {
  171. $strToLower = function ($arr) {
  172. foreach ($arr as $key => $value) {
  173. yield strtolower($key) => $value;
  174. }
  175. };
  176. $this->assertEquals($response->getStatusCode(), $finalResponse->getStatusCode());
  177. $this->assertEquals($response->getReasonPhrase(), $finalResponse->getReasonPhrase());
  178. $this->assertEquals((string) $response->getBody(), (string) $finalResponse->getBody());
  179. $this->assertEquals($strToLower($response->getHeaders()), $strToLower($finalResponse->getHeaders()));
  180. $this->assertEquals($response->getProtocolVersion(), $finalResponse->getProtocolVersion());
  181. } else {
  182. $this->fail('$finalResponse must be an instance of PSR7 or a HTTPFoundation response');
  183. }
  184. }
  185. public function responseProvider()
  186. {
  187. $sfResponse = new Response(
  188. 'Response content.',
  189. 202,
  190. ['x-symfony' => ['3.4']]
  191. );
  192. if (method_exists(Cookie::class, 'create')) {
  193. $cookie = Cookie::create('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT'));
  194. } else {
  195. $cookie = new Cookie('city', 'Lille', new \DateTime('Wed, 13 Jan 2021 22:23:01 GMT'));
  196. }
  197. $sfResponse->headers->setCookie($cookie);
  198. $body = Psr7Stream::create();
  199. $status = 302;
  200. $headers = [
  201. 'location' => ['http://example.com/'],
  202. ];
  203. $zendResponse = new Psr7Response($status, $headers, $body);
  204. $nyholmFactory = new Psr17Factory();
  205. $psr17Factory = new PsrHttpFactory($nyholmFactory, $nyholmFactory, $nyholmFactory, $nyholmFactory);
  206. $symfonyFactory = new HttpFoundationFactory();
  207. return [
  208. [$sfResponse, $psr17Factory, $symfonyFactory],
  209. [$zendResponse, $symfonyFactory, $psr17Factory],
  210. ];
  211. }
  212. private function createUploadedFile($content, $originalName, $mimeType, $error)
  213. {
  214. $path = tempnam(sys_get_temp_dir(), uniqid());
  215. file_put_contents($path, $content);
  216. return new UploadedFile($path, $originalName, $mimeType, $error, true);
  217. }
  218. }