ClientTest.php 7.0 KB

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