Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / http-kernel / Tests / ClientTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests;
13
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;
21
22 /**
23  * @group time-sensitive
24  */
25 class ClientTest extends TestCase
26 {
27     public function testDoRequest()
28     {
29         $client = new Client(new TestHttpKernel());
30
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());
37
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');
41
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');
44     }
45
46     public function testGetScript()
47     {
48         $client = new TestClient(new TestHttpKernel());
49         $client->insulate();
50         $client->request('GET', '/');
51
52         $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
53     }
54
55     public function testFilterResponseConvertsCookies()
56     {
57         $client = new Client(new TestHttpKernel());
58
59         $r = new \ReflectionObject($client);
60         $m = $r->getMethod('filterResponse');
61         $m->setAccessible(true);
62
63         $expected31 = array(
64             'foo=bar; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
65             'foo1=bar1; expires=Sun, 15 Feb 2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly',
66         );
67         $expected33 = array(
68             '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',
69             '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',
70         );
71
72         $response = new Response();
73         $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));
74         $domResponse = $m->invoke($client, $response);
75         try {
76             $this->assertEquals($expected31[0], $domResponse->getHeader('Set-Cookie'));
77         } catch (\PHPUnit\Framework\ExpectationFailedException $e) {
78             $this->assertEquals($expected33[0], $domResponse->getHeader('Set-Cookie'));
79         } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
80             $this->assertEquals($expected33[0], $domResponse->getHeader('Set-Cookie'));
81         }
82
83         $response = new Response();
84         $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));
85         $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));
86         $domResponse = $m->invoke($client, $response);
87         try {
88             $this->assertEquals($expected31[0], $domResponse->getHeader('Set-Cookie'));
89         } catch (\PHPUnit\Framework\ExpectationFailedException $e) {
90             $this->assertEquals($expected33[0], $domResponse->getHeader('Set-Cookie'));
91         } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
92             $this->assertEquals($expected33[0], $domResponse->getHeader('Set-Cookie'));
93         }
94         try {
95             $this->assertEquals($expected31, $domResponse->getHeader('Set-Cookie', false));
96         } catch (\PHPUnit\Framework\ExpectationFailedException $e) {
97             $this->assertEquals($expected33, $domResponse->getHeader('Set-Cookie', false));
98         } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
99             $this->assertEquals($expected33, $domResponse->getHeader('Set-Cookie', false));
100         }
101     }
102
103     public function testFilterResponseSupportsStreamedResponses()
104     {
105         $client = new Client(new TestHttpKernel());
106
107         $r = new \ReflectionObject($client);
108         $m = $r->getMethod('filterResponse');
109         $m->setAccessible(true);
110
111         $response = new StreamedResponse(function () {
112             echo 'foo';
113         });
114
115         $domResponse = $m->invoke($client, $response);
116         $this->assertEquals('foo', $domResponse->getContent());
117     }
118
119     public function testUploadedFile()
120     {
121         $source = tempnam(sys_get_temp_dir(), 'source');
122         $target = sys_get_temp_dir().'/sf.moved.file';
123         @unlink($target);
124
125         $kernel = new TestHttpKernel();
126         $client = new Client($kernel);
127
128         $files = array(
129             array('tmp_name' => $source, 'name' => 'original', 'type' => 'mime/original', 'size' => 123, 'error' => UPLOAD_ERR_OK),
130             new UploadedFile($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true),
131         );
132
133         $file = null;
134         foreach ($files as $file) {
135             $client->request('POST', '/', array(), array('foo' => $file));
136
137             $files = $client->getRequest()->files->all();
138
139             $this->assertCount(1, $files);
140
141             $file = $files['foo'];
142
143             $this->assertEquals('original', $file->getClientOriginalName());
144             $this->assertEquals('mime/original', $file->getClientMimeType());
145             $this->assertEquals('123', $file->getClientSize());
146             $this->assertTrue($file->isValid());
147         }
148
149         $file->move(dirname($target), basename($target));
150
151         $this->assertFileExists($target);
152         unlink($target);
153     }
154
155     public function testUploadedFileWhenNoFileSelected()
156     {
157         $kernel = new TestHttpKernel();
158         $client = new Client($kernel);
159
160         $file = array('tmp_name' => '', 'name' => '', 'type' => '', 'size' => 0, 'error' => UPLOAD_ERR_NO_FILE);
161
162         $client->request('POST', '/', array(), array('foo' => $file));
163
164         $files = $client->getRequest()->files->all();
165
166         $this->assertCount(1, $files);
167         $this->assertNull($files['foo']);
168     }
169
170     public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
171     {
172         $source = tempnam(sys_get_temp_dir(), 'source');
173
174         $kernel = new TestHttpKernel();
175         $client = new Client($kernel);
176
177         $file = $this
178             ->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
179             ->setConstructorArgs(array($source, 'original', 'mime/original', 123, UPLOAD_ERR_OK, true))
180             ->setMethods(array('getSize'))
181             ->getMock()
182         ;
183
184         $file->expects($this->once())
185             ->method('getSize')
186             ->will($this->returnValue(INF))
187         ;
188
189         $client->request('POST', '/', array(), array($file));
190
191         $files = $client->getRequest()->files->all();
192
193         $this->assertCount(1, $files);
194
195         $file = $files[0];
196
197         $this->assertFalse($file->isValid());
198         $this->assertEquals(UPLOAD_ERR_INI_SIZE, $file->getError());
199         $this->assertEquals('mime/original', $file->getClientMimeType());
200         $this->assertEquals('original', $file->getClientOriginalName());
201         $this->assertEquals(0, $file->getClientSize());
202
203         unlink($source);
204     }
205 }