Yaffs site version 1.1
[yaffs-website] / vendor / symfony / browser-kit / 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\BrowserKit\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\BrowserKit\Client;
16 use Symfony\Component\BrowserKit\History;
17 use Symfony\Component\BrowserKit\CookieJar;
18 use Symfony\Component\BrowserKit\Response;
19
20 class SpecialResponse extends Response
21 {
22 }
23
24 class TestClient extends Client
25 {
26     protected $nextResponse = null;
27     protected $nextScript = null;
28
29     public function setNextResponse(Response $response)
30     {
31         $this->nextResponse = $response;
32     }
33
34     public function setNextScript($script)
35     {
36         $this->nextScript = $script;
37     }
38
39     protected function doRequest($request)
40     {
41         if (null === $this->nextResponse) {
42             return new Response();
43         }
44
45         $response = $this->nextResponse;
46         $this->nextResponse = null;
47
48         return $response;
49     }
50
51     protected function filterResponse($response)
52     {
53         if ($response instanceof SpecialResponse) {
54             return new Response($response->getContent(), $response->getStatus(), $response->getHeaders());
55         }
56
57         return $response;
58     }
59
60     protected function getScript($request)
61     {
62         $r = new \ReflectionClass('Symfony\Component\BrowserKit\Response');
63         $path = $r->getFileName();
64
65         return <<<EOF
66 <?php
67
68 require_once('$path');
69
70 echo serialize($this->nextScript);
71 EOF;
72     }
73 }
74
75 class ClientTest extends TestCase
76 {
77     public function testGetHistory()
78     {
79         $client = new TestClient(array(), $history = new History());
80         $this->assertSame($history, $client->getHistory(), '->getHistory() returns the History');
81     }
82
83     public function testGetCookieJar()
84     {
85         $client = new TestClient(array(), null, $cookieJar = new CookieJar());
86         $this->assertSame($cookieJar, $client->getCookieJar(), '->getCookieJar() returns the CookieJar');
87     }
88
89     public function testGetRequest()
90     {
91         $client = new TestClient();
92         $client->request('GET', 'http://example.com/');
93
94         $this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
95     }
96
97     public function testGetRequestWithIpAsHttpHost()
98     {
99         $client = new TestClient();
100         $client->request('GET', 'https://example.com/foo', array(), array(), array('HTTP_HOST' => '127.0.0.1'));
101
102         $this->assertEquals('https://example.com/foo', $client->getRequest()->getUri());
103         $headers = $client->getRequest()->getServer();
104         $this->assertEquals('127.0.0.1', $headers['HTTP_HOST']);
105     }
106
107     public function testGetResponse()
108     {
109         $client = new TestClient();
110         $client->setNextResponse(new Response('foo'));
111         $client->request('GET', 'http://example.com/');
112
113         $this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request');
114         $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request');
115     }
116
117     public function testGetInternalResponse()
118     {
119         $client = new TestClient();
120         $client->setNextResponse(new SpecialResponse('foo'));
121         $client->request('GET', 'http://example.com/');
122
123         $this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getInternalResponse());
124         $this->assertNotInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getInternalResponse());
125         $this->assertInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getResponse());
126     }
127
128     public function testGetContent()
129     {
130         $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
131
132         $client = new TestClient();
133         $client->request('POST', 'http://example.com/jsonrpc', array(), array(), array(), $json);
134         $this->assertEquals($json, $client->getRequest()->getContent());
135     }
136
137     public function testGetCrawler()
138     {
139         $client = new TestClient();
140         $client->setNextResponse(new Response('foo'));
141         $crawler = $client->request('GET', 'http://example.com/');
142
143         $this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
144     }
145
146     public function testRequestHttpHeaders()
147     {
148         $client = new TestClient();
149         $client->request('GET', '/');
150         $headers = $client->getRequest()->getServer();
151         $this->assertEquals('localhost', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
152
153         $client = new TestClient();
154         $client->request('GET', 'http://www.example.com');
155         $headers = $client->getRequest()->getServer();
156         $this->assertEquals('www.example.com', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header');
157
158         $client->request('GET', 'https://www.example.com');
159         $headers = $client->getRequest()->getServer();
160         $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header');
161
162         $client = new TestClient();
163         $client->request('GET', 'http://www.example.com:8080');
164         $headers = $client->getRequest()->getServer();
165         $this->assertEquals('www.example.com:8080', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header with port');
166     }
167
168     public function testRequestURIConversion()
169     {
170         $client = new TestClient();
171         $client->request('GET', '/foo');
172         $this->assertEquals('http://localhost/foo', $client->getRequest()->getUri(), '->request() converts the URI to an absolute one');
173
174         $client = new TestClient();
175         $client->request('GET', 'http://www.example.com');
176         $this->assertEquals('http://www.example.com', $client->getRequest()->getUri(), '->request() does not change absolute URIs');
177
178         $client = new TestClient();
179         $client->request('GET', 'http://www.example.com/');
180         $client->request('GET', '/foo');
181         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
182
183         $client = new TestClient();
184         $client->request('GET', 'http://www.example.com/foo');
185         $client->request('GET', '#');
186         $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
187         $client->request('GET', '#');
188         $this->assertEquals('http://www.example.com/foo#', $client->getRequest()->getUri(), '->request() uses the previous request for #');
189         $client->request('GET', '#foo');
190         $this->assertEquals('http://www.example.com/foo#foo', $client->getRequest()->getUri(), '->request() uses the previous request for #');
191
192         $client = new TestClient();
193         $client->request('GET', 'http://www.example.com/foo/');
194         $client->request('GET', 'bar');
195         $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
196
197         $client = new TestClient();
198         $client->request('GET', 'http://www.example.com/foo/foobar');
199         $client->request('GET', 'bar');
200         $this->assertEquals('http://www.example.com/foo/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
201
202         $client = new TestClient();
203         $client->request('GET', 'http://www.example.com/foo/');
204         $client->request('GET', 'http');
205         $this->assertEquals('http://www.example.com/foo/http', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
206
207         $client = new TestClient();
208         $client->request('GET', 'http://www.example.com/foo');
209         $client->request('GET', 'http/bar');
210         $this->assertEquals('http://www.example.com/http/bar', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
211
212         $client = new TestClient();
213         $client->request('GET', 'http://www.example.com/');
214         $client->request('GET', 'http');
215         $this->assertEquals('http://www.example.com/http', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
216
217         $client = new TestClient();
218         $client->request('GET', 'http://www.example.com/foo');
219         $client->request('GET', '?');
220         $this->assertEquals('http://www.example.com/foo?', $client->getRequest()->getUri(), '->request() uses the previous request for ?');
221         $client->request('GET', '?');
222         $this->assertEquals('http://www.example.com/foo?', $client->getRequest()->getUri(), '->request() uses the previous request for ?');
223         $client->request('GET', '?foo=bar');
224         $this->assertEquals('http://www.example.com/foo?foo=bar', $client->getRequest()->getUri(), '->request() uses the previous request for ?');
225     }
226
227     public function testRequestReferer()
228     {
229         $client = new TestClient();
230         $client->request('GET', 'http://www.example.com/foo/foobar');
231         $client->request('GET', 'bar');
232         $server = $client->getRequest()->getServer();
233         $this->assertEquals('http://www.example.com/foo/foobar', $server['HTTP_REFERER'], '->request() sets the referer');
234     }
235
236     public function testRequestHistory()
237     {
238         $client = new TestClient();
239         $client->request('GET', 'http://www.example.com/foo/foobar');
240         $client->request('GET', 'bar');
241
242         $this->assertEquals('http://www.example.com/foo/bar', $client->getHistory()->current()->getUri(), '->request() updates the History');
243         $this->assertEquals('http://www.example.com/foo/foobar', $client->getHistory()->back()->getUri(), '->request() updates the History');
244     }
245
246     public function testRequestCookies()
247     {
248         $client = new TestClient();
249         $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array('Set-Cookie' => 'foo=bar')));
250         $client->request('GET', 'http://www.example.com/foo/foobar');
251         $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
252
253         $client->request('GET', 'bar');
254         $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
255     }
256
257     public function testRequestSecureCookies()
258     {
259         $client = new TestClient();
260         $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>', 200, array('Set-Cookie' => 'foo=bar; path=/; secure')));
261         $client->request('GET', 'https://www.example.com/foo/foobar');
262
263         $this->assertTrue($client->getCookieJar()->get('foo', '/', 'www.example.com')->isSecure());
264     }
265
266     public function testClick()
267     {
268         $client = new TestClient();
269         $client->setNextResponse(new Response('<html><a href="/foo">foo</a></html>'));
270         $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
271
272         $client->click($crawler->filter('a')->link());
273
274         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
275     }
276
277     public function testClickForm()
278     {
279         $client = new TestClient();
280         $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
281         $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
282
283         $client->click($crawler->filter('input')->form());
284
285         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() Form submit forms');
286     }
287
288     public function testSubmit()
289     {
290         $client = new TestClient();
291         $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
292         $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
293
294         $client->submit($crawler->filter('input')->form());
295
296         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
297     }
298
299     public function testSubmitPreserveAuth()
300     {
301         $client = new TestClient(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'bar'));
302         $client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
303         $crawler = $client->request('GET', 'http://www.example.com/foo/foobar');
304
305         $server = $client->getRequest()->getServer();
306         $this->assertArrayHasKey('PHP_AUTH_USER', $server);
307         $this->assertEquals('foo', $server['PHP_AUTH_USER']);
308         $this->assertArrayHasKey('PHP_AUTH_PW', $server);
309         $this->assertEquals('bar', $server['PHP_AUTH_PW']);
310
311         $client->submit($crawler->filter('input')->form());
312
313         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
314
315         $server = $client->getRequest()->getServer();
316         $this->assertArrayHasKey('PHP_AUTH_USER', $server);
317         $this->assertEquals('foo', $server['PHP_AUTH_USER']);
318         $this->assertArrayHasKey('PHP_AUTH_PW', $server);
319         $this->assertEquals('bar', $server['PHP_AUTH_PW']);
320     }
321
322     public function testFollowRedirect()
323     {
324         $client = new TestClient();
325         $client->followRedirects(false);
326         $client->request('GET', 'http://www.example.com/foo/foobar');
327
328         try {
329             $client->followRedirect();
330             $this->fail('->followRedirect() throws a \LogicException if the request was not redirected');
331         } catch (\Exception $e) {
332             $this->assertInstanceOf('LogicException', $e, '->followRedirect() throws a \LogicException if the request was not redirected');
333         }
334
335         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
336         $client->request('GET', 'http://www.example.com/foo/foobar');
337         $client->followRedirect();
338
339         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
340
341         $client = new TestClient();
342         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
343         $client->request('GET', 'http://www.example.com/foo/foobar');
344
345         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
346
347         $client = new TestClient();
348         $client->setNextResponse(new Response('', 201, array('Location' => 'http://www.example.com/redirected')));
349         $client->request('GET', 'http://www.example.com/foo/foobar');
350
351         $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->followRedirect() does not follow redirect if HTTP Code is not 30x');
352
353         $client = new TestClient();
354         $client->setNextResponse(new Response('', 201, array('Location' => 'http://www.example.com/redirected')));
355         $client->followRedirects(false);
356         $client->request('GET', 'http://www.example.com/foo/foobar');
357
358         try {
359             $client->followRedirect();
360             $this->fail('->followRedirect() throws a \LogicException if the request did not respond with 30x HTTP Code');
361         } catch (\Exception $e) {
362             $this->assertInstanceOf('LogicException', $e, '->followRedirect() throws a \LogicException if the request did not respond with 30x HTTP Code');
363         }
364     }
365
366     public function testFollowRelativeRedirect()
367     {
368         $client = new TestClient();
369         $client->setNextResponse(new Response('', 302, array('Location' => '/redirected')));
370         $client->request('GET', 'http://www.example.com/foo/foobar');
371         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
372
373         $client = new TestClient();
374         $client->setNextResponse(new Response('', 302, array('Location' => '/redirected:1234')));
375         $client->request('GET', 'http://www.example.com/foo/foobar');
376         $this->assertEquals('http://www.example.com/redirected:1234', $client->getRequest()->getUri(), '->followRedirect() follows relative urls');
377     }
378
379     public function testFollowRedirectWithMaxRedirects()
380     {
381         $client = new TestClient();
382         $client->setMaxRedirects(1);
383         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
384         $client->request('GET', 'http://www.example.com/foo/foobar');
385         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
386
387         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected2')));
388         try {
389             $client->followRedirect();
390             $this->fail('->followRedirect() throws a \LogicException if the request was redirected and limit of redirections was reached');
391         } catch (\Exception $e) {
392             $this->assertInstanceOf('LogicException', $e, '->followRedirect() throws a \LogicException if the request was redirected and limit of redirections was reached');
393         }
394
395         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
396         $client->request('GET', 'http://www.example.com/foo/foobar');
397         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
398
399         $client->setNextResponse(new Response('', 302, array('Location' => '/redirected')));
400         $client->request('GET', 'http://www.example.com/foo/foobar');
401
402         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows relative URLs');
403
404         $client = new TestClient();
405         $client->setNextResponse(new Response('', 302, array('Location' => '//www.example.org/')));
406         $client->request('GET', 'https://www.example.com/');
407
408         $this->assertEquals('https://www.example.org/', $client->getRequest()->getUri(), '->followRedirect() follows protocol-relative URLs');
409
410         $client = new TestClient();
411         $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected')));
412         $client->request('POST', 'http://www.example.com/foo/foobar', array('name' => 'bar'));
413
414         $this->assertEquals('GET', $client->getRequest()->getMethod(), '->followRedirect() uses a GET for 302');
415         $this->assertEquals(array(), $client->getRequest()->getParameters(), '->followRedirect() does not submit parameters when changing the method');
416     }
417
418     public function testFollowRedirectWithCookies()
419     {
420         $client = new TestClient();
421         $client->followRedirects(false);
422         $client->setNextResponse(new Response('', 302, array(
423             'Location' => 'http://www.example.com/redirected',
424             'Set-Cookie' => 'foo=bar',
425         )));
426         $client->request('GET', 'http://www.example.com/');
427         $this->assertEquals(array(), $client->getRequest()->getCookies());
428         $client->followRedirect();
429         $this->assertEquals(array('foo' => 'bar'), $client->getRequest()->getCookies());
430     }
431
432     public function testFollowRedirectWithHeaders()
433     {
434         $headers = array(
435             'HTTP_HOST' => 'www.example.com',
436             'HTTP_USER_AGENT' => 'Symfony BrowserKit',
437             'CONTENT_TYPE' => 'application/vnd.custom+xml',
438             'HTTPS' => false,
439         );
440
441         $client = new TestClient();
442         $client->followRedirects(false);
443         $client->setNextResponse(new Response('', 302, array(
444             'Location' => 'http://www.example.com/redirected',
445         )));
446         $client->request('GET', 'http://www.example.com/', array(), array(), array(
447             'CONTENT_TYPE' => 'application/vnd.custom+xml',
448         ));
449
450         $this->assertEquals($headers, $client->getRequest()->getServer());
451
452         $client->followRedirect();
453
454         $headers['HTTP_REFERER'] = 'http://www.example.com/';
455
456         $this->assertEquals($headers, $client->getRequest()->getServer());
457     }
458
459     public function testFollowRedirectWithPort()
460     {
461         $headers = array(
462             'HTTP_HOST' => 'www.example.com:8080',
463             'HTTP_USER_AGENT' => 'Symfony BrowserKit',
464             'HTTPS' => false,
465             'HTTP_REFERER' => 'http://www.example.com:8080/',
466         );
467
468         $client = new TestClient();
469         $client->setNextResponse(new Response('', 302, array(
470             'Location' => 'http://www.example.com:8080/redirected',
471         )));
472         $client->request('GET', 'http://www.example.com:8080/');
473
474         $this->assertEquals($headers, $client->getRequest()->getServer());
475     }
476
477     public function testIsFollowingRedirects()
478     {
479         $client = new TestClient();
480         $this->assertTrue($client->isFollowingRedirects(), '->getFollowRedirects() returns default value');
481         $client->followRedirects(false);
482         $this->assertFalse($client->isFollowingRedirects(), '->getFollowRedirects() returns assigned value');
483     }
484
485     public function testGetMaxRedirects()
486     {
487         $client = new TestClient();
488         $this->assertEquals(-1, $client->getMaxRedirects(), '->getMaxRedirects() returns default value');
489         $client->setMaxRedirects(3);
490         $this->assertEquals(3, $client->getMaxRedirects(), '->getMaxRedirects() returns assigned value');
491     }
492
493     public function testFollowRedirectWithPostMethod()
494     {
495         $parameters = array('foo' => 'bar');
496         $files = array('myfile.foo' => 'baz');
497         $server = array('X_TEST_FOO' => 'bazbar');
498         $content = 'foobarbaz';
499
500         $client = new TestClient();
501
502         $client->setNextResponse(new Response('', 307, array('Location' => 'http://www.example.com/redirected')));
503         $client->request('POST', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
504
505         $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect with POST method');
506         $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->followRedirect() keeps parameters with POST method');
507         $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->followRedirect() keeps files with POST method');
508         $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->followRedirect() keeps $_SERVER with POST method');
509         $this->assertEquals($content, $client->getRequest()->getContent(), '->followRedirect() keeps content with POST method');
510         $this->assertEquals('POST', $client->getRequest()->getMethod(), '->followRedirect() keeps request method');
511     }
512
513     public function testFollowRedirectDropPostMethod()
514     {
515         $parameters = array('foo' => 'bar');
516         $files = array('myfile.foo' => 'baz');
517         $server = array('X_TEST_FOO' => 'bazbar');
518         $content = 'foobarbaz';
519
520         $client = new TestClient();
521
522         foreach (array(301, 302, 303) as $code) {
523             $client->setNextResponse(new Response('', $code, array('Location' => 'http://www.example.com/redirected')));
524             $client->request('POST', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
525
526             $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect with POST method on response code: '.$code.'.');
527             $this->assertEmpty($client->getRequest()->getParameters(), '->followRedirect() drops parameters with POST method on response code: '.$code.'.');
528             $this->assertEmpty($client->getRequest()->getFiles(), '->followRedirect() drops files with POST method on response code: '.$code.'.');
529             $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->followRedirect() keeps $_SERVER with POST method on response code: '.$code.'.');
530             $this->assertEmpty($client->getRequest()->getContent(), '->followRedirect() drops content with POST method on response code: '.$code.'.');
531             $this->assertEquals('GET', $client->getRequest()->getMethod(), '->followRedirect() drops request method to GET on response code: '.$code.'.');
532         }
533     }
534
535     public function testBack()
536     {
537         $client = new TestClient();
538
539         $parameters = array('foo' => 'bar');
540         $files = array('myfile.foo' => 'baz');
541         $server = array('X_TEST_FOO' => 'bazbar');
542         $content = 'foobarbaz';
543
544         $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
545         $client->request('GET', 'http://www.example.com/foo');
546         $client->back();
547
548         $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->back() goes back in the history');
549         $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->back() keeps parameters');
550         $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->back() keeps files');
551         $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->back() keeps $_SERVER');
552         $this->assertEquals($content, $client->getRequest()->getContent(), '->back() keeps content');
553     }
554
555     public function testForward()
556     {
557         $client = new TestClient();
558
559         $parameters = array('foo' => 'bar');
560         $files = array('myfile.foo' => 'baz');
561         $server = array('X_TEST_FOO' => 'bazbar');
562         $content = 'foobarbaz';
563
564         $client->request('GET', 'http://www.example.com/foo/foobar');
565         $client->request('GET', 'http://www.example.com/foo', $parameters, $files, $server, $content);
566         $client->back();
567         $client->forward();
568
569         $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->forward() goes forward in the history');
570         $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->forward() keeps parameters');
571         $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->forward() keeps files');
572         $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->forward() keeps $_SERVER');
573         $this->assertEquals($content, $client->getRequest()->getContent(), '->forward() keeps content');
574     }
575
576     public function testReload()
577     {
578         $client = new TestClient();
579
580         $parameters = array('foo' => 'bar');
581         $files = array('myfile.foo' => 'baz');
582         $server = array('X_TEST_FOO' => 'bazbar');
583         $content = 'foobarbaz';
584
585         $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
586         $client->reload();
587
588         $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->reload() reloads the current page');
589         $this->assertArrayHasKey('foo', $client->getRequest()->getParameters(), '->reload() keeps parameters');
590         $this->assertArrayHasKey('myfile.foo', $client->getRequest()->getFiles(), '->reload() keeps files');
591         $this->assertArrayHasKey('X_TEST_FOO', $client->getRequest()->getServer(), '->reload() keeps $_SERVER');
592         $this->assertEquals($content, $client->getRequest()->getContent(), '->reload() keeps content');
593     }
594
595     public function testRestart()
596     {
597         $client = new TestClient();
598         $client->request('GET', 'http://www.example.com/foo/foobar');
599         $client->restart();
600
601         $this->assertTrue($client->getHistory()->isEmpty(), '->restart() clears the history');
602         $this->assertEquals(array(), $client->getCookieJar()->all(), '->restart() clears the cookies');
603     }
604
605     public function testInsulatedRequests()
606     {
607         $client = new TestClient();
608         $client->insulate();
609         $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar')");
610         $client->request('GET', 'http://www.example.com/foo/foobar');
611
612         $this->assertEquals('foobar', $client->getResponse()->getContent(), '->insulate() process the request in a forked process');
613
614         $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar)");
615
616         try {
617             $client->request('GET', 'http://www.example.com/foo/foobar');
618             $this->fail('->request() throws a \RuntimeException if the script has an error');
619         } catch (\Exception $e) {
620             $this->assertInstanceOf('RuntimeException', $e, '->request() throws a \RuntimeException if the script has an error');
621         }
622     }
623
624     public function testGetServerParameter()
625     {
626         $client = new TestClient();
627         $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
628         $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
629         $this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
630     }
631
632     public function testSetServerParameter()
633     {
634         $client = new TestClient();
635
636         $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
637         $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
638
639         $client->setServerParameter('HTTP_HOST', 'testhost');
640         $this->assertEquals('testhost', $client->getServerParameter('HTTP_HOST'));
641
642         $client->setServerParameter('HTTP_USER_AGENT', 'testua');
643         $this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
644     }
645
646     public function testSetServerParameterInRequest()
647     {
648         $client = new TestClient();
649
650         $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
651         $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
652
653         $client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
654             'HTTP_HOST' => 'testhost',
655             'HTTP_USER_AGENT' => 'testua',
656             'HTTPS' => false,
657             'NEW_SERVER_KEY' => 'new-server-key-value',
658         ));
659
660         $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
661         $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
662
663         $this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
664
665         $server = $client->getRequest()->getServer();
666
667         $this->assertArrayHasKey('HTTP_USER_AGENT', $server);
668         $this->assertEquals('testua', $server['HTTP_USER_AGENT']);
669
670         $this->assertArrayHasKey('HTTP_HOST', $server);
671         $this->assertEquals('testhost', $server['HTTP_HOST']);
672
673         $this->assertArrayHasKey('NEW_SERVER_KEY', $server);
674         $this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']);
675
676         $this->assertArrayHasKey('HTTPS', $server);
677         $this->assertFalse($server['HTTPS']);
678     }
679
680     public function testInternalRequest()
681     {
682         $client = new TestClient();
683
684         $client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
685             'HTTP_HOST' => 'testhost',
686             'HTTP_USER_AGENT' => 'testua',
687             'HTTPS' => false,
688             'NEW_SERVER_KEY' => 'new-server-key-value',
689         ));
690
691         $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
692     }
693
694     public function testInternalRequestNull()
695     {
696         $client = new TestClient();
697         $this->assertNull($client->getInternalRequest());
698     }
699 }