HttpFoundationFactoryTest.php 9.0 KB

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