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\BrowserKit\Tests;
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;
20 class SpecialResponse extends Response
24 class TestClient extends Client
26 protected $nextResponse = null;
27 protected $nextScript = null;
29 public function setNextResponse(Response $response)
31 $this->nextResponse = $response;
34 public function setNextScript($script)
36 $this->nextScript = $script;
39 protected function doRequest($request)
41 if (null === $this->nextResponse) {
42 return new Response();
45 $response = $this->nextResponse;
46 $this->nextResponse = null;
51 protected function filterResponse($response)
53 if ($response instanceof SpecialResponse) {
54 return new Response($response->getContent(), $response->getStatus(), $response->getHeaders());
60 protected function getScript($request)
62 $r = new \ReflectionClass('Symfony\Component\BrowserKit\Response');
63 $path = $r->getFileName();
68 require_once('$path');
70 echo serialize($this->nextScript);
75 class ClientTest extends TestCase
77 public function testGetHistory()
79 $client = new TestClient(array(), $history = new History());
80 $this->assertSame($history, $client->getHistory(), '->getHistory() returns the History');
83 public function testGetCookieJar()
85 $client = new TestClient(array(), null, $cookieJar = new CookieJar());
86 $this->assertSame($cookieJar, $client->getCookieJar(), '->getCookieJar() returns the CookieJar');
89 public function testGetRequest()
91 $client = new TestClient();
92 $client->request('GET', 'http://example.com/');
94 $this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
97 public function testGetRequestWithIpAsHttpHost()
99 $client = new TestClient();
100 $client->request('GET', 'https://example.com/foo', array(), array(), array('HTTP_HOST' => '127.0.0.1'));
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']);
107 public function testGetResponse()
109 $client = new TestClient();
110 $client->setNextResponse(new Response('foo'));
111 $client->request('GET', 'http://example.com/');
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');
117 public function testGetInternalResponse()
119 $client = new TestClient();
120 $client->setNextResponse(new SpecialResponse('foo'));
121 $client->request('GET', 'http://example.com/');
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());
128 public function testGetContent()
130 $json = '{"jsonrpc":"2.0","method":"echo","id":7,"params":["Hello World"]}';
132 $client = new TestClient();
133 $client->request('POST', 'http://example.com/jsonrpc', array(), array(), array(), $json);
134 $this->assertEquals($json, $client->getRequest()->getContent());
137 public function testGetCrawler()
139 $client = new TestClient();
140 $client->setNextResponse(new Response('foo'));
141 $crawler = $client->request('GET', 'http://example.com/');
143 $this->assertSame($crawler, $client->getCrawler(), '->getCrawler() returns the Crawler of the last request');
146 public function testRequestHttpHeaders()
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');
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');
158 $client->request('GET', 'https://www.example.com');
159 $headers = $client->getRequest()->getServer();
160 $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header');
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');
168 public function testRequestURIConversion()
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');
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');
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');
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 #');
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');
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');
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');
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');
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');
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 ?');
227 public function testRequestReferer()
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');
236 public function testRequestHistory()
238 $client = new TestClient();
239 $client->request('GET', 'http://www.example.com/foo/foobar');
240 $client->request('GET', 'bar');
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');
246 public function testRequestCookies()
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');
253 $client->request('GET', 'bar');
254 $this->assertEquals(array('foo' => 'bar'), $client->getCookieJar()->allValues('http://www.example.com/foo/foobar'), '->request() updates the CookieJar');
257 public function testRequestSecureCookies()
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');
263 $this->assertTrue($client->getCookieJar()->get('foo', '/', 'www.example.com')->isSecure());
266 public function testClick()
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');
272 $client->click($crawler->filter('a')->link());
274 $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() clicks on links');
277 public function testClickForm()
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');
283 $client->click($crawler->filter('input')->form());
285 $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->click() Form submit forms');
288 public function testSubmit()
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');
294 $client->submit($crawler->filter('input')->form());
296 $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
299 public function testSubmitPreserveAuth()
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');
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']);
311 $client->submit($crawler->filter('input')->form());
313 $this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->submit() submit forms');
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']);
322 public function testFollowRedirect()
324 $client = new TestClient();
325 $client->followRedirects(false);
326 $client->request('GET', 'http://www.example.com/foo/foobar');
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');
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();
339 $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
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');
345 $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
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');
351 $this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->followRedirect() does not follow redirect if HTTP Code is not 30x');
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');
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');
366 public function testFollowRelativeRedirect()
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');
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');
379 public function testFollowRedirectWithMaxRedirects()
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');
387 $client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected2')));
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');
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');
399 $client->setNextResponse(new Response('', 302, array('Location' => '/redirected')));
400 $client->request('GET', 'http://www.example.com/foo/foobar');
402 $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows relative URLs');
404 $client = new TestClient();
405 $client->setNextResponse(new Response('', 302, array('Location' => '//www.example.org/')));
406 $client->request('GET', 'https://www.example.com/');
408 $this->assertEquals('https://www.example.org/', $client->getRequest()->getUri(), '->followRedirect() follows protocol-relative URLs');
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'));
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');
418 public function testFollowRedirectWithCookies()
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',
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());
432 public function testFollowRedirectWithHeaders()
435 'HTTP_HOST' => 'www.example.com',
436 'HTTP_USER_AGENT' => 'Symfony BrowserKit',
437 'CONTENT_TYPE' => 'application/vnd.custom+xml',
441 $client = new TestClient();
442 $client->followRedirects(false);
443 $client->setNextResponse(new Response('', 302, array(
444 'Location' => 'http://www.example.com/redirected',
446 $client->request('GET', 'http://www.example.com/', array(), array(), array(
447 'CONTENT_TYPE' => 'application/vnd.custom+xml',
450 $this->assertEquals($headers, $client->getRequest()->getServer());
452 $client->followRedirect();
454 $headers['HTTP_REFERER'] = 'http://www.example.com/';
456 $this->assertEquals($headers, $client->getRequest()->getServer());
459 public function testFollowRedirectWithPort()
462 'HTTP_HOST' => 'www.example.com:8080',
463 'HTTP_USER_AGENT' => 'Symfony BrowserKit',
465 'HTTP_REFERER' => 'http://www.example.com:8080/',
468 $client = new TestClient();
469 $client->setNextResponse(new Response('', 302, array(
470 'Location' => 'http://www.example.com:8080/redirected',
472 $client->request('GET', 'http://www.example.com:8080/');
474 $this->assertEquals($headers, $client->getRequest()->getServer());
477 public function testIsFollowingRedirects()
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');
485 public function testGetMaxRedirects()
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');
493 public function testFollowRedirectWithPostMethod()
495 $parameters = array('foo' => 'bar');
496 $files = array('myfile.foo' => 'baz');
497 $server = array('X_TEST_FOO' => 'bazbar');
498 $content = 'foobarbaz';
500 $client = new TestClient();
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);
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');
513 public function testFollowRedirectDropPostMethod()
515 $parameters = array('foo' => 'bar');
516 $files = array('myfile.foo' => 'baz');
517 $server = array('X_TEST_FOO' => 'bazbar');
518 $content = 'foobarbaz';
520 $client = new TestClient();
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);
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.'.');
535 public function testBack()
537 $client = new TestClient();
539 $parameters = array('foo' => 'bar');
540 $files = array('myfile.foo' => 'baz');
541 $server = array('X_TEST_FOO' => 'bazbar');
542 $content = 'foobarbaz';
544 $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
545 $client->request('GET', 'http://www.example.com/foo');
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');
555 public function testForward()
557 $client = new TestClient();
559 $parameters = array('foo' => 'bar');
560 $files = array('myfile.foo' => 'baz');
561 $server = array('X_TEST_FOO' => 'bazbar');
562 $content = 'foobarbaz';
564 $client->request('GET', 'http://www.example.com/foo/foobar');
565 $client->request('GET', 'http://www.example.com/foo', $parameters, $files, $server, $content);
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');
576 public function testReload()
578 $client = new TestClient();
580 $parameters = array('foo' => 'bar');
581 $files = array('myfile.foo' => 'baz');
582 $server = array('X_TEST_FOO' => 'bazbar');
583 $content = 'foobarbaz';
585 $client->request('GET', 'http://www.example.com/foo/foobar', $parameters, $files, $server, $content);
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');
595 public function testRestart()
597 $client = new TestClient();
598 $client->request('GET', 'http://www.example.com/foo/foobar');
601 $this->assertTrue($client->getHistory()->isEmpty(), '->restart() clears the history');
602 $this->assertEquals(array(), $client->getCookieJar()->all(), '->restart() clears the cookies');
605 public function testInsulatedRequests()
607 $client = new TestClient();
609 $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar')");
610 $client->request('GET', 'http://www.example.com/foo/foobar');
612 $this->assertEquals('foobar', $client->getResponse()->getContent(), '->insulate() process the request in a forked process');
614 $client->setNextScript("new Symfony\Component\BrowserKit\Response('foobar)");
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');
624 public function testGetServerParameter()
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'));
632 public function testSetServerParameter()
634 $client = new TestClient();
636 $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
637 $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
639 $client->setServerParameter('HTTP_HOST', 'testhost');
640 $this->assertEquals('testhost', $client->getServerParameter('HTTP_HOST'));
642 $client->setServerParameter('HTTP_USER_AGENT', 'testua');
643 $this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT'));
646 public function testSetServerParameterInRequest()
648 $client = new TestClient();
650 $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
651 $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
653 $client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
654 'HTTP_HOST' => 'testhost',
655 'HTTP_USER_AGENT' => 'testua',
657 'NEW_SERVER_KEY' => 'new-server-key-value',
660 $this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
661 $this->assertEquals('Symfony BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
663 $this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
665 $server = $client->getRequest()->getServer();
667 $this->assertArrayHasKey('HTTP_USER_AGENT', $server);
668 $this->assertEquals('testua', $server['HTTP_USER_AGENT']);
670 $this->assertArrayHasKey('HTTP_HOST', $server);
671 $this->assertEquals('testhost', $server['HTTP_HOST']);
673 $this->assertArrayHasKey('NEW_SERVER_KEY', $server);
674 $this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']);
676 $this->assertArrayHasKey('HTTPS', $server);
677 $this->assertFalse($server['HTTPS']);
680 public function testInternalRequest()
682 $client = new TestClient();
684 $client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
685 'HTTP_HOST' => 'testhost',
686 'HTTP_USER_AGENT' => 'testua',
688 'NEW_SERVER_KEY' => 'new-server-key-value',
691 $this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getInternalRequest());
694 public function testInternalRequestNull()
696 $client = new TestClient();
697 $this->assertNull($client->getInternalRequest());