ClientTest.php 26 KB

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