ClientTest.php 30 KB

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