Base.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. <?php
  2. abstract class RequestsTest_Transport_Base extends PHPUnit_Framework_TestCase {
  3. public function setUp() {
  4. $callback = array($this->transport, 'test');
  5. $supported = call_user_func($callback);
  6. if (!$supported) {
  7. $this->markTestSkipped($this->transport . ' is not available');
  8. return;
  9. }
  10. $ssl_supported = call_user_func($callback, array('ssl' => true));
  11. if (!$ssl_supported) {
  12. $this->skip_https = true;
  13. }
  14. }
  15. protected $skip_https = false;
  16. protected function getOptions($other = array()) {
  17. $options = array(
  18. 'transport' => $this->transport
  19. );
  20. $options = array_merge($options, $other);
  21. return $options;
  22. }
  23. public function testResponseByteLimit() {
  24. $limit = 104;
  25. $options = array(
  26. 'max_bytes' => $limit,
  27. );
  28. $response = Requests::get(httpbin('/bytes/325'), array(), $this->getOptions($options));
  29. $this->assertEquals($limit, strlen($response->body));
  30. }
  31. public function testResponseByteLimitWithFile() {
  32. $limit = 300;
  33. $options = array(
  34. 'max_bytes' => $limit,
  35. 'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
  36. );
  37. $response = Requests::get(httpbin('/bytes/482'), array(), $this->getOptions($options));
  38. $this->assertEmpty($response->body);
  39. $this->assertEquals($limit, filesize($options['filename']));
  40. unlink($options['filename']);
  41. }
  42. public function testSimpleGET() {
  43. $request = Requests::get(httpbin('/get'), array(), $this->getOptions());
  44. $this->assertEquals(200, $request->status_code);
  45. $result = json_decode($request->body, true);
  46. $this->assertEquals(httpbin('/get'), $result['url']);
  47. $this->assertEmpty($result['args']);
  48. }
  49. public function testGETWithArgs() {
  50. $request = Requests::get(httpbin('/get?test=true&test2=test'), array(), $this->getOptions());
  51. $this->assertEquals(200, $request->status_code);
  52. $result = json_decode($request->body, true);
  53. $this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
  54. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
  55. }
  56. public function testGETWithData() {
  57. $data = array(
  58. 'test' => 'true',
  59. 'test2' => 'test',
  60. );
  61. $request = Requests::request(httpbin('/get'), array(), $data, Requests::GET, $this->getOptions());
  62. $this->assertEquals(200, $request->status_code);
  63. $result = json_decode($request->body, true);
  64. $this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
  65. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
  66. }
  67. public function testGETWithNestedData() {
  68. $data = array(
  69. 'test' => 'true',
  70. 'test2' => array(
  71. 'test3' => 'test',
  72. 'test4' => 'test-too',
  73. ),
  74. );
  75. $request = Requests::request(httpbin('/get'), array(), $data, Requests::GET, $this->getOptions());
  76. $this->assertEquals(200, $request->status_code);
  77. $result = json_decode($request->body, true);
  78. $this->assertEquals(httpbin('/get?test=true&test2%5Btest3%5D=test&test2%5Btest4%5D=test-too'), $result['url']);
  79. $this->assertEquals(array('test' => 'true', 'test2[test3]' => 'test', 'test2[test4]' => 'test-too'), $result['args']);
  80. }
  81. public function testGETWithDataAndQuery() {
  82. $data = array(
  83. 'test2' => 'test',
  84. );
  85. $request = Requests::request(httpbin('/get?test=true'), array(), $data, Requests::GET, $this->getOptions());
  86. $this->assertEquals(200, $request->status_code);
  87. $result = json_decode($request->body, true);
  88. $this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
  89. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
  90. }
  91. public function testGETWithHeaders() {
  92. $headers = array(
  93. 'Requested-At' => time(),
  94. );
  95. $request = Requests::get(httpbin('/get'), $headers, $this->getOptions());
  96. $this->assertEquals(200, $request->status_code);
  97. $result = json_decode($request->body, true);
  98. $this->assertEquals($headers['Requested-At'], $result['headers']['Requested-At']);
  99. }
  100. public function testChunked() {
  101. $request = Requests::get(httpbin('/stream/1'), array(), $this->getOptions());
  102. $this->assertEquals(200, $request->status_code);
  103. $result = json_decode($request->body, true);
  104. $this->assertEquals(httpbin('/stream/1'), $result['url']);
  105. $this->assertEmpty($result['args']);
  106. }
  107. public function testHEAD() {
  108. $request = Requests::head(httpbin('/get'), array(), $this->getOptions());
  109. $this->assertEquals(200, $request->status_code);
  110. $this->assertEquals('', $request->body);
  111. }
  112. public function testTRACE() {
  113. $request = Requests::trace(httpbin('/trace'), array(), $this->getOptions());
  114. $this->assertEquals(200, $request->status_code);
  115. }
  116. public function testRawPOST() {
  117. $data = 'test';
  118. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
  119. $this->assertEquals(200, $request->status_code);
  120. $result = json_decode($request->body, true);
  121. $this->assertEquals('test', $result['data']);
  122. }
  123. public function testFormPost() {
  124. $data = 'test=true&test2=test';
  125. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
  126. $this->assertEquals(200, $request->status_code);
  127. $result = json_decode($request->body, true);
  128. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  129. }
  130. public function testPOSTWithArray() {
  131. $data = array(
  132. 'test' => 'true',
  133. 'test2' => 'test',
  134. );
  135. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
  136. $this->assertEquals(200, $request->status_code);
  137. $result = json_decode($request->body, true);
  138. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  139. }
  140. public function testPOSTWithNestedData() {
  141. $data = array(
  142. 'test' => 'true',
  143. 'test2' => array(
  144. 'test3' => 'test',
  145. 'test4' => 'test-too',
  146. ),
  147. );
  148. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
  149. $this->assertEquals(200, $request->status_code);
  150. $result = json_decode($request->body, true);
  151. $this->assertEquals(array('test' => 'true', 'test2[test3]' => 'test', 'test2[test4]' => 'test-too'), $result['form']);
  152. }
  153. public function testRawPUT() {
  154. $data = 'test';
  155. $request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
  156. $this->assertEquals(200, $request->status_code);
  157. $result = json_decode($request->body, true);
  158. $this->assertEquals('test', $result['data']);
  159. }
  160. public function testFormPUT() {
  161. $data = 'test=true&test2=test';
  162. $request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
  163. $this->assertEquals(200, $request->status_code);
  164. $result = json_decode($request->body, true);
  165. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  166. }
  167. public function testPUTWithArray() {
  168. $data = array(
  169. 'test' => 'true',
  170. 'test2' => 'test',
  171. );
  172. $request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
  173. $this->assertEquals(200, $request->status_code);
  174. $result = json_decode($request->body, true);
  175. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  176. }
  177. public function testRawPATCH() {
  178. $data = 'test';
  179. $request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
  180. $this->assertEquals(200, $request->status_code);
  181. $result = json_decode($request->body, true);
  182. $this->assertEquals('test', $result['data']);
  183. }
  184. public function testFormPATCH() {
  185. $data = 'test=true&test2=test';
  186. $request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
  187. $this->assertEquals(200, $request->status_code, $request->body);
  188. $result = json_decode($request->body, true);
  189. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  190. }
  191. public function testPATCHWithArray() {
  192. $data = array(
  193. 'test' => 'true',
  194. 'test2' => 'test',
  195. );
  196. $request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
  197. $this->assertEquals(200, $request->status_code);
  198. $result = json_decode($request->body, true);
  199. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  200. }
  201. public function testOPTIONS() {
  202. $request = Requests::options(httpbin('/options'), array(), array(), $this->getOptions());
  203. $this->assertEquals(200, $request->status_code);
  204. }
  205. public function testDELETE() {
  206. $request = Requests::delete(httpbin('/delete'), array(), $this->getOptions());
  207. $this->assertEquals(200, $request->status_code);
  208. $result = json_decode($request->body, true);
  209. $this->assertEquals(httpbin('/delete'), $result['url']);
  210. $this->assertEmpty($result['args']);
  211. }
  212. public function testDELETEWithData() {
  213. $data = array(
  214. 'test' => 'true',
  215. 'test2' => 'test',
  216. );
  217. $request = Requests::request(httpbin('/delete'), array(), $data, Requests::DELETE, $this->getOptions());
  218. $this->assertEquals(200, $request->status_code);
  219. $result = json_decode($request->body, true);
  220. $this->assertEquals(httpbin('/delete?test=true&test2=test'), $result['url']);
  221. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
  222. }
  223. public function testLOCK() {
  224. $request = Requests::request(httpbin('/lock'), array(), array(), 'LOCK', $this->getOptions());
  225. $this->assertEquals(200, $request->status_code);
  226. }
  227. public function testLOCKWithData() {
  228. $data = array(
  229. 'test' => 'true',
  230. 'test2' => 'test',
  231. );
  232. $request = Requests::request(httpbin('/lock'), array(), $data, 'LOCK', $this->getOptions());
  233. $this->assertEquals(200, $request->status_code);
  234. $result = json_decode($request->body, true);
  235. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  236. }
  237. public function testRedirects() {
  238. $request = Requests::get(httpbin('/redirect/6'), array(), $this->getOptions());
  239. $this->assertEquals(200, $request->status_code);
  240. $this->assertEquals(6, $request->redirects);
  241. }
  242. public function testRelativeRedirects() {
  243. $request = Requests::get(httpbin('/relative-redirect/6'), array(), $this->getOptions());
  244. $this->assertEquals(200, $request->status_code);
  245. $this->assertEquals(6, $request->redirects);
  246. }
  247. /**
  248. * @expectedException Requests_Exception
  249. * @todo This should also check that the type is "toomanyredirects"
  250. */
  251. public function testTooManyRedirects() {
  252. $options = array(
  253. 'redirects' => 10, // default, but force just in case
  254. );
  255. $request = Requests::get(httpbin('/redirect/11'), array(), $this->getOptions($options));
  256. }
  257. public static function statusCodeSuccessProvider() {
  258. return array(
  259. array(200, true),
  260. array(201, true),
  261. array(202, true),
  262. array(203, true),
  263. array(204, true),
  264. array(205, true),
  265. array(206, true),
  266. array(300, false),
  267. array(301, false),
  268. array(302, false),
  269. array(303, false),
  270. array(304, false),
  271. array(305, false),
  272. array(306, false),
  273. array(307, false),
  274. array(400, false),
  275. array(401, false),
  276. array(402, false),
  277. array(403, false),
  278. array(404, false),
  279. array(405, false),
  280. array(406, false),
  281. array(407, false),
  282. array(408, false),
  283. array(409, false),
  284. array(410, false),
  285. array(411, false),
  286. array(412, false),
  287. array(413, false),
  288. array(414, false),
  289. array(415, false),
  290. array(416, false),
  291. array(417, false),
  292. array(418, false), // RFC 2324
  293. array(428, false), // RFC 6585
  294. array(429, false), // RFC 6585
  295. array(431, false), // RFC 6585
  296. array(500, false),
  297. array(501, false),
  298. array(502, false),
  299. array(503, false),
  300. array(504, false),
  301. array(505, false),
  302. array(511, false), // RFC 6585
  303. );
  304. }
  305. /**
  306. * @dataProvider statusCodeSuccessProvider
  307. */
  308. public function testStatusCode($code, $success) {
  309. $transport = new MockTransport();
  310. $transport->code = $code;
  311. $url = sprintf(httpbin('/status/%d'), $code);
  312. $options = array(
  313. 'follow_redirects' => false,
  314. 'transport' => $transport,
  315. );
  316. $request = Requests::get($url, array(), $options);
  317. $this->assertEquals($code, $request->status_code);
  318. $this->assertEquals($success, $request->success);
  319. }
  320. /**
  321. * @dataProvider statusCodeSuccessProvider
  322. */
  323. public function testStatusCodeThrow($code, $success) {
  324. $transport = new MockTransport();
  325. $transport->code = $code;
  326. $url = sprintf(httpbin('/status/%d'), $code);
  327. $options = array(
  328. 'follow_redirects' => false,
  329. 'transport' => $transport,
  330. );
  331. if (!$success) {
  332. if ($code >= 400) {
  333. $this->setExpectedException('Requests_Exception_HTTP_' . $code, '', $code);
  334. }
  335. elseif ($code >= 300 && $code < 400) {
  336. $this->setExpectedException('Requests_Exception');
  337. }
  338. }
  339. $request = Requests::get($url, array(), $options);
  340. $request->throw_for_status(false);
  341. }
  342. /**
  343. * @dataProvider statusCodeSuccessProvider
  344. */
  345. public function testStatusCodeThrowAllowRedirects($code, $success) {
  346. $transport = new MockTransport();
  347. $transport->code = $code;
  348. $url = sprintf(httpbin('/status/%d'), $code);
  349. $options = array(
  350. 'follow_redirects' => false,
  351. 'transport' => $transport,
  352. );
  353. if (!$success) {
  354. if ($code >= 400 || $code === 304 || $code === 305 || $code === 306) {
  355. $this->setExpectedException('Requests_Exception_HTTP_' . $code, '', $code);
  356. }
  357. }
  358. $request = Requests::get($url, array(), $options);
  359. $request->throw_for_status(true);
  360. }
  361. public function testStatusCodeUnknown(){
  362. $transport = new MockTransport();
  363. $transport->code = 599;
  364. $options = array(
  365. 'transport' => $transport,
  366. );
  367. $request = Requests::get(httpbin('/status/599'), array(), $options);
  368. $this->assertEquals(599, $request->status_code);
  369. $this->assertEquals(false, $request->success);
  370. }
  371. /**
  372. * @expectedException Requests_Exception_HTTP_Unknown
  373. */
  374. public function testStatusCodeThrowUnknown(){
  375. $transport = new MockTransport();
  376. $transport->code = 599;
  377. $options = array(
  378. 'transport' => $transport,
  379. );
  380. $request = Requests::get(httpbin('/status/599'), array(), $options);
  381. $request->throw_for_status(true);
  382. }
  383. public function testGzipped() {
  384. $request = Requests::get(httpbin('/gzip'), array(), $this->getOptions());
  385. $this->assertEquals(200, $request->status_code);
  386. $result = json_decode($request->body);
  387. $this->assertEquals(true, $result->gzipped);
  388. }
  389. public function testStreamToFile() {
  390. $options = array(
  391. 'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
  392. );
  393. $request = Requests::get(httpbin('/get'), array(), $this->getOptions($options));
  394. $this->assertEquals(200, $request->status_code);
  395. $this->assertEmpty($request->body);
  396. $contents = file_get_contents($options['filename']);
  397. $result = json_decode($contents, true);
  398. $this->assertEquals(httpbin('/get'), $result['url']);
  399. $this->assertEmpty($result['args']);
  400. unlink($options['filename']);
  401. }
  402. public function testNonblocking() {
  403. $options = array(
  404. 'blocking' => false
  405. );
  406. $request = Requests::get(httpbin('/get'), array(), $this->getOptions($options));
  407. $empty = new Requests_Response();
  408. $this->assertEquals($empty, $request);
  409. }
  410. /**
  411. * @expectedException Requests_Exception
  412. */
  413. public function testBadIP() {
  414. $request = Requests::get('http://256.256.256.0/', array(), $this->getOptions());
  415. }
  416. public function testHTTPS() {
  417. if ($this->skip_https) {
  418. $this->markTestSkipped('SSL support is not available.');
  419. return;
  420. }
  421. $request = Requests::get(httpbin('/get', true), array(), $this->getOptions());
  422. $this->assertEquals(200, $request->status_code);
  423. $result = json_decode($request->body, true);
  424. // Disable, since httpbin always returns http
  425. // $this->assertEquals(httpbin('/get', true), $result['url']);
  426. $this->assertEmpty($result['args']);
  427. }
  428. /**
  429. * @expectedException Requests_Exception
  430. */
  431. public function testExpiredHTTPS() {
  432. if ($this->skip_https) {
  433. $this->markTestSkipped('SSL support is not available.');
  434. return;
  435. }
  436. $request = Requests::get('https://testssl-expire.disig.sk/index.en.html', array(), $this->getOptions());
  437. }
  438. /**
  439. * @expectedException Requests_Exception
  440. */
  441. public function testRevokedHTTPS() {
  442. if ($this->skip_https) {
  443. $this->markTestSkipped('SSL support is not available.');
  444. return;
  445. }
  446. $request = Requests::get('https://testssl-revoked.disig.sk/index.en.html', array(), $this->getOptions());
  447. }
  448. /**
  449. * Test that SSL fails with a bad certificate
  450. *
  451. * @expectedException Requests_Exception
  452. */
  453. public function testBadDomain() {
  454. if ($this->skip_https) {
  455. $this->markTestSkipped('SSL support is not available.');
  456. return;
  457. }
  458. $request = Requests::head('https://wrong.host.badssl.com/', array(), $this->getOptions());
  459. }
  460. /**
  461. * Test that the transport supports Server Name Indication with HTTPS
  462. *
  463. * badssl.com is used for SSL testing, and the common name is set to
  464. * `*.badssl.com` as such. Without alternate name support, this will fail
  465. * as `badssl.com` is only in the alternate name
  466. */
  467. public function testAlternateNameSupport() {
  468. if ($this->skip_https) {
  469. $this->markTestSkipped('SSL support is not available.');
  470. return;
  471. }
  472. $request = Requests::head('https://badssl.com/', array(), $this->getOptions());
  473. $this->assertEquals(200, $request->status_code);
  474. }
  475. /**
  476. * Test that the transport supports Server Name Indication with HTTPS
  477. *
  478. * feelingrestful.com (owned by hmn.md and used with permission) points to
  479. * CloudFlare, and will fail if SNI isn't sent.
  480. */
  481. public function testSNISupport() {
  482. if ($this->skip_https) {
  483. $this->markTestSkipped('SSL support is not available.');
  484. return;
  485. }
  486. $request = Requests::head('https://feelingrestful.com/', array(), $this->getOptions());
  487. $this->assertEquals(200, $request->status_code);
  488. }
  489. /**
  490. * @expectedException Requests_Exception
  491. */
  492. public function testTimeout() {
  493. $options = array(
  494. 'timeout' => 1,
  495. );
  496. $request = Requests::get(httpbin('/delay/10'), array(), $this->getOptions($options));
  497. var_dump($request);
  498. }
  499. public function testMultiple() {
  500. $requests = array(
  501. 'test1' => array(
  502. 'url' => httpbin('/get')
  503. ),
  504. 'test2' => array(
  505. 'url' => httpbin('/get')
  506. ),
  507. );
  508. $responses = Requests::request_multiple($requests, $this->getOptions());
  509. // test1
  510. $this->assertNotEmpty($responses['test1']);
  511. $this->assertInstanceOf('Requests_Response', $responses['test1']);
  512. $this->assertEquals(200, $responses['test1']->status_code);
  513. $result = json_decode($responses['test1']->body, true);
  514. $this->assertEquals(httpbin('/get'), $result['url']);
  515. $this->assertEmpty($result['args']);
  516. // test2
  517. $this->assertNotEmpty($responses['test2']);
  518. $this->assertInstanceOf('Requests_Response', $responses['test2']);
  519. $this->assertEquals(200, $responses['test2']->status_code);
  520. $result = json_decode($responses['test2']->body, true);
  521. $this->assertEquals(httpbin('/get'), $result['url']);
  522. $this->assertEmpty($result['args']);
  523. }
  524. public function testMultipleWithDifferingMethods() {
  525. $requests = array(
  526. 'get' => array(
  527. 'url' => httpbin('/get'),
  528. ),
  529. 'post' => array(
  530. 'url' => httpbin('/post'),
  531. 'type' => Requests::POST,
  532. 'data' => 'test',
  533. ),
  534. );
  535. $responses = Requests::request_multiple($requests, $this->getOptions());
  536. // get
  537. $this->assertEquals(200, $responses['get']->status_code);
  538. // post
  539. $this->assertEquals(200, $responses['post']->status_code);
  540. $result = json_decode($responses['post']->body, true);
  541. $this->assertEquals('test', $result['data']);
  542. }
  543. /**
  544. * @depends testTimeout
  545. */
  546. public function testMultipleWithFailure() {
  547. $requests = array(
  548. 'success' => array(
  549. 'url' => httpbin('/get'),
  550. ),
  551. 'timeout' => array(
  552. 'url' => httpbin('/delay/10'),
  553. 'options' => array(
  554. 'timeout' => 1,
  555. ),
  556. ),
  557. );
  558. $responses = Requests::request_multiple($requests, $this->getOptions());
  559. $this->assertEquals(200, $responses['success']->status_code);
  560. $this->assertInstanceOf('Requests_Exception', $responses['timeout']);
  561. }
  562. public function testMultipleUsingCallback() {
  563. $requests = array(
  564. 'get' => array(
  565. 'url' => httpbin('/get'),
  566. ),
  567. 'post' => array(
  568. 'url' => httpbin('/post'),
  569. 'type' => Requests::POST,
  570. 'data' => 'test',
  571. ),
  572. );
  573. $this->completed = array();
  574. $options = array(
  575. 'complete' => array($this, 'completeCallback'),
  576. );
  577. $responses = Requests::request_multiple($requests, $this->getOptions($options));
  578. $this->assertEquals($this->completed, $responses);
  579. $this->completed = array();
  580. }
  581. public function testMultipleUsingCallbackAndFailure() {
  582. $requests = array(
  583. 'success' => array(
  584. 'url' => httpbin('/get'),
  585. ),
  586. 'timeout' => array(
  587. 'url' => httpbin('/delay/10'),
  588. 'options' => array(
  589. 'timeout' => 1,
  590. ),
  591. ),
  592. );
  593. $this->completed = array();
  594. $options = array(
  595. 'complete' => array($this, 'completeCallback'),
  596. );
  597. $responses = Requests::request_multiple($requests, $this->getOptions($options));
  598. $this->assertEquals($this->completed, $responses);
  599. $this->completed = array();
  600. }
  601. public function completeCallback($response, $key) {
  602. $this->completed[$key] = $response;
  603. }
  604. public function testMultipleToFile() {
  605. $requests = array(
  606. 'get' => array(
  607. 'url' => httpbin('/get'),
  608. 'options' => array(
  609. 'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
  610. ),
  611. ),
  612. 'post' => array(
  613. 'url' => httpbin('/post'),
  614. 'type' => Requests::POST,
  615. 'data' => 'test',
  616. 'options' => array(
  617. 'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
  618. ),
  619. ),
  620. );
  621. $responses = Requests::request_multiple($requests, $this->getOptions());
  622. // GET request
  623. $contents = file_get_contents($requests['get']['options']['filename']);
  624. $result = json_decode($contents, true);
  625. $this->assertEquals(httpbin('/get'), $result['url']);
  626. $this->assertEmpty($result['args']);
  627. unlink($requests['get']['options']['filename']);
  628. // POST request
  629. $contents = file_get_contents($requests['post']['options']['filename']);
  630. $result = json_decode($contents, true);
  631. $this->assertEquals(httpbin('/post'), $result['url']);
  632. $this->assertEquals('test', $result['data']);
  633. unlink($requests['post']['options']['filename']);
  634. }
  635. public function testAlternatePort() {
  636. $request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions());
  637. $this->assertEquals(200, $request->status_code);
  638. $num = preg_match('#You have reached this page on port <b>(\d+)</b>#i', $request->body, $matches);
  639. $this->assertEquals(1, $num, 'Response should contain the port number');
  640. $this->assertEquals(8080, $matches[1]);
  641. }
  642. public function testProgressCallback() {
  643. $mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock();
  644. $mock->expects($this->atLeastOnce())->method('progress');
  645. $hooks = new Requests_Hooks();
  646. $hooks->register('request.progress', array($mock, 'progress'));
  647. $options = array(
  648. 'hooks' => $hooks,
  649. );
  650. $options = $this->getOptions($options);
  651. $response = Requests::get(httpbin('/get'), array(), $options);
  652. }
  653. public function testAfterRequestCallback() {
  654. $mock = $this->getMockBuilder('stdClass')
  655. ->setMethods(array('after_request'))
  656. ->getMock();
  657. $mock->expects($this->atLeastOnce())
  658. ->method('after_request')
  659. ->with(
  660. $this->isType('string'),
  661. $this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty()))
  662. );
  663. $hooks = new Requests_Hooks();
  664. $hooks->register('curl.after_request', array($mock, 'after_request'));
  665. $hooks->register('fsockopen.after_request', array($mock, 'after_request'));
  666. $options = array(
  667. 'hooks' => $hooks,
  668. );
  669. $options = $this->getOptions($options);
  670. $response = Requests::get(httpbin('/get'), array(), $options);
  671. }
  672. public function testReusableTransport() {
  673. $options = $this->getOptions(array('transport' => new $this->transport()));
  674. $request1 = Requests::get(httpbin('/get'), array(), $options);
  675. $request2 = Requests::get(httpbin('/get'), array(), $options);
  676. $this->assertEquals(200, $request1->status_code);
  677. $this->assertEquals(200, $request2->status_code);
  678. $result1 = json_decode($request1->body, true);
  679. $result2 = json_decode($request2->body, true);
  680. $this->assertEquals(httpbin('/get'), $result1['url']);
  681. $this->assertEquals(httpbin('/get'), $result2['url']);
  682. $this->assertEmpty($result1['args']);
  683. $this->assertEmpty($result2['args']);
  684. }
  685. public function testQueryDataFormat() {
  686. $data = array('test' => 'true', 'test2' => 'test');
  687. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'query')));
  688. $this->assertEquals(200, $request->status_code);
  689. $result = json_decode($request->body, true);
  690. $this->assertEquals(httpbin('/post').'?test=true&test2=test', $result['url']);
  691. $this->assertEquals('', $result['data']);
  692. }
  693. public function testBodyDataFormat() {
  694. $data = array('test' => 'true', 'test2' => 'test');
  695. $request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'body')));
  696. $this->assertEquals(200, $request->status_code);
  697. $result = json_decode($request->body, true);
  698. $this->assertEquals(httpbin('/post'), $result['url']);
  699. $this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
  700. }
  701. }