ClientTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\HttpKernel\Client;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpFoundation\StreamedResponse;
  15. use Symfony\Component\HttpFoundation\Cookie;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  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. $expected = array(
  52. 'foo=bar; expires=Sun, 15-Feb-2009 20:00:00 GMT; max-age='.(strtotime('Sun, 15-Feb-2009 20:00:00 GMT') - time()).'; path=/foo; domain=http://example.com; secure; httponly',
  53. 'foo1=bar1; expires=Sun, 15-Feb-2009 20:00:00 GMT; max-age='.(strtotime('Sun, 15-Feb-2009 20:00:00 GMT') - time()).'; path=/foo; domain=http://example.com; secure; httponly',
  54. );
  55. $response = new Response();
  56. $response->headers->setCookie(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));
  57. $domResponse = $m->invoke($client, $response);
  58. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  59. $response = new Response();
  60. $response->headers->setCookie(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));
  61. $response->headers->setCookie(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));
  62. $domResponse = $m->invoke($client, $response);
  63. $this->assertEquals($expected[0], $domResponse->getHeader('Set-Cookie'));
  64. $this->assertEquals($expected, $domResponse->getHeader('Set-Cookie', false));
  65. }
  66. public function testFilterResponseSupportsStreamedResponses()
  67. {
  68. $client = new Client(new TestHttpKernel());
  69. $r = new \ReflectionObject($client);
  70. $m = $r->getMethod('filterResponse');
  71. $m->setAccessible(true);
  72. $response = new StreamedResponse(function () {
  73. echo 'foo';
  74. });
  75. $domResponse = $m->invoke($client, $response);
  76. $this->assertEquals('foo', $domResponse->getContent());
  77. }
  78. public function testUploadedFile()
  79. {
  80. $source = tempnam(sys_get_temp_dir(), 'source');
  81. $target = sys_get_temp_dir().'/sf.moved.file';
  82. @unlink($target);
  83. $kernel = new TestHttpKernel();
  84. $client = new Client($kernel);
  85. $files = array(
  86. array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
  87. new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true),
  88. );
  89. $file = null;
  90. foreach ($files as $file) {
  91. $client->request('POST', '/', array(), array('foo' => $file));
  92. $files = $client->getRequest()->files->all();
  93. $this->assertCount(1, $files);
  94. $file = $files['foo'];
  95. $this->assertEquals('original', $file->getClientOriginalName());
  96. $this->assertEquals('mime/original', $file->getClientMimeType());
  97. $this->assertEquals('123', $file->getClientSize());
  98. $this->assertTrue($file->isValid());
  99. }
  100. $file->move(dirname($target), basename($target));
  101. $this->assertFileExists($target);
  102. unlink($target);
  103. }
  104. public function testUploadedFileWhenNoFileSelected()
  105. {
  106. $kernel = new TestHttpKernel();
  107. $client = new Client($kernel);
  108. $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
  109. $client->request('POST', '/', array(), array('foo' => $file));
  110. $files = $client->getRequest()->files->all();
  111. $this->assertCount(1, $files);
  112. $this->assertNull($files['foo']);
  113. }
  114. public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
  115. {
  116. $source = tempnam(sys_get_temp_dir(), 'source');
  117. $kernel = new TestHttpKernel();
  118. $client = new Client($kernel);
  119. $file = $this
  120. ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
  121. ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
  122. ->setMethods(array('getSize'))
  123. ->getMock()
  124. ;
  125. $file->expects($this->once())
  126. ->method('getSize')
  127. ->will($this->returnValue(INF))
  128. ;
  129. $client->request('POST', '/', array(), array($file));
  130. $files = $client->getRequest()->files->all();
  131. $this->assertCount(1, $files);
  132. $file = $files[0];
  133. $this->assertFalse($file->isValid());
  134. $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
  135. $this->assertEquals('mime/original', $file->getClientMimeType());
  136. $this->assertEquals('original', $file->getClientOriginalName());
  137. $this->assertEquals(0, $file->getClientSize());
  138. unlink($source);
  139. }
  140. }