HttpFoundationFactoryTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 Psr\Http\Message\UploadedFileInterface;
  13. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  14. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Response;
  15. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\ServerRequest;
  16. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Stream;
  17. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\UploadedFile;
  18. use Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\Uri;
  19. use Symfony\Component\HttpFoundation\Cookie;
  20. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  21. use Symfony\Component\HttpFoundation\File\UploadedFile as HttpFoundationUploadedFile;
  22. /**
  23. * @author Kévin Dunglas <dunglas@gmail.com>
  24. */
  25. class HttpFoundationFactoryTest extends TestCase
  26. {
  27. /** @var HttpFoundationFactory */
  28. private $factory;
  29. /** @var string */
  30. private $tmpDir;
  31. protected function setUp(): void
  32. {
  33. $this->factory = new HttpFoundationFactory();
  34. $this->tmpDir = sys_get_temp_dir();
  35. }
  36. public function testCreateRequest()
  37. {
  38. $stdClass = new \stdClass();
  39. $serverRequest = new ServerRequest(
  40. '1.1',
  41. [
  42. 'X-Dunglas-API-Platform' => '1.0',
  43. 'X-data' => ['a', 'b'],
  44. ],
  45. new Stream('The body'),
  46. '/about/kevin',
  47. 'GET',
  48. 'http://les-tilleuls.coop/about/kevin',
  49. ['country' => 'France'],
  50. ['city' => 'Lille'],
  51. ['url' => 'http://les-tilleuls.coop'],
  52. [
  53. 'doc1' => $this->createUploadedFile('Doc 1', \UPLOAD_ERR_OK, 'doc1.txt', 'text/plain'),
  54. 'nested' => [
  55. 'docs' => [
  56. $this->createUploadedFile('Doc 2', \UPLOAD_ERR_OK, 'doc2.txt', 'text/plain'),
  57. $this->createUploadedFile('Doc 3', \UPLOAD_ERR_OK, 'doc3.txt', 'text/plain'),
  58. ],
  59. ],
  60. ],
  61. ['url' => 'http://dunglas.fr'],
  62. ['custom' => $stdClass]
  63. );
  64. $symfonyRequest = $this->factory->createRequest($serverRequest);
  65. $files = $symfonyRequest->files->all();
  66. $this->assertEquals('http://les-tilleuls.coop', $symfonyRequest->query->get('url'));
  67. $this->assertEquals('doc1.txt', $files['doc1']->getClientOriginalName());
  68. $this->assertEquals('doc2.txt', $files['nested']['docs'][0]->getClientOriginalName());
  69. $this->assertEquals('doc3.txt', $files['nested']['docs'][1]->getClientOriginalName());
  70. $this->assertEquals('http://dunglas.fr', $symfonyRequest->request->get('url'));
  71. $this->assertEquals($stdClass, $symfonyRequest->attributes->get('custom'));
  72. $this->assertEquals('Lille', $symfonyRequest->cookies->get('city'));
  73. $this->assertEquals('France', $symfonyRequest->server->get('country'));
  74. $this->assertEquals('The body', $symfonyRequest->getContent());
  75. $this->assertEquals('1.0', $symfonyRequest->headers->get('X-Dunglas-API-Platform'));
  76. $this->assertEquals(['a', 'b'], $symfonyRequest->headers->all('X-data'));
  77. }
  78. public function testCreateRequestWithStreamedBody()
  79. {
  80. $serverRequest = new ServerRequest(
  81. '1.1',
  82. [],
  83. new Stream('The body'),
  84. '/',
  85. 'GET',
  86. null,
  87. [],
  88. [],
  89. [],
  90. [],
  91. null,
  92. []
  93. );
  94. $symfonyRequest = $this->factory->createRequest($serverRequest, true);
  95. $this->assertEquals('The body', $symfonyRequest->getContent());
  96. }
  97. public function testCreateRequestWithNullParsedBody()
  98. {
  99. $serverRequest = new ServerRequest(
  100. '1.1',
  101. [],
  102. new Stream(),
  103. '/',
  104. 'GET',
  105. null,
  106. [],
  107. [],
  108. [],
  109. [],
  110. null,
  111. []
  112. );
  113. $this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
  114. }
  115. public function testCreateRequestWithObjectParsedBody()
  116. {
  117. $serverRequest = new ServerRequest(
  118. '1.1',
  119. [],
  120. new Stream(),
  121. '/',
  122. 'GET',
  123. null,
  124. [],
  125. [],
  126. [],
  127. [],
  128. new \stdClass(),
  129. []
  130. );
  131. $this->assertCount(0, $this->factory->createRequest($serverRequest)->request);
  132. }
  133. public function testCreateRequestWithUri()
  134. {
  135. $serverRequest = new ServerRequest(
  136. '1.1',
  137. [],
  138. new Stream(),
  139. '/',
  140. 'GET',
  141. new Uri('http://les-tilleuls.coop/about/kevin'),
  142. [],
  143. [],
  144. [],
  145. [],
  146. null,
  147. []
  148. );
  149. $this->assertEquals('/about/kevin', $this->factory->createRequest($serverRequest)->getPathInfo());
  150. }
  151. public function testCreateUploadedFile()
  152. {
  153. $uploadedFile = $this->createUploadedFile('An uploaded file.', \UPLOAD_ERR_OK, 'myfile.txt', 'text/plain');
  154. $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
  155. $size = $symfonyUploadedFile->getSize();
  156. $uniqid = uniqid();
  157. $symfonyUploadedFile->move($this->tmpDir, $uniqid);
  158. $this->assertEquals($uploadedFile->getSize(), $size);
  159. $this->assertEquals(\UPLOAD_ERR_OK, $symfonyUploadedFile->getError());
  160. $this->assertEquals('myfile.txt', $symfonyUploadedFile->getClientOriginalName());
  161. $this->assertEquals('txt', $symfonyUploadedFile->getClientOriginalExtension());
  162. $this->assertEquals('text/plain', $symfonyUploadedFile->getClientMimeType());
  163. $this->assertEquals('An uploaded file.', file_get_contents($this->tmpDir.'/'.$uniqid));
  164. }
  165. public function testCreateUploadedFileWithError()
  166. {
  167. $this->expectException(FileException::class);
  168. $this->expectExceptionMessage('The file "e" could not be written on disk.');
  169. $uploadedFile = $this->createUploadedFile('Error.', \UPLOAD_ERR_CANT_WRITE, 'e', 'text/plain');
  170. $symfonyUploadedFile = $this->callCreateUploadedFile($uploadedFile);
  171. $this->assertEquals(\UPLOAD_ERR_CANT_WRITE, $symfonyUploadedFile->getError());
  172. $symfonyUploadedFile->move($this->tmpDir, 'shouldFail.txt');
  173. }
  174. private function createUploadedFile($content, $error, $clientFileName, $clientMediaType): UploadedFile
  175. {
  176. $filePath = tempnam($this->tmpDir, uniqid());
  177. file_put_contents($filePath, $content);
  178. return new UploadedFile($filePath, filesize($filePath), $error, $clientFileName, $clientMediaType);
  179. }
  180. private function callCreateUploadedFile(UploadedFileInterface $uploadedFile): HttpFoundationUploadedFile
  181. {
  182. $reflection = new \ReflectionClass($this->factory);
  183. $createUploadedFile = $reflection->getMethod('createUploadedFile');
  184. $createUploadedFile->setAccessible(true);
  185. return $createUploadedFile->invokeArgs($this->factory, [$uploadedFile]);
  186. }
  187. public function testCreateResponse()
  188. {
  189. $response = new Response(
  190. '1.0',
  191. [
  192. 'X-Symfony' => ['2.8'],
  193. 'Set-Cookie' => [
  194. 'theme=light',
  195. 'test',
  196. 'ABC=AeD; Domain=dunglas.fr; Path=/kevin; Expires=Wed, 13 Jan 2021 22:23:01 GMT; Secure; HttpOnly; SameSite=Strict',
  197. ],
  198. ],
  199. new Stream('The response body'),
  200. 200
  201. );
  202. $symfonyResponse = $this->factory->createResponse($response);
  203. $this->assertEquals('1.0', $symfonyResponse->getProtocolVersion());
  204. $this->assertEquals('2.8', $symfonyResponse->headers->get('X-Symfony'));
  205. $cookies = $symfonyResponse->headers->getCookies();
  206. $this->assertEquals('theme', $cookies[0]->getName());
  207. $this->assertEquals('light', $cookies[0]->getValue());
  208. $this->assertEquals(0, $cookies[0]->getExpiresTime());
  209. $this->assertNull($cookies[0]->getDomain());
  210. $this->assertEquals('/', $cookies[0]->getPath());
  211. $this->assertFalse($cookies[0]->isSecure());
  212. $this->assertFalse($cookies[0]->isHttpOnly());
  213. $this->assertEquals('test', $cookies[1]->getName());
  214. $this->assertNull($cookies[1]->getValue());
  215. $this->assertEquals('ABC', $cookies[2]->getName());
  216. $this->assertEquals('AeD', $cookies[2]->getValue());
  217. $this->assertEquals(strtotime('Wed, 13 Jan 2021 22:23:01 GMT'), $cookies[2]->getExpiresTime());
  218. $this->assertEquals('dunglas.fr', $cookies[2]->getDomain());
  219. $this->assertEquals('/kevin', $cookies[2]->getPath());
  220. $this->assertTrue($cookies[2]->isSecure());
  221. $this->assertTrue($cookies[2]->isHttpOnly());
  222. if (\defined('Symfony\Component\HttpFoundation\Cookie::SAMESITE_STRICT')) {
  223. $this->assertEquals(Cookie::SAMESITE_STRICT, $cookies[2]->getSameSite());
  224. }
  225. $this->assertEquals('The response body', $symfonyResponse->getContent());
  226. $this->assertEquals(200, $symfonyResponse->getStatusCode());
  227. $symfonyResponse = $this->factory->createResponse($response, true);
  228. ob_start();
  229. $symfonyResponse->sendContent();
  230. $sentContent = ob_get_clean();
  231. $this->assertEquals('The response body', $sentContent);
  232. $this->assertEquals(200, $symfonyResponse->getStatusCode());
  233. }
  234. }