Açıklama Yok

ClientTest.php 8.0KB

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