ClientTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Cookie\CookieJar;
  5. use GuzzleHttp\Handler\MockHandler;
  6. use GuzzleHttp\HandlerStack;
  7. use GuzzleHttp\Promise\PromiseInterface;
  8. use GuzzleHttp\Psr7;
  9. use GuzzleHttp\Psr7\Request;
  10. use GuzzleHttp\Psr7\Response;
  11. use GuzzleHttp\Psr7\Uri;
  12. use Psr\Http\Message\ResponseInterface;
  13. use PHPUnit\Framework\TestCase;
  14. class ClientTest extends TestCase
  15. {
  16. public function testUsesDefaultHandler()
  17. {
  18. $client = new Client();
  19. Server::enqueue([new Response(200, ['Content-Length' => 0])]);
  20. $response = $client->get(Server::$url);
  21. $this->assertSame(200, $response->getStatusCode());
  22. }
  23. /**
  24. * @expectedException \InvalidArgumentException
  25. * @expectedExceptionMessage Magic request methods require a URI and optional options array
  26. */
  27. public function testValidatesArgsForMagicMethods()
  28. {
  29. $client = new Client();
  30. $client->get();
  31. }
  32. public function testCanSendMagicAsyncRequests()
  33. {
  34. $client = new Client();
  35. Server::flush();
  36. Server::enqueue([new Response(200, ['Content-Length' => 2], 'hi')]);
  37. $p = $client->getAsync(Server::$url, ['query' => ['test' => 'foo']]);
  38. $this->assertInstanceOf(PromiseInterface::class, $p);
  39. $this->assertSame(200, $p->wait()->getStatusCode());
  40. $received = Server::received(true);
  41. $this->assertCount(1, $received);
  42. $this->assertSame('test=foo', $received[0]->getUri()->getQuery());
  43. }
  44. public function testCanSendSynchronously()
  45. {
  46. $client = new Client(['handler' => new MockHandler([new Response()])]);
  47. $request = new Request('GET', 'http://example.com');
  48. $r = $client->send($request);
  49. $this->assertInstanceOf(ResponseInterface::class, $r);
  50. $this->assertSame(200, $r->getStatusCode());
  51. }
  52. public function testClientHasOptions()
  53. {
  54. $client = new Client([
  55. 'base_uri' => 'http://foo.com',
  56. 'timeout' => 2,
  57. 'headers' => ['bar' => 'baz'],
  58. 'handler' => new MockHandler()
  59. ]);
  60. $base = $client->getConfig('base_uri');
  61. $this->assertSame('http://foo.com', (string) $base);
  62. $this->assertInstanceOf(Uri::class, $base);
  63. $this->assertNotNull($client->getConfig('handler'));
  64. $this->assertSame(2, $client->getConfig('timeout'));
  65. $this->assertArrayHasKey('timeout', $client->getConfig());
  66. $this->assertArrayHasKey('headers', $client->getConfig());
  67. }
  68. public function testCanMergeOnBaseUri()
  69. {
  70. $mock = new MockHandler([new Response()]);
  71. $client = new Client([
  72. 'base_uri' => 'http://foo.com/bar/',
  73. 'handler' => $mock
  74. ]);
  75. $client->get('baz');
  76. $this->assertSame(
  77. 'http://foo.com/bar/baz',
  78. (string)$mock->getLastRequest()->getUri()
  79. );
  80. }
  81. public function testCanMergeOnBaseUriWithRequest()
  82. {
  83. $mock = new MockHandler([new Response(), new Response()]);
  84. $client = new Client([
  85. 'handler' => $mock,
  86. 'base_uri' => 'http://foo.com/bar/'
  87. ]);
  88. $client->request('GET', new Uri('baz'));
  89. $this->assertSame(
  90. 'http://foo.com/bar/baz',
  91. (string) $mock->getLastRequest()->getUri()
  92. );
  93. $client->request('GET', new Uri('baz'), ['base_uri' => 'http://example.com/foo/']);
  94. $this->assertSame(
  95. 'http://example.com/foo/baz',
  96. (string) $mock->getLastRequest()->getUri(),
  97. 'Can overwrite the base_uri through the request options'
  98. );
  99. }
  100. public function testCanUseRelativeUriWithSend()
  101. {
  102. $mock = new MockHandler([new Response()]);
  103. $client = new Client([
  104. 'handler' => $mock,
  105. 'base_uri' => 'http://bar.com'
  106. ]);
  107. $this->assertSame('http://bar.com', (string) $client->getConfig('base_uri'));
  108. $request = new Request('GET', '/baz');
  109. $client->send($request);
  110. $this->assertSame(
  111. 'http://bar.com/baz',
  112. (string) $mock->getLastRequest()->getUri()
  113. );
  114. }
  115. public function testMergesDefaultOptionsAndDoesNotOverwriteUa()
  116. {
  117. $c = new Client(['headers' => ['User-agent' => 'foo']]);
  118. $this->assertSame(['User-agent' => 'foo'], $c->getConfig('headers'));
  119. $this->assertInternalType('array', $c->getConfig('allow_redirects'));
  120. $this->assertTrue($c->getConfig('http_errors'));
  121. $this->assertTrue($c->getConfig('decode_content'));
  122. $this->assertTrue($c->getConfig('verify'));
  123. }
  124. public function testDoesNotOverwriteHeaderWithDefault()
  125. {
  126. $mock = new MockHandler([new Response()]);
  127. $c = new Client([
  128. 'headers' => ['User-agent' => 'foo'],
  129. 'handler' => $mock
  130. ]);
  131. $c->get('http://example.com', ['headers' => ['User-Agent' => 'bar']]);
  132. $this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
  133. }
  134. public function testDoesNotOverwriteHeaderWithDefaultInRequest()
  135. {
  136. $mock = new MockHandler([new Response()]);
  137. $c = new Client([
  138. 'headers' => ['User-agent' => 'foo'],
  139. 'handler' => $mock
  140. ]);
  141. $request = new Request('GET', Server::$url, ['User-Agent' => 'bar']);
  142. $c->send($request);
  143. $this->assertSame('bar', $mock->getLastRequest()->getHeaderLine('User-Agent'));
  144. }
  145. public function testDoesOverwriteHeaderWithSetRequestOption()
  146. {
  147. $mock = new MockHandler([new Response()]);
  148. $c = new Client([
  149. 'headers' => ['User-agent' => 'foo'],
  150. 'handler' => $mock
  151. ]);
  152. $request = new Request('GET', Server::$url, ['User-Agent' => 'bar']);
  153. $c->send($request, ['headers' => ['User-Agent' => 'YO']]);
  154. $this->assertSame('YO', $mock->getLastRequest()->getHeaderLine('User-Agent'));
  155. }
  156. public function testCanUnsetRequestOptionWithNull()
  157. {
  158. $mock = new MockHandler([new Response()]);
  159. $c = new Client([
  160. 'headers' => ['foo' => 'bar'],
  161. 'handler' => $mock
  162. ]);
  163. $c->get('http://example.com', ['headers' => null]);
  164. $this->assertFalse($mock->getLastRequest()->hasHeader('foo'));
  165. }
  166. public function testRewriteExceptionsToHttpErrors()
  167. {
  168. $client = new Client(['handler' => new MockHandler([new Response(404)])]);
  169. $res = $client->get('http://foo.com', ['exceptions' => false]);
  170. $this->assertSame(404, $res->getStatusCode());
  171. }
  172. public function testRewriteSaveToToSink()
  173. {
  174. $r = Psr7\stream_for(fopen('php://temp', 'r+'));
  175. $mock = new MockHandler([new Response(200, [], 'foo')]);
  176. $client = new Client(['handler' => $mock]);
  177. $client->get('http://foo.com', ['save_to' => $r]);
  178. $this->assertSame($r, $mock->getLastOptions()['sink']);
  179. }
  180. public function testAllowRedirectsCanBeTrue()
  181. {
  182. $mock = new MockHandler([new Response(200, [], 'foo')]);
  183. $handler = HandlerStack::create($mock);
  184. $client = new Client(['handler' => $handler]);
  185. $client->get('http://foo.com', ['allow_redirects' => true]);
  186. $this->assertInternalType('array', $mock->getLastOptions()['allow_redirects']);
  187. }
  188. /**
  189. * @expectedException \InvalidArgumentException
  190. * @expectedExceptionMessage allow_redirects must be true, false, or array
  191. */
  192. public function testValidatesAllowRedirects()
  193. {
  194. $mock = new MockHandler([new Response(200, [], 'foo')]);
  195. $handler = HandlerStack::create($mock);
  196. $client = new Client(['handler' => $handler]);
  197. $client->get('http://foo.com', ['allow_redirects' => 'foo']);
  198. }
  199. /**
  200. * @expectedException \GuzzleHttp\Exception\ClientException
  201. */
  202. public function testThrowsHttpErrorsByDefault()
  203. {
  204. $mock = new MockHandler([new Response(404)]);
  205. $handler = HandlerStack::create($mock);
  206. $client = new Client(['handler' => $handler]);
  207. $client->get('http://foo.com');
  208. }
  209. /**
  210. * @expectedException \InvalidArgumentException
  211. * @expectedExceptionMessage cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface
  212. */
  213. public function testValidatesCookies()
  214. {
  215. $mock = new MockHandler([new Response(200, [], 'foo')]);
  216. $handler = HandlerStack::create($mock);
  217. $client = new Client(['handler' => $handler]);
  218. $client->get('http://foo.com', ['cookies' => 'foo']);
  219. }
  220. public function testSetCookieToTrueUsesSharedJar()
  221. {
  222. $mock = new MockHandler([
  223. new Response(200, ['Set-Cookie' => 'foo=bar']),
  224. new Response()
  225. ]);
  226. $handler = HandlerStack::create($mock);
  227. $client = new Client(['handler' => $handler, 'cookies' => true]);
  228. $client->get('http://foo.com');
  229. $client->get('http://foo.com');
  230. $this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
  231. }
  232. public function testSetCookieToJar()
  233. {
  234. $mock = new MockHandler([
  235. new Response(200, ['Set-Cookie' => 'foo=bar']),
  236. new Response()
  237. ]);
  238. $handler = HandlerStack::create($mock);
  239. $client = new Client(['handler' => $handler]);
  240. $jar = new CookieJar();
  241. $client->get('http://foo.com', ['cookies' => $jar]);
  242. $client->get('http://foo.com', ['cookies' => $jar]);
  243. $this->assertSame('foo=bar', $mock->getLastRequest()->getHeaderLine('Cookie'));
  244. }
  245. public function testCanDisableContentDecoding()
  246. {
  247. $mock = new MockHandler([new Response()]);
  248. $client = new Client(['handler' => $mock]);
  249. $client->get('http://foo.com', ['decode_content' => false]);
  250. $last = $mock->getLastRequest();
  251. $this->assertFalse($last->hasHeader('Accept-Encoding'));
  252. $this->assertFalse($mock->getLastOptions()['decode_content']);
  253. }
  254. public function testCanSetContentDecodingToValue()
  255. {
  256. $mock = new MockHandler([new Response()]);
  257. $client = new Client(['handler' => $mock]);
  258. $client->get('http://foo.com', ['decode_content' => 'gzip']);
  259. $last = $mock->getLastRequest();
  260. $this->assertSame('gzip', $last->getHeaderLine('Accept-Encoding'));
  261. $this->assertSame('gzip', $mock->getLastOptions()['decode_content']);
  262. }
  263. /**
  264. * @expectedException \InvalidArgumentException
  265. */
  266. public function testValidatesHeaders()
  267. {
  268. $mock = new MockHandler();
  269. $client = new Client(['handler' => $mock]);
  270. $client->get('http://foo.com', ['headers' => 'foo']);
  271. }
  272. public function testAddsBody()
  273. {
  274. $mock = new MockHandler([new Response()]);
  275. $client = new Client(['handler' => $mock]);
  276. $request = new Request('PUT', 'http://foo.com');
  277. $client->send($request, ['body' => 'foo']);
  278. $last = $mock->getLastRequest();
  279. $this->assertSame('foo', (string) $last->getBody());
  280. }
  281. /**
  282. * @expectedException \InvalidArgumentException
  283. */
  284. public function testValidatesQuery()
  285. {
  286. $mock = new MockHandler();
  287. $client = new Client(['handler' => $mock]);
  288. $request = new Request('PUT', 'http://foo.com');
  289. $client->send($request, ['query' => false]);
  290. }
  291. public function testQueryCanBeString()
  292. {
  293. $mock = new MockHandler([new Response()]);
  294. $client = new Client(['handler' => $mock]);
  295. $request = new Request('PUT', 'http://foo.com');
  296. $client->send($request, ['query' => 'foo']);
  297. $this->assertSame('foo', $mock->getLastRequest()->getUri()->getQuery());
  298. }
  299. public function testQueryCanBeArray()
  300. {
  301. $mock = new MockHandler([new Response()]);
  302. $client = new Client(['handler' => $mock]);
  303. $request = new Request('PUT', 'http://foo.com');
  304. $client->send($request, ['query' => ['foo' => 'bar baz']]);
  305. $this->assertSame('foo=bar%20baz', $mock->getLastRequest()->getUri()->getQuery());
  306. }
  307. public function testCanAddJsonData()
  308. {
  309. $mock = new MockHandler([new Response()]);
  310. $client = new Client(['handler' => $mock]);
  311. $request = new Request('PUT', 'http://foo.com');
  312. $client->send($request, ['json' => ['foo' => 'bar']]);
  313. $last = $mock->getLastRequest();
  314. $this->assertSame('{"foo":"bar"}', (string) $mock->getLastRequest()->getBody());
  315. $this->assertSame('application/json', $last->getHeaderLine('Content-Type'));
  316. }
  317. public function testCanAddJsonDataWithoutOverwritingContentType()
  318. {
  319. $mock = new MockHandler([new Response()]);
  320. $client = new Client(['handler' => $mock]);
  321. $request = new Request('PUT', 'http://foo.com');
  322. $client->send($request, [
  323. 'headers' => ['content-type' => 'foo'],
  324. 'json' => 'a'
  325. ]);
  326. $last = $mock->getLastRequest();
  327. $this->assertSame('"a"', (string) $mock->getLastRequest()->getBody());
  328. $this->assertSame('foo', $last->getHeaderLine('Content-Type'));
  329. }
  330. public function testAuthCanBeTrue()
  331. {
  332. $mock = new MockHandler([new Response()]);
  333. $client = new Client(['handler' => $mock]);
  334. $client->get('http://foo.com', ['auth' => false]);
  335. $last = $mock->getLastRequest();
  336. $this->assertFalse($last->hasHeader('Authorization'));
  337. }
  338. public function testAuthCanBeArrayForBasicAuth()
  339. {
  340. $mock = new MockHandler([new Response()]);
  341. $client = new Client(['handler' => $mock]);
  342. $client->get('http://foo.com', ['auth' => ['a', 'b']]);
  343. $last = $mock->getLastRequest();
  344. $this->assertSame('Basic YTpi', $last->getHeaderLine('Authorization'));
  345. }
  346. public function testAuthCanBeArrayForDigestAuth()
  347. {
  348. $mock = new MockHandler([new Response()]);
  349. $client = new Client(['handler' => $mock]);
  350. $client->get('http://foo.com', ['auth' => ['a', 'b', 'digest']]);
  351. $last = $mock->getLastOptions();
  352. $this->assertSame([
  353. CURLOPT_HTTPAUTH => 2,
  354. CURLOPT_USERPWD => 'a:b'
  355. ], $last['curl']);
  356. }
  357. public function testAuthCanBeArrayForNtlmAuth()
  358. {
  359. $mock = new MockHandler([new Response()]);
  360. $client = new Client(['handler' => $mock]);
  361. $client->get('http://foo.com', ['auth' => ['a', 'b', 'ntlm']]);
  362. $last = $mock->getLastOptions();
  363. $this->assertSame([
  364. CURLOPT_HTTPAUTH => 8,
  365. CURLOPT_USERPWD => 'a:b'
  366. ], $last['curl']);
  367. }
  368. public function testAuthCanBeCustomType()
  369. {
  370. $mock = new MockHandler([new Response()]);
  371. $client = new Client(['handler' => $mock]);
  372. $client->get('http://foo.com', ['auth' => 'foo']);
  373. $last = $mock->getLastOptions();
  374. $this->assertSame('foo', $last['auth']);
  375. }
  376. public function testCanAddFormParams()
  377. {
  378. $mock = new MockHandler([new Response()]);
  379. $client = new Client(['handler' => $mock]);
  380. $client->post('http://foo.com', [
  381. 'form_params' => [
  382. 'foo' => 'bar bam',
  383. 'baz' => ['boo' => 'qux']
  384. ]
  385. ]);
  386. $last = $mock->getLastRequest();
  387. $this->assertSame(
  388. 'application/x-www-form-urlencoded',
  389. $last->getHeaderLine('Content-Type')
  390. );
  391. $this->assertSame(
  392. 'foo=bar+bam&baz%5Bboo%5D=qux',
  393. (string) $last->getBody()
  394. );
  395. }
  396. public function testFormParamsEncodedProperly()
  397. {
  398. $separator = ini_get('arg_separator.output');
  399. ini_set('arg_separator.output', '&amp;');
  400. $mock = new MockHandler([new Response()]);
  401. $client = new Client(['handler' => $mock]);
  402. $client->post('http://foo.com', [
  403. 'form_params' => [
  404. 'foo' => 'bar bam',
  405. 'baz' => ['boo' => 'qux']
  406. ]
  407. ]);
  408. $last = $mock->getLastRequest();
  409. $this->assertSame(
  410. 'foo=bar+bam&baz%5Bboo%5D=qux',
  411. (string) $last->getBody()
  412. );
  413. ini_set('arg_separator.output', $separator);
  414. }
  415. /**
  416. * @expectedException \InvalidArgumentException
  417. */
  418. public function testEnsuresThatFormParamsAndMultipartAreExclusive()
  419. {
  420. $client = new Client(['handler' => function () {}]);
  421. $client->post('http://foo.com', [
  422. 'form_params' => ['foo' => 'bar bam'],
  423. 'multipart' => []
  424. ]);
  425. }
  426. public function testCanSendMultipart()
  427. {
  428. $mock = new MockHandler([new Response()]);
  429. $client = new Client(['handler' => $mock]);
  430. $client->post('http://foo.com', [
  431. 'multipart' => [
  432. [
  433. 'name' => 'foo',
  434. 'contents' => 'bar'
  435. ],
  436. [
  437. 'name' => 'test',
  438. 'contents' => fopen(__FILE__, 'r')
  439. ]
  440. ]
  441. ]);
  442. $last = $mock->getLastRequest();
  443. $this->assertContains(
  444. 'multipart/form-data; boundary=',
  445. $last->getHeaderLine('Content-Type')
  446. );
  447. $this->assertContains(
  448. 'Content-Disposition: form-data; name="foo"',
  449. (string) $last->getBody()
  450. );
  451. $this->assertContains('bar', (string) $last->getBody());
  452. $this->assertContains(
  453. 'Content-Disposition: form-data; name="foo"' . "\r\n",
  454. (string) $last->getBody()
  455. );
  456. $this->assertContains(
  457. 'Content-Disposition: form-data; name="test"; filename="ClientTest.php"',
  458. (string) $last->getBody()
  459. );
  460. }
  461. public function testCanSendMultipartWithExplicitBody()
  462. {
  463. $mock = new MockHandler([new Response()]);
  464. $client = new Client(['handler' => $mock]);
  465. $client->send(
  466. new Request(
  467. 'POST',
  468. 'http://foo.com',
  469. [],
  470. new Psr7\MultipartStream(
  471. [
  472. [
  473. 'name' => 'foo',
  474. 'contents' => 'bar',
  475. ],
  476. [
  477. 'name' => 'test',
  478. 'contents' => fopen(__FILE__, 'r'),
  479. ],
  480. ]
  481. )
  482. )
  483. );
  484. $last = $mock->getLastRequest();
  485. $this->assertContains(
  486. 'multipart/form-data; boundary=',
  487. $last->getHeaderLine('Content-Type')
  488. );
  489. $this->assertContains(
  490. 'Content-Disposition: form-data; name="foo"',
  491. (string) $last->getBody()
  492. );
  493. $this->assertContains('bar', (string) $last->getBody());
  494. $this->assertContains(
  495. 'Content-Disposition: form-data; name="foo"' . "\r\n",
  496. (string) $last->getBody()
  497. );
  498. $this->assertContains(
  499. 'Content-Disposition: form-data; name="test"; filename="ClientTest.php"',
  500. (string) $last->getBody()
  501. );
  502. }
  503. public function testUsesProxyEnvironmentVariables()
  504. {
  505. $http = getenv('HTTP_PROXY');
  506. $https = getenv('HTTPS_PROXY');
  507. $no = getenv('NO_PROXY');
  508. $client = new Client();
  509. $this->assertNull($client->getConfig('proxy'));
  510. putenv('HTTP_PROXY=127.0.0.1');
  511. $client = new Client();
  512. $this->assertSame(
  513. ['http' => '127.0.0.1'],
  514. $client->getConfig('proxy')
  515. );
  516. putenv('HTTPS_PROXY=127.0.0.2');
  517. putenv('NO_PROXY=127.0.0.3, 127.0.0.4');
  518. $client = new Client();
  519. $this->assertSame(
  520. ['http' => '127.0.0.1', 'https' => '127.0.0.2', 'no' => ['127.0.0.3','127.0.0.4']],
  521. $client->getConfig('proxy')
  522. );
  523. putenv("HTTP_PROXY=$http");
  524. putenv("HTTPS_PROXY=$https");
  525. putenv("NO_PROXY=$no");
  526. }
  527. public function testRequestSendsWithSync()
  528. {
  529. $mock = new MockHandler([new Response()]);
  530. $client = new Client(['handler' => $mock]);
  531. $client->request('GET', 'http://foo.com');
  532. $this->assertTrue($mock->getLastOptions()['synchronous']);
  533. }
  534. public function testSendSendsWithSync()
  535. {
  536. $mock = new MockHandler([new Response()]);
  537. $client = new Client(['handler' => $mock]);
  538. $client->send(new Request('GET', 'http://foo.com'));
  539. $this->assertTrue($mock->getLastOptions()['synchronous']);
  540. }
  541. public function testCanSetCustomHandler()
  542. {
  543. $mock = new MockHandler([new Response(500)]);
  544. $client = new Client(['handler' => $mock]);
  545. $mock2 = new MockHandler([new Response(200)]);
  546. $this->assertSame(
  547. 200,
  548. $client->send(new Request('GET', 'http://foo.com'), [
  549. 'handler' => $mock2
  550. ])->getStatusCode()
  551. );
  552. }
  553. public function testProperlyBuildsQuery()
  554. {
  555. $mock = new MockHandler([new Response()]);
  556. $client = new Client(['handler' => $mock]);
  557. $request = new Request('PUT', 'http://foo.com');
  558. $client->send($request, ['query' => ['foo' => 'bar', 'john' => 'doe']]);
  559. $this->assertSame('foo=bar&john=doe', $mock->getLastRequest()->getUri()->getQuery());
  560. }
  561. public function testSendSendsWithIpAddressAndPortAndHostHeaderInRequestTheHostShouldBePreserved()
  562. {
  563. $mockHandler = new MockHandler([new Response()]);
  564. $client = new Client(['base_uri' => 'http://127.0.0.1:8585', 'handler' => $mockHandler]);
  565. $request = new Request('GET', '/test', ['Host'=>'foo.com']);
  566. $client->send($request);
  567. $this->assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]);
  568. }
  569. public function testSendSendsWithDomainAndHostHeaderInRequestTheHostShouldBePreserved()
  570. {
  571. $mockHandler = new MockHandler([new Response()]);
  572. $client = new Client(['base_uri' => 'http://foo2.com', 'handler' => $mockHandler]);
  573. $request = new Request('GET', '/test', ['Host'=>'foo.com']);
  574. $client->send($request);
  575. $this->assertSame('foo.com', $mockHandler->getLastRequest()->getHeader('Host')[0]);
  576. }
  577. /**
  578. * @expectedException \InvalidArgumentException
  579. */
  580. public function testValidatesSink()
  581. {
  582. $mockHandler = new MockHandler([new Response()]);
  583. $client = new Client(['handler' => $mockHandler]);
  584. $client->get('http://test.com', ['sink' => true]);
  585. }
  586. public function testHttpDefaultSchemeIfUriHasNone()
  587. {
  588. $mockHandler = new MockHandler([new Response()]);
  589. $client = new Client(['handler' => $mockHandler]);
  590. $client->request('GET', '//example.org/test');
  591. $this->assertSame('http://example.org/test', (string) $mockHandler->getLastRequest()->getUri());
  592. }
  593. public function testOnlyAddSchemeWhenHostIsPresent()
  594. {
  595. $mockHandler = new MockHandler([new Response()]);
  596. $client = new Client(['handler' => $mockHandler]);
  597. $client->request('GET', 'baz');
  598. $this->assertSame(
  599. 'baz',
  600. (string) $mockHandler->getLastRequest()->getUri()
  601. );
  602. }
  603. /**
  604. * @expectedException InvalidArgumentException
  605. */
  606. public function testHandlerIsCallable()
  607. {
  608. new Client(['handler' => 'not_cllable']);
  609. }
  610. }