ClientTest.php 31 KB

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