ClientTest.php 42 KB

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