AbstractBrowserTest.php 43 KB

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