ClientTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\Component\HttpKernel\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\StreamedResponse;
  16. use Symfony\Component\HttpKernel\Client;
  17. use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;
  18. /**
  19. * @group time-sensitive
  20. */
  21. class ClientTest extends TestCase
  22. {
  23. public function testDoRequest()
  24. {
  25. $client = new Client(new TestHttpKernel());
  26. $client->request('GET', '/');
  27. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  28. $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
  29. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
  30. $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
  31. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
  32. $client->request('GET', 'http://www.example.com/');
  33. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  34. $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
  35. $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
  36. $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
  37. }
  38. public function testGetScript()
  39. {
  40. $client = new TestClient(new TestHttpKernel());
  41. $client->insulate();
  42. $client->request('GET', '/');
  43. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
  44. }
  45. public function testFilterResponseConvertsCookies()
  46. {
  47. $client = new Client(new TestHttpKernel());
  48. $r = new \ReflectionObject($client);
  49. $m = $r->getMethod('filterResponse');
  50. $m->setAccessible(true);
  51. $response = new Response();
  52. $response->headers->setCookie($cookie1 = new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
  53. $domResponse = $m->invoke($client, $response);
  54. $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
  55. $response = new Response();
  56. $response->headers->setCookie($cookie1 = new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
  57. $response->headers->setCookie($cookie2 = new Cookie('foo1', 'bar1', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true, false, null));
  58. $domResponse = $m->invoke($client, $response);
  59. $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
  60. $this->assertSame([(string) $cookie1, (string) $cookie2], $domResponse->getHeader('Set-Cookie', false));
  61. }
  62. public function testFilterResponseSupportsStreamedResponses()
  63. {
  64. $client = new Client(new TestHttpKernel());
  65. $r = new \ReflectionObject($client);
  66. $m = $r->getMethod('filterResponse');
  67. $m->setAccessible(true);
  68. $response = new StreamedResponse(function () {
  69. echo 'foo';
  70. });
  71. $domResponse = $m->invoke($client, $response);
  72. $this->assertEquals('foo', $domResponse->getContent());
  73. }
  74. public function testUploadedFile()
  75. {
  76. $source = tempnam(sys_get_temp_dir(), 'source');
  77. file_put_contents($source, '1');
  78. $target = sys_get_temp_dir().'/sf.moved.file';
  79. @unlink($target);
  80. $kernel = new TestHttpKernel();
  81. $client = new Client($kernel);
  82. $files = [
  83. ['tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => null, 'error' => UPLOAD_ERR_OK],
  84. new UploadedFile($source, 'original', 'mime/original', UPLOAD_ERR_OK, true),
  85. ];
  86. $file = null;
  87. foreach ($files as $file) {
  88. $client->request('POST', '/', [], ['foo' => $file]);
  89. $files = $client->getRequest()->files->all();
  90. $this->assertCount(1, $files);
  91. $file = $files['foo'];
  92. $this->assertEquals('original', $file->getClientOriginalName());
  93. $this->assertEquals('mime/original', $file->getClientMimeType());
  94. $this->assertEquals(1, $file->getSize());
  95. }
  96. $file->move(\dirname($target), basename($target));
  97. $this->assertFileExists($target);
  98. unlink($target);
  99. }
  100. public function testUploadedFileWhenNoFileSelected()
  101. {
  102. $kernel = new TestHttpKernel();
  103. $client = new Client($kernel);
  104. $file = ['tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE];
  105. $client->request('POST', '/', [], ['foo' => $file]);
  106. $files = $client->getRequest()->files->all();
  107. $this->assertCount(1, $files);
  108. $this->assertNull($files['foo']);
  109. }
  110. public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
  111. {
  112. $source = tempnam(sys_get_temp_dir(), 'source');
  113. $kernel = new TestHttpKernel();
  114. $client = new Client($kernel);
  115. $file = $this
  116. ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
  117. ->setConstructorArgs([$source, 'original', 'mime/original', UPLOAD_ERR_OK, true])
  118. ->setMethods(['getSize', 'getClientSize'])
  119. ->getMock()
  120. ;
  121. /* should be modified when the getClientSize will be removed */
  122. $file->expects($this->any())
  123. ->method('getSize')
  124. ->will($this->returnValue(INF))
  125. ;
  126. $file->expects($this->any())
  127. ->method('getClientSize')
  128. ->will($this->returnValue(INF))
  129. ;
  130. $client->request('POST', '/', [], [$file]);
  131. $files = $client->getRequest()->files->all();
  132. $this->assertCount(1, $files);
  133. $file = $files[0];
  134. $this->assertFalse($file->isValid());
  135. $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
  136. $this->assertEquals('mime/original', $file->getClientMimeType());
  137. $this->assertEquals('original', $file->getClientOriginalName());
  138. $this->assertEquals(0, $file->getSize());
  139. unlink($source);
  140. }
  141. }