ClientTest.php 25 KB

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