4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\Client;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\HttpFoundation\StreamedResponse;
18 use Symfony\Component\HttpFoundation\Cookie;
19 use Symfony\Component\HttpFoundation\File\UploadedFile;
20 use Symfony\Component\HttpKernel\Tests\Fixtures\TestClient;
23 * @group time-sensitive
25 class ClientTest extends TestCase
27 public function testDoRequest()
29 $client = new Client(new TestHttpKernel());
31 $client->request('GET', '/');
32 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
33 $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
34 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getRequest());
35 $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
36 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getResponse());
38 $client->request('GET', 'http://www.example.com/');
39 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
40 $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
42 $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
43 $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
46 public function testGetScript()
48 $client = new TestClient(new TestHttpKernel());
50 $client->request('GET', '/');
52 $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
55 public function testFilterResponseConvertsCookies()
57 $client = new Client(new TestHttpKernel());
59 $r = new \ReflectionObject($client);
60 $m = $r->getMethod('filterResponse');
61 $m->setAccessible(true);
63 $response = new Response();
64 $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));
65 $domResponse = $m->invoke($client, $response);
66 $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
68 $response = new Response();
69 $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));
70 $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));
71 $domResponse = $m->invoke($client, $response);
72 $this->assertSame((string) $cookie1, $domResponse->getHeader('Set-Cookie'));
73 $this->assertSame(array((string) $cookie1, (string) $cookie2), $domResponse->getHeader('Set-Cookie', false));
76 public function testFilterResponseSupportsStreamedResponses()
78 $client = new Client(new TestHttpKernel());
80 $r = new \ReflectionObject($client);
81 $m = $r->getMethod('filterResponse');
82 $m->setAccessible(true);
84 $response = new StreamedResponse(function () {
88 $domResponse = $m->invoke($client, $response);
89 $this->assertEquals('foo', $domResponse->getContent());
92 public function testUploadedFile()
94 $source = tempnam(sys_get_temp_dir(), 'source');
95 file_put_contents($source, '1');
96 $target = sys_get_temp_dir().'/sf.moved.file';
99 $kernel = new TestHttpKernel();
100 $client = new Client($kernel);
103 array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 1, 'error' => UPLOAD_ERR_OK),
104 new UploadedFile($source, 'original', 'mime/original', 1, UPLOAD_ERR_OK, true),
108 foreach ($files as $file) {
109 $client->request('POST', '/', array(), array('foo' => $file));
111 $files = $client->getRequest()->files->all();
113 $this->assertCount(1, $files);
115 $file = $files['foo'];
117 $this->assertEquals('original', $file->getClientOriginalName());
118 $this->assertEquals('mime/original', $file->getClientMimeType());
119 $this->assertSame(1, $file->getClientSize());
120 $this->assertTrue($file->isValid());
123 $file->move(dirname($target), basename($target));
125 $this->assertFileExists($target);
129 public function testUploadedFileWhenNoFileSelected()
131 $kernel = new TestHttpKernel();
132 $client = new Client($kernel);
134 $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
136 $client->request('POST', '/', array(), array('foo' => $file));
138 $files = $client->getRequest()->files->all();
140 $this->assertCount(1, $files);
141 $this->assertNull($files['foo']);
144 public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
146 $source = tempnam(sys_get_temp_dir(), 'source');
148 $kernel = new TestHttpKernel();
149 $client = new Client($kernel);
152 ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
153 ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
154 ->setMethods(array('getSize'))
158 $file->expects($this->once())
160 ->will($this->returnValue(INF))
163 $client->request('POST', '/', array(), array($file));
165 $files = $client->getRequest()->files->all();
167 $this->assertCount(1, $files);
171 $this->assertFalse($file->isValid());
172 $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
173 $this->assertEquals('mime/original', $file->getClientMimeType());
174 $this->assertEquals('original', $file->getClientOriginalName());
175 $this->assertEquals(0, $file->getClientSize());