ClientTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Http;
  15. use Cake\Http\Client;
  16. use Cake\Http\Client\Request;
  17. use Cake\Http\Client\Response;
  18. use Cake\Http\Cookie\Cookie;
  19. use Cake\Http\Cookie\CookieCollection;
  20. use Cake\TestSuite\TestCase;
  21. use InvalidArgumentException;
  22. /**
  23. * HTTP client test.
  24. */
  25. class ClientTest extends TestCase
  26. {
  27. /**
  28. * Test storing config options and modifying them.
  29. *
  30. * @return void
  31. */
  32. public function testConstructConfig()
  33. {
  34. $config = [
  35. 'scheme' => 'http',
  36. 'host' => 'example.org',
  37. ];
  38. $http = new Client($config);
  39. $result = $http->getConfig();
  40. foreach ($config as $key => $val) {
  41. $this->assertEquals($val, $result[$key]);
  42. }
  43. $result = $http->setConfig([
  44. 'auth' => ['username' => 'mark', 'password' => 'secret']
  45. ]);
  46. $this->assertSame($result, $http);
  47. $result = $http->getConfig();
  48. $expected = [
  49. 'scheme' => 'http',
  50. 'host' => 'example.org',
  51. 'auth' => ['username' => 'mark', 'password' => 'secret']
  52. ];
  53. foreach ($expected as $key => $val) {
  54. $this->assertEquals($val, $result[$key]);
  55. }
  56. }
  57. /**
  58. * testAdapterInstanceCheck
  59. *
  60. * @return void
  61. */
  62. public function testAdapterInstanceCheck()
  63. {
  64. $this->expectException(InvalidArgumentException::class);
  65. $this->expectExceptionMessage('Adapter must be an instance of Cake\Http\Client\AdapterInterface');
  66. new Client(['adapter' => 'stdClass']);
  67. }
  68. /**
  69. * Data provider for buildUrl() tests
  70. *
  71. * @return array
  72. */
  73. public static function urlProvider()
  74. {
  75. return [
  76. [
  77. 'http://example.com/test.html',
  78. 'http://example.com/test.html',
  79. [],
  80. null,
  81. 'Null options'
  82. ],
  83. [
  84. 'http://example.com/test.html',
  85. 'http://example.com/test.html',
  86. [],
  87. [],
  88. 'Simple string'
  89. ],
  90. [
  91. 'http://example.com/test.html',
  92. '/test.html',
  93. [],
  94. ['host' => 'example.com'],
  95. 'host name option',
  96. ],
  97. [
  98. 'https://example.com/test.html',
  99. '/test.html',
  100. [],
  101. ['host' => 'example.com', 'scheme' => 'https'],
  102. 'HTTPS',
  103. ],
  104. [
  105. 'http://example.com:8080/test.html',
  106. '/test.html',
  107. [],
  108. ['host' => 'example.com', 'port' => '8080'],
  109. 'Non standard port',
  110. ],
  111. [
  112. 'http://example.com/test.html',
  113. '/test.html',
  114. [],
  115. ['host' => 'example.com', 'port' => '80'],
  116. 'standard port, does not display'
  117. ],
  118. [
  119. 'https://example.com/test.html',
  120. '/test.html',
  121. [],
  122. ['host' => 'example.com', 'scheme' => 'https', 'port' => '443'],
  123. 'standard port, does not display'
  124. ],
  125. [
  126. 'http://example.com/test.html',
  127. 'http://example.com/test.html',
  128. [],
  129. ['host' => 'example.com', 'scheme' => 'https'],
  130. 'options do not duplicate'
  131. ],
  132. [
  133. 'http://example.com/search?q=hi+there&cat%5Bid%5D%5B0%5D=2&cat%5Bid%5D%5B1%5D=3',
  134. 'http://example.com/search',
  135. ['q' => 'hi there', 'cat' => ['id' => [2, 3]]],
  136. [],
  137. 'query string data.'
  138. ],
  139. [
  140. 'http://example.com/search?q=hi+there&id=12',
  141. 'http://example.com/search?q=hi+there',
  142. ['id' => '12'],
  143. [],
  144. 'query string data with some already on the url.'
  145. ],
  146. [
  147. 'http://example.com/test.html',
  148. '//test.html',
  149. [],
  150. [
  151. 'scheme' => 'http',
  152. 'host' => 'example.com',
  153. 'protocolRelative' => false
  154. ],
  155. 'url with a double slash',
  156. ],
  157. [
  158. 'http://example.com/test.html',
  159. '//example.com/test.html',
  160. [],
  161. [
  162. 'scheme' => 'http',
  163. 'protocolRelative' => true
  164. ],
  165. 'protocol relative url',
  166. ],
  167. ];
  168. }
  169. /**
  170. * @dataProvider urlProvider
  171. */
  172. public function testBuildUrl($expected, $url, $query, $opts)
  173. {
  174. $http = new Client();
  175. $result = $http->buildUrl($url, $query, (array)$opts);
  176. $this->assertEquals($expected, $result);
  177. }
  178. /**
  179. * test simple get request with headers & cookies.
  180. *
  181. * @return void
  182. */
  183. public function testGetSimpleWithHeadersAndCookies()
  184. {
  185. $response = new Response();
  186. $headers = [
  187. 'User-Agent' => 'Cake',
  188. 'Connection' => 'close',
  189. 'Content-Type' => 'application/x-www-form-urlencoded',
  190. ];
  191. $cookies = [
  192. 'split' => 'value'
  193. ];
  194. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  195. ->setMethods(['send'])
  196. ->getMock();
  197. $mock->expects($this->once())
  198. ->method('send')
  199. ->with($this->callback(function ($request) use ($cookies, $headers) {
  200. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  201. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  202. $this->assertEquals('http://cakephp.org/test.html', $request->getUri() . '');
  203. $this->assertEquals('split=value', $request->getHeaderLine('Cookie'));
  204. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('content-type'));
  205. $this->assertEquals($headers['Connection'], $request->getHeaderLine('connection'));
  206. return true;
  207. }))
  208. ->will($this->returnValue([$response]));
  209. $http = new Client(['adapter' => $mock]);
  210. $result = $http->get('http://cakephp.org/test.html', [], [
  211. 'headers' => $headers,
  212. 'cookies' => $cookies,
  213. ]);
  214. $this->assertSame($result, $response);
  215. }
  216. /**
  217. * test get request with no data
  218. *
  219. * @return void
  220. */
  221. public function testGetNoData()
  222. {
  223. $response = new Response();
  224. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  225. ->setMethods(['send'])
  226. ->getMock();
  227. $mock->expects($this->once())
  228. ->method('send')
  229. ->with($this->callback(function ($request) {
  230. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  231. $this->assertEmpty($request->getHeaderLine('Content-Type'), 'Should have no content-type set');
  232. $this->assertEquals(
  233. 'http://cakephp.org/search',
  234. $request->getUri() . ''
  235. );
  236. return true;
  237. }))
  238. ->will($this->returnValue([$response]));
  239. $http = new Client([
  240. 'host' => 'cakephp.org',
  241. 'adapter' => $mock
  242. ]);
  243. $result = $http->get('/search');
  244. $this->assertSame($result, $response);
  245. }
  246. /**
  247. * test get request with querystring data
  248. *
  249. * @return void
  250. */
  251. public function testGetQuerystring()
  252. {
  253. $response = new Response();
  254. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  255. ->setMethods(['send'])
  256. ->getMock();
  257. $mock->expects($this->once())
  258. ->method('send')
  259. ->with($this->callback(function ($request) {
  260. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  261. $this->assertEquals(
  262. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  263. $request->getUri() . ''
  264. );
  265. return true;
  266. }))
  267. ->will($this->returnValue([$response]));
  268. $http = new Client([
  269. 'host' => 'cakephp.org',
  270. 'adapter' => $mock
  271. ]);
  272. $result = $http->get('/search', [
  273. 'q' => 'hi there',
  274. 'Category' => ['id' => [2, 3]]
  275. ]);
  276. $this->assertSame($result, $response);
  277. }
  278. /**
  279. * test get request with string of query data.
  280. *
  281. * @return void
  282. */
  283. public function testGetQuerystringString()
  284. {
  285. $response = new Response();
  286. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  287. ->setMethods(['send'])
  288. ->getMock();
  289. $mock->expects($this->once())
  290. ->method('send')
  291. ->with($this->callback(function ($request) {
  292. $this->assertEquals(
  293. 'http://cakephp.org/search?q=hi+there&Category%5Bid%5D%5B0%5D=2&Category%5Bid%5D%5B1%5D=3',
  294. $request->getUri() . ''
  295. );
  296. return true;
  297. }))
  298. ->will($this->returnValue([$response]));
  299. $http = new Client([
  300. 'host' => 'cakephp.org',
  301. 'adapter' => $mock
  302. ]);
  303. $data = [
  304. 'q' => 'hi there',
  305. 'Category' => ['id' => [2, 3]]
  306. ];
  307. $result = $http->get('/search', http_build_query($data));
  308. $this->assertSame($response, $result);
  309. }
  310. /**
  311. * Test a GET with a request body. Services like
  312. * elasticsearch use this feature.
  313. *
  314. * @return void
  315. */
  316. public function testGetWithContent()
  317. {
  318. $response = new Response();
  319. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  320. ->setMethods(['send'])
  321. ->getMock();
  322. $mock->expects($this->once())
  323. ->method('send')
  324. ->with($this->callback(function ($request) {
  325. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  326. $this->assertEquals('http://cakephp.org/search', '' . $request->getUri());
  327. $this->assertEquals('some data', '' . $request->getBody());
  328. return true;
  329. }))
  330. ->will($this->returnValue([$response]));
  331. $http = new Client([
  332. 'host' => 'cakephp.org',
  333. 'adapter' => $mock
  334. ]);
  335. $result = $http->get('/search', [
  336. '_content' => 'some data'
  337. ]);
  338. $this->assertSame($result, $response);
  339. }
  340. /**
  341. * Test invalid authentication types throw exceptions.
  342. *
  343. * @return void
  344. */
  345. public function testInvalidAuthenticationType()
  346. {
  347. $this->expectException(\Cake\Core\Exception\Exception::class);
  348. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  349. ->setMethods(['send'])
  350. ->getMock();
  351. $mock->expects($this->never())
  352. ->method('send');
  353. $http = new Client([
  354. 'host' => 'cakephp.org',
  355. 'adapter' => $mock
  356. ]);
  357. $result = $http->get('/', [], [
  358. 'auth' => ['type' => 'horribly broken']
  359. ]);
  360. }
  361. /**
  362. * Test setting basic authentication with get
  363. *
  364. * @return void
  365. */
  366. public function testGetWithAuthenticationAndProxy()
  367. {
  368. $response = new Response();
  369. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  370. ->setMethods(['send'])
  371. ->getMock();
  372. $headers = [
  373. 'Authorization' => 'Basic ' . base64_encode('mark:secret'),
  374. 'Proxy-Authorization' => 'Basic ' . base64_encode('mark:pass'),
  375. ];
  376. $mock->expects($this->once())
  377. ->method('send')
  378. ->with($this->callback(function ($request) use ($headers) {
  379. $this->assertEquals(Request::METHOD_GET, $request->getMethod());
  380. $this->assertEquals('http://cakephp.org/', '' . $request->getUri());
  381. $this->assertEquals($headers['Authorization'], $request->getHeaderLine('Authorization'));
  382. $this->assertEquals($headers['Proxy-Authorization'], $request->getHeaderLine('Proxy-Authorization'));
  383. return true;
  384. }))
  385. ->will($this->returnValue([$response]));
  386. $http = new Client([
  387. 'host' => 'cakephp.org',
  388. 'adapter' => $mock
  389. ]);
  390. $result = $http->get('/', [], [
  391. 'auth' => ['username' => 'mark', 'password' => 'secret'],
  392. 'proxy' => ['username' => 'mark', 'password' => 'pass'],
  393. ]);
  394. $this->assertSame($result, $response);
  395. }
  396. /**
  397. * Return a list of HTTP methods.
  398. *
  399. * @return array
  400. */
  401. public static function methodProvider()
  402. {
  403. return [
  404. [Request::METHOD_GET],
  405. [Request::METHOD_POST],
  406. [Request::METHOD_PUT],
  407. [Request::METHOD_DELETE],
  408. [Request::METHOD_PATCH],
  409. [Request::METHOD_OPTIONS],
  410. [Request::METHOD_TRACE],
  411. ];
  412. }
  413. /**
  414. * test simple POST request.
  415. *
  416. * @dataProvider methodProvider
  417. * @return void
  418. */
  419. public function testMethodsSimple($method)
  420. {
  421. $response = new Response();
  422. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  423. ->setMethods(['send'])
  424. ->getMock();
  425. $mock->expects($this->once())
  426. ->method('send')
  427. ->with($this->callback(function ($request) use ($method) {
  428. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  429. $this->assertEquals($method, $request->getMethod());
  430. $this->assertEquals('http://cakephp.org/projects/add', '' . $request->getUri());
  431. return true;
  432. }))
  433. ->will($this->returnValue([$response]));
  434. $http = new Client([
  435. 'host' => 'cakephp.org',
  436. 'adapter' => $mock
  437. ]);
  438. $result = $http->{$method}('/projects/add');
  439. $this->assertSame($result, $response);
  440. }
  441. /**
  442. * Provider for testing the type option.
  443. *
  444. * @return array
  445. */
  446. public static function typeProvider()
  447. {
  448. return [
  449. ['application/json', 'application/json'],
  450. ['json', 'application/json'],
  451. ['xml', 'application/xml'],
  452. ['application/xml', 'application/xml'],
  453. ];
  454. }
  455. /**
  456. * Test that using the 'type' option sets the correct headers
  457. *
  458. * @dataProvider typeProvider
  459. * @return void
  460. */
  461. public function testPostWithTypeKey($type, $mime)
  462. {
  463. $response = new Response();
  464. $data = 'some data';
  465. $headers = [
  466. 'Content-Type' => $mime,
  467. 'Accept' => $mime,
  468. ];
  469. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  470. ->setMethods(['send'])
  471. ->getMock();
  472. $mock->expects($this->once())
  473. ->method('send')
  474. ->with($this->callback(function ($request) use ($headers) {
  475. $this->assertEquals(Request::METHOD_POST, $request->getMethod());
  476. $this->assertEquals($headers['Content-Type'], $request->getHeaderLine('Content-Type'));
  477. $this->assertEquals($headers['Accept'], $request->getHeaderLine('Accept'));
  478. return true;
  479. }))
  480. ->will($this->returnValue([$response]));
  481. $http = new Client([
  482. 'host' => 'cakephp.org',
  483. 'adapter' => $mock
  484. ]);
  485. $http->post('/projects/add', $data, ['type' => $type]);
  486. }
  487. /**
  488. * Test that string payloads with no content type have a default content-type set.
  489. *
  490. * @return void
  491. */
  492. public function testPostWithStringDataDefaultsToFormEncoding()
  493. {
  494. $response = new Response();
  495. $data = 'some=value&more=data';
  496. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  497. ->setMethods(['send'])
  498. ->getMock();
  499. $mock->expects($this->any())
  500. ->method('send')
  501. ->with($this->callback(function ($request) use ($data) {
  502. $this->assertEquals($data, '' . $request->getBody());
  503. $this->assertEquals('application/x-www-form-urlencoded', $request->getHeaderLine('content-type'));
  504. return true;
  505. }))
  506. ->will($this->returnValue([$response]));
  507. $http = new Client([
  508. 'host' => 'cakephp.org',
  509. 'adapter' => $mock
  510. ]);
  511. $http->post('/projects/add', $data);
  512. $http->put('/projects/add', $data);
  513. $http->delete('/projects/add', $data);
  514. }
  515. /**
  516. * Test that exceptions are raised on invalid types.
  517. *
  518. * @return void
  519. */
  520. public function testExceptionOnUnknownType()
  521. {
  522. $this->expectException(\Cake\Core\Exception\Exception::class);
  523. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  524. ->setMethods(['send'])
  525. ->getMock();
  526. $mock->expects($this->never())
  527. ->method('send');
  528. $http = new Client([
  529. 'host' => 'cakephp.org',
  530. 'adapter' => $mock
  531. ]);
  532. $http->post('/projects/add', 'it works', ['type' => 'invalid']);
  533. }
  534. /**
  535. * Test that Client stores cookies
  536. *
  537. * @return void
  538. */
  539. public function testCookieStorage()
  540. {
  541. $adapter = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  542. ->setMethods(['send'])
  543. ->getMock();
  544. $headers = [
  545. 'HTTP/1.0 200 Ok',
  546. 'Set-Cookie: first=1',
  547. 'Set-Cookie: expiring=now; Expires=Wed, 09-Jun-1999 10:18:14 GMT'
  548. ];
  549. $response = new Response($headers, '');
  550. $adapter->expects($this->at(0))
  551. ->method('send')
  552. ->will($this->returnValue([$response]));
  553. $http = new Client([
  554. 'host' => 'cakephp.org',
  555. 'adapter' => $adapter,
  556. ]);
  557. $http->get('/projects');
  558. $cookies = $http->cookies();
  559. $this->assertCount(1, $cookies);
  560. $this->assertTrue($cookies->has('first'));
  561. $this->assertFalse($cookies->has('expiring'));
  562. }
  563. /**
  564. * Test cookieJar config option.
  565. *
  566. * @return void
  567. */
  568. public function testCookieJar()
  569. {
  570. $jar = new CookieCollection();
  571. $http = new Client([
  572. 'cookieJar' => $jar
  573. ]);
  574. $this->assertSame($jar, $http->cookies());
  575. }
  576. /**
  577. * Test addCookie() method.
  578. *
  579. * @return void
  580. */
  581. public function testAddCookie()
  582. {
  583. $client = new Client();
  584. $cookie = new Cookie('foo', '', null, '/', 'example.com');
  585. $this->assertFalse($client->cookies()->has('foo'));
  586. $client->addCookie($cookie);
  587. $this->assertTrue($client->cookies()->has('foo'));
  588. }
  589. /**
  590. * Test addCookie() method without a domain.
  591. *
  592. * @return void
  593. */
  594. public function testAddCookieWithoutDomain()
  595. {
  596. $this->expectException(\InvalidArgumentException::class);
  597. $this->expectExceptionMessage('Cookie must have a domain and a path set.');
  598. $client = new Client();
  599. $cookie = new Cookie('foo', '', null, '/', '');
  600. $this->assertFalse($client->cookies()->has('foo'));
  601. $client->addCookie($cookie);
  602. $this->assertTrue($client->cookies()->has('foo'));
  603. }
  604. /**
  605. * Test addCookie() method without a path.
  606. *
  607. * @return void
  608. */
  609. public function testAddCookieWithoutPath()
  610. {
  611. $this->expectException(\InvalidArgumentException::class);
  612. $this->expectExceptionMessage('Cookie must have a domain and a path set.');
  613. $client = new Client();
  614. $cookie = new Cookie('foo', '', null, '', 'example.com');
  615. $this->assertFalse($client->cookies()->has('foo'));
  616. $client->addCookie($cookie);
  617. $this->assertTrue($client->cookies()->has('foo'));
  618. }
  619. /**
  620. * test head request with querystring data
  621. *
  622. * @return void
  623. */
  624. public function testHeadQuerystring()
  625. {
  626. $response = new Response();
  627. $mock = $this->getMockBuilder('Cake\Http\Client\Adapter\Stream')
  628. ->setMethods(['send'])
  629. ->getMock();
  630. $mock->expects($this->once())
  631. ->method('send')
  632. ->with($this->callback(function ($request) {
  633. $this->assertInstanceOf('Cake\Http\Client\Request', $request);
  634. $this->assertEquals(Request::METHOD_HEAD, $request->getMethod());
  635. $this->assertEquals('http://cakephp.org/search?q=hi+there', '' . $request->getUri());
  636. return true;
  637. }))
  638. ->will($this->returnValue([$response]));
  639. $http = new Client([
  640. 'host' => 'cakephp.org',
  641. 'adapter' => $mock
  642. ]);
  643. $result = $http->head('/search', [
  644. 'q' => 'hi there',
  645. ]);
  646. $this->assertSame($result, $response);
  647. }
  648. /**
  649. * test redirects
  650. *
  651. * @return void
  652. */
  653. public function testRedirects()
  654. {
  655. $url = 'http://cakephp.org';
  656. $adapter = $this->getMockBuilder(Client\Adapter\Stream::class)
  657. ->setMethods(['send'])
  658. ->getMock();
  659. $redirect = new Response([
  660. 'HTTP/1.0 301',
  661. 'Location: http://cakephp.org/redirect1?foo=bar',
  662. 'Set-Cookie: redirect1=true;path=/',
  663. ]);
  664. $adapter->expects($this->at(0))
  665. ->method('send')
  666. ->with(
  667. $this->callback(function ($request) use ($url) {
  668. $this->assertInstanceOf(Request::class, $request);
  669. $this->assertEquals($url, $request->getUri());
  670. return true;
  671. }),
  672. $this->callback(function ($options) {
  673. $this->assertArrayNotHasKey('redirect', $options);
  674. return true;
  675. })
  676. )
  677. ->willReturn([$redirect]);
  678. $redirect2 = new Response([
  679. 'HTTP/1.0 301',
  680. 'Location: /redirect2#foo',
  681. 'Set-Cookie: redirect2=true;path=/',
  682. ]);
  683. $adapter->expects($this->at(1))
  684. ->method('send')
  685. ->with(
  686. $this->callback(function ($request) use ($url) {
  687. $this->assertInstanceOf(Request::class, $request);
  688. $this->assertEquals($url . '/redirect1?foo=bar', $request->getUri());
  689. return true;
  690. }),
  691. $this->callback(function ($options) {
  692. $this->assertArrayNotHasKey('redirect', $options);
  693. return true;
  694. })
  695. )
  696. ->willReturn([$redirect2]);
  697. $response = new Response([
  698. 'HTTP/1.0 200'
  699. ]);
  700. $adapter->expects($this->at(2))
  701. ->method('send')
  702. ->with($this->callback(function ($request) use ($url) {
  703. $this->assertInstanceOf(Request::class, $request);
  704. $this->assertEquals($url . '/redirect2#foo', $request->getUri());
  705. return true;
  706. }))
  707. ->willReturn([$response]);
  708. $client = new Client([
  709. 'adapter' => $adapter
  710. ]);
  711. $result = $client->send(new Request($url), [
  712. 'redirect' => 10
  713. ]);
  714. $this->assertInstanceOf(Response::class, $result);
  715. $this->assertTrue($result->isOk());
  716. $cookies = $client->cookies();
  717. $this->assertTrue($cookies->has('redirect1'));
  718. $this->assertTrue($cookies->has('redirect2'));
  719. }
  720. }