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