httpMockCest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. <?php
  2. //use namespaces
  3. use Mcustiel\Phiremock\Client\Phiremock;
  4. use Mcustiel\Phiremock\Client\Utils\A;
  5. use Mcustiel\Phiremock\Client\Utils\Respond;
  6. use Mcustiel\Phiremock\Client\Utils\Is;
  7. /**
  8. * Class httpMockCest
  9. */
  10. class httpMockCest
  11. {
  12. // ############################################### private class vars // ###########################################
  13. /**
  14. * Holds instance of yii2-curl
  15. * @type linslin\yii2\curl\Curl
  16. */
  17. private $_curl = null;
  18. /**
  19. * Default test server endpoint URL
  20. * @type string
  21. */
  22. private $_endPoint = 'http://127.0.0.1:18080';
  23. // ################################################## class methods // #############################################
  24. /**
  25. * Cleanup
  26. * @param \FunctionalTester $I
  27. */
  28. public function _before(\FunctionalTester $I)
  29. {
  30. $I->haveACleanSetupInRemoteService();
  31. //Init curl
  32. $this->_curl = new linslin\yii2\curl\Curl();
  33. }
  34. /**
  35. * Simple HTTP ok
  36. * @param FunctionalTester $I
  37. * @throws Exception
  38. */
  39. public function simpleHttpOkTest(\FunctionalTester $I)
  40. {
  41. $I->expectARequestToRemoteServiceWithAResponse(
  42. Phiremock::on(
  43. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/200'))
  44. )->then(
  45. Respond::withStatusCode(200)
  46. )
  47. );
  48. $this->_curl->get($this->_endPoint . '/test/httpStatus/200');
  49. $I->assertEquals($this->_curl->responseCode, 200);
  50. }
  51. /**
  52. * Try set params to send with get request
  53. * @param FunctionalTester $I
  54. * @throws Exception
  55. */
  56. public function setGetParamsTest(\FunctionalTester $I)
  57. {
  58. //Init
  59. $this->_curl->reset();
  60. $params = [
  61. 'key' => 'value',
  62. 'secondKey' => 'secondValue'
  63. ];
  64. $I->expectARequestToRemoteServiceWithAResponse(
  65. Phiremock::on(
  66. A::getRequest()->andUrl(Is::equalTo('/test/params/get?' . http_build_query($params)))
  67. )->then(
  68. Respond::withStatusCode(200)
  69. )
  70. );
  71. $this->_curl->setGetParams($params)
  72. ->get($this->_endPoint . '/test/params/get');
  73. $I->assertEquals($this->_curl->responseCode, 200);
  74. $I->assertEquals($this->_curl->getUrl(), $this->_endPoint . '/test/params/get?' . http_build_query($params));
  75. }
  76. /**
  77. * Try set post to send with post request
  78. * @param FunctionalTester $I
  79. * @throws Exception
  80. */
  81. public function setPostParamsTest(\FunctionalTester $I)
  82. {
  83. //Init
  84. $this->_curl->reset();
  85. $params = [
  86. 'key' => 'value',
  87. 'secondKey' => 'secondValue'
  88. ];
  89. $I->expectARequestToRemoteServiceWithAResponse(
  90. $expectation = Phiremock::on(
  91. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  92. ->andBody(Is::equalTo(http_build_query($params)))
  93. ->andHeader('Content-Type', Is::equalTo('application/x-www-form-urlencoded'))
  94. )->then(
  95. Respond::withStatusCode(200)
  96. )
  97. );
  98. $this->_curl->setPostParams($params)
  99. ->post($this->_endPoint . '/test/params/post');
  100. $I->assertEquals($this->_curl->responseCode, 200);
  101. }
  102. /**
  103. * Try set post to send with post request
  104. * @param FunctionalTester $I
  105. * @throws Exception
  106. */
  107. public function setPostParamsOptionTest(\FunctionalTester $I)
  108. {
  109. //Init
  110. $this->_curl->reset();
  111. $params = [
  112. 'key' => 'value',
  113. 'secondKey' => 'secondValue'
  114. ];
  115. $I->expectARequestToRemoteServiceWithAResponse(
  116. $expectation = Phiremock::on(
  117. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  118. ->andBody(Is::equalTo(http_build_query($params)))
  119. ->andHeader('Content-Type', Is::equalTo('application/x-www-form-urlencoded'))
  120. )->then(
  121. Respond::withStatusCode(200)
  122. )
  123. );
  124. $this->_curl->setOption(
  125. CURLOPT_POSTFIELDS,
  126. http_build_query($params))
  127. ->post($this->_endPoint . '/test/params/post');
  128. $I->assertEquals($this->_curl->responseCode, 200);
  129. }
  130. /**
  131. * Try set post param with header modification
  132. * @param FunctionalTester $I
  133. * @throws Exception
  134. */
  135. public function setPostParamsWithHeaderTest(\FunctionalTester $I)
  136. {
  137. //Init
  138. $this->_curl->reset();
  139. $params = [
  140. 'key' => 'value',
  141. 'secondKey' => 'secondValue'
  142. ];
  143. $I->expectARequestToRemoteServiceWithAResponse(
  144. $expectation = Phiremock::on(
  145. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  146. ->andBody(Is::equalTo(http_build_query($params)))
  147. ->andHeader('Content-Type', Is::equalTo('application/json'))
  148. )->then(
  149. Respond::withStatusCode(200)
  150. )
  151. );
  152. $this->_curl->setPostParams($params)
  153. ->setHeaders([
  154. 'Content-Type' => 'application/json'
  155. ])
  156. ->post($this->_endPoint . '/test/params/post');
  157. $I->assertEquals($this->_curl->responseCode, 200);
  158. }
  159. /**
  160. * Post JSON data test
  161. * @param FunctionalTester $I
  162. * @throws Exception
  163. */
  164. public function postJsonTest(\FunctionalTester $I)
  165. {
  166. //Init
  167. $this->_curl->reset();
  168. $params = [
  169. 'key' => 'value',
  170. 'secondKey' => 'secondValue'
  171. ];
  172. $I->expectARequestToRemoteServiceWithAResponse(
  173. $expectation = Phiremock::on(
  174. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  175. ->andBody(Is::equalTo(json_encode($params)))
  176. ->andHeader('content-type', Is::equalTo('application/json'))
  177. )->then(
  178. Respond::withStatusCode(200)
  179. ->andBody('{"id": 1, "description": "I am a resource"}')
  180. )
  181. );
  182. $this->_curl->setRequestBody(json_encode($params))
  183. ->setHeaders([
  184. 'content-type' => 'application/json',
  185. 'content-length' => strlen(json_encode($params))
  186. ])
  187. ->post($this->_endPoint . '/test/params/post');
  188. $I->assertEquals($this->_curl->responseCode, 200);
  189. }
  190. /**
  191. * Get JSON response test
  192. * @param FunctionalTester $I
  193. * @throws Exception
  194. */
  195. public function getWithDecodedJsonResponseTest(\FunctionalTester $I)
  196. {
  197. //Init
  198. $this->_curl->reset();
  199. $I->expectARequestToRemoteServiceWithAResponse(
  200. $expectation = Phiremock::on(
  201. A::getRequest()->andUrl(Is::equalTo('/test/params/get/json'))
  202. )->then(
  203. Respond::withStatusCode(200)
  204. ->andBody('{"id": 1, "description": "I am a resource"}')
  205. )
  206. );
  207. $jsonResponse = $this->_curl->get($this->_endPoint . '/test/params/get/json', false);
  208. $I->assertEquals($this->_curl->responseCode, 200);
  209. $I->assertArrayHasKey('id', $jsonResponse);
  210. $I->assertArrayHasKey('description', $jsonResponse);
  211. $I->assertEquals($jsonResponse['id'], 1);
  212. $I->assertEquals($jsonResponse['description'], 'I am a resource');
  213. }
  214. /**
  215. * Get JSON response test
  216. * @param FunctionalTester $I
  217. * @throws Exception
  218. */
  219. public function getWithRawJsonResponseTest(\FunctionalTester $I)
  220. {
  221. //Init
  222. $this->_curl->reset();
  223. $I->expectARequestToRemoteServiceWithAResponse(
  224. $expectation = Phiremock::on(
  225. A::getRequest()->andUrl(Is::equalTo('/test/params/get/json'))
  226. )->then(
  227. Respond::withStatusCode(200)
  228. ->andBody('{"id": 1, "description": "I am a resource"}')
  229. )
  230. );
  231. $rawResponse = $this->_curl->get($this->_endPoint . '/test/params/get/json', true);
  232. $I->assertEquals($this->_curl->responseCode, 200);
  233. $I->assertEquals($rawResponse, '{"id": 1, "description": "I am a resource"}');
  234. }
  235. /**
  236. * Get header params with special header separators in values
  237. * @issue https://github.com/linslin/Yii2-Curl/issues/59
  238. *
  239. * @param FunctionalTester $I
  240. * @throws Exception
  241. */
  242. public function getHeaderParamWithSpecialHeaderSeparatorInValue(\FunctionalTester $I)
  243. {
  244. //Init
  245. $this->_curl->reset();
  246. $I->expectARequestToRemoteServiceWithAResponse(
  247. Phiremock::on(
  248. A::getRequest()->andUrl(Is::equalTo('/test/header'))
  249. )->then(
  250. Respond::withStatusCode(200)
  251. ->andHeader('param', 'value')
  252. ->andHeader('location', 'http://somelocation/')
  253. )
  254. );
  255. $this->_curl->get($this->_endPoint . '/test/header');
  256. $I->assertEquals($this->_curl->responseCode, 200);
  257. $I->assertEquals($this->_curl->responseHeaders['location'], 'http://somelocation/');
  258. $I->assertEquals($this->_curl->responseHeaders['param'], 'value');
  259. }
  260. /**
  261. * Default head method test
  262. * @param FunctionalTester $I
  263. * @throws Exception
  264. */
  265. public function defaultHeadMethodTest(\FunctionalTester $I)
  266. {
  267. //Init
  268. $this->_curl->reset();
  269. $I->expectARequestToRemoteServiceWithAResponse(
  270. Phiremock::on(
  271. A::headRequest()->andUrl(Is::equalTo('/test/head'))
  272. )->then(
  273. Respond::withStatusCode(200)
  274. )
  275. );
  276. $this->_curl->head($this->_endPoint . '/test/head');
  277. $I->assertEquals($this->_curl->responseCode, 200);
  278. }
  279. /**
  280. * Default delete method test
  281. * @param FunctionalTester $I
  282. * @throws Exception
  283. */
  284. public function defaultDeleteMethodTest(\FunctionalTester $I)
  285. {
  286. //Init
  287. $this->_curl->reset();
  288. $I->expectARequestToRemoteServiceWithAResponse(
  289. Phiremock::on(
  290. A::deleteRequest()->andUrl(Is::equalTo('/test/head'))
  291. )->then(
  292. Respond::withStatusCode(200)
  293. )
  294. );
  295. $this->_curl->delete($this->_endPoint . '/test/head');
  296. $I->assertEquals($this->_curl->responseCode, 200);
  297. }
  298. /**
  299. * Default patch method test
  300. * @param FunctionalTester $I
  301. * @throws Exception
  302. */
  303. public function defaultPatchMethodTest(\FunctionalTester $I)
  304. {
  305. //Init
  306. $this->_curl->reset();
  307. $I->expectARequestToRemoteServiceWithAResponse(
  308. Phiremock::on(
  309. A::patchRequest()->andUrl(Is::equalTo('/test/head'))
  310. ->andHeader('X-HTTP-Method-Override', Is::equalTo('PATCH'))
  311. )->then(
  312. Respond::withStatusCode(200)
  313. )
  314. );
  315. $this->_curl->patch($this->_endPoint . '/test/head');
  316. $I->assertEquals($this->_curl->responseCode, 200);
  317. }
  318. /**
  319. * Default put method test
  320. * @param FunctionalTester $I
  321. * @throws Exception
  322. */
  323. public function defaultPutMethodTest(\FunctionalTester $I)
  324. {
  325. //Init
  326. $this->_curl->reset();
  327. $I->expectARequestToRemoteServiceWithAResponse(
  328. Phiremock::on(
  329. A::putRequest()->andUrl(Is::equalTo('/test/head'))
  330. )->then(
  331. Respond::withStatusCode(200)
  332. )
  333. );
  334. $this->_curl->put($this->_endPoint . '/test/head');
  335. $I->assertEquals($this->_curl->responseCode, 200);
  336. }
  337. /**
  338. * Set single option test
  339. * @param FunctionalTester $I
  340. * @throws Exception
  341. */
  342. public function setSingleDefaultOptionTest(\FunctionalTester $I)
  343. {
  344. //Init
  345. $this->_curl->reset();
  346. $params = [
  347. 'key' => 'value',
  348. 'secondKey' => 'secondValue'
  349. ];
  350. $I->expectARequestToRemoteServiceWithAResponse(
  351. $expectation = Phiremock::on(
  352. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  353. ->andBody(Is::equalTo(http_build_query($params)))
  354. ->andHeader('Content-Type', Is::equalTo('application/x-www-form-urlencoded'))
  355. ->andHeader('user-agent', Is::equalTo('my-agent'))
  356. )->then(
  357. Respond::withStatusCode(200)
  358. )
  359. );
  360. $this->_curl->setOption(CURLOPT_USERAGENT, 'my-agent')
  361. ->setPostParams($params)
  362. ->post($this->_endPoint . '/test/params/post');
  363. $I->assertEquals($this->_curl->responseCode, 200);
  364. }
  365. /**
  366. * Set multiple option test
  367. * @param FunctionalTester $I
  368. * @throws Exception
  369. */
  370. public function setMultipleOptionsTest(\FunctionalTester $I)
  371. {
  372. //Init
  373. $this->_curl->reset();
  374. $params = [
  375. 'key' => 'value',
  376. 'secondKey' => 'secondValue'
  377. ];
  378. $I->expectARequestToRemoteServiceWithAResponse(
  379. $expectation = Phiremock::on(
  380. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  381. ->andBody(Is::equalTo(http_build_query($params)))
  382. ->andHeader('Content-Type', Is::equalTo('application/x-www-form-urlencoded'))
  383. )->then(
  384. Respond::withStatusCode(200)
  385. )
  386. );
  387. $this->_curl->setOptions([
  388. CURLOPT_USERAGENT => 'my-agent',
  389. CURLOPT_POSTFIELDS => http_build_query($params)
  390. ])
  391. ->post($this->_endPoint . '/test/params/post');
  392. $I->assertEquals($this->_curl->responseCode, 200);
  393. }
  394. /**
  395. * Set and unset option test
  396. * @param FunctionalTester $I
  397. * @throws Exception
  398. */
  399. public function setUnsetSingleOptionTest(\FunctionalTester $I)
  400. {
  401. //Init
  402. $this->_curl->reset();
  403. $params = [
  404. 'key' => 'value',
  405. 'secondKey' => 'secondValue'
  406. ];
  407. $I->expectARequestToRemoteServiceWithAResponse(
  408. $expectation = Phiremock::on(
  409. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  410. ->andBody(Is::equalTo(''))
  411. )->then(
  412. Respond::withStatusCode(200)
  413. )
  414. );
  415. $this->_curl->setOption(CURLOPT_POSTFIELDS, http_build_query($params))
  416. ->unsetOption(CURLOPT_POSTFIELDS)
  417. ->post($this->_endPoint . '/test/params/post');
  418. $I->assertEquals($this->_curl->responseCode, 200);
  419. }
  420. /**
  421. * Set and unset all options test
  422. * @param FunctionalTester $I
  423. * @throws Exception
  424. */
  425. public function setAllAndUnsertOptionsTest(\FunctionalTester $I)
  426. {
  427. //Init
  428. $this->_curl->reset();
  429. $params = [
  430. 'key' => 'value',
  431. 'secondKey' => 'secondValue'
  432. ];
  433. $I->expectARequestToRemoteServiceWithAResponse(
  434. $expectation = Phiremock::on(
  435. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  436. ->andBody(Is::equalTo(''))
  437. )->then(
  438. Respond::withStatusCode(200)
  439. )
  440. );
  441. $this->_curl->setOption(CURLOPT_POSTFIELDS, http_build_query($params))
  442. ->unsetOptions()
  443. ->post($this->_endPoint . '/test/params/post');
  444. $I->assertEquals($this->_curl->responseCode, 200);
  445. }
  446. /**
  447. * Simple reset after request test
  448. * @param FunctionalTester $I
  449. * @throws Exception
  450. */
  451. public function resetAfterGet(\FunctionalTester $I)
  452. {
  453. $I->expectARequestToRemoteServiceWithAResponse(
  454. Phiremock::on(
  455. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/200'))
  456. )->then(
  457. Respond::withStatusCode(200)
  458. )
  459. );
  460. $this->_curl->get($this->_endPoint . '/test/httpStatus/200');
  461. $I->assertEquals($this->_curl->responseCode, 200);
  462. $this->_curl->reset();
  463. $I->assertEquals($this->_curl->curl, null);
  464. }
  465. /**
  466. * Simple get info test
  467. * @param FunctionalTester $I
  468. * @throws Exception
  469. */
  470. public function getInfo(\FunctionalTester $I)
  471. {
  472. $I->expectARequestToRemoteServiceWithAResponse(
  473. Phiremock::on(
  474. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/200'))
  475. )->then(
  476. Respond::withStatusCode(200)
  477. )
  478. );
  479. $this->_curl->get($this->_endPoint . '/test/httpStatus/200');
  480. $I->assertEquals($this->_curl->responseCode, 200);
  481. $this->_curl->getInfo();
  482. }
  483. /**
  484. * Simple get info without curl test
  485. * @param \FunctionalTester $I
  486. */
  487. public function getInfoWithoutCurl(\FunctionalTester $I)
  488. {
  489. $I->assertEquals($this->_curl->getInfo(CURLINFO_HEADER_SIZE ), []);
  490. }
  491. /**
  492. * Simple curl timeout test
  493. * @param FunctionalTester $I
  494. * @throws Exception
  495. */
  496. public function defaultCurlTimeoutError(\FunctionalTester $I)
  497. {
  498. $I->expectARequestToRemoteServiceWithAResponse(
  499. Phiremock::on(
  500. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/timeout'))
  501. )->then(
  502. Respond::withStatusCode(404)
  503. ->andDelayInMillis(2000)
  504. )
  505. );
  506. $this->_curl->setOption(CURLOPT_TIMEOUT, 1)
  507. ->get($this->_endPoint . '/test/httpStatus/timeout');
  508. $I->assertEquals($this->_curl->responseCode, 'timeout');
  509. }
  510. /**
  511. * Simple get without head request
  512. * @param FunctionalTester $I
  513. * @throws Exception
  514. */
  515. public function simpleGetWithoutHead(\FunctionalTester $I)
  516. {
  517. $I->expectARequestToRemoteServiceWithAResponse(
  518. Phiremock::on(
  519. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/200'))
  520. )->then(
  521. Respond::withStatusCode(200)
  522. )
  523. );
  524. $this->_curl
  525. ->setOption(CURLOPT_HEADER, false)
  526. ->get($this->_endPoint . '/test/httpStatus/200');
  527. $I->assertEquals($this->_curl->responseCode, 200);
  528. }
  529. /**
  530. * Simple curl error test CURL_UNSUPPORTED_PROTOCOL
  531. * @param FunctionalTester $I
  532. * @throws Exception
  533. */
  534. public function defaultCurlError(\FunctionalTester $I)
  535. {
  536. $this->_curl->get( 'messy:/test/httpStatus/timeout');
  537. $I->assertEquals($this->_curl->responseCode, null);
  538. $I->assertLessOrEquals($this->_curl->errorCode, 1);
  539. }
  540. /**
  541. * Default charset extract test
  542. * @param FunctionalTester $I
  543. * @throws Exception
  544. */
  545. public function defaultCharsetExtractTest(\FunctionalTester $I)
  546. {
  547. $I->expectARequestToRemoteServiceWithAResponse(
  548. Phiremock::on(
  549. A::getRequest()->andUrl(Is::equalTo('/test/httpStatus/header'))
  550. )->then(
  551. Respond::withStatusCode(200)
  552. ->andHeader('Content-Type', Is::equalTo('application/x-javascript;charset=UTF-8'))
  553. )
  554. );
  555. $this->_curl
  556. ->get($this->_endPoint . '/test/httpStatus/header');
  557. $I->assertEquals($this->_curl->responseCharset, 'utf-8');
  558. }
  559. /**
  560. * Try set a header param and check if getHeaders() does return it
  561. * @param FunctionalTester $I
  562. * @throws Exception
  563. */
  564. public function setHeaderParamAndTestGetHeaders(\FunctionalTester $I)
  565. {
  566. //Init
  567. $this->_curl->reset();
  568. $params = [
  569. 'key' => 'value',
  570. 'secondKey' => 'secondValue'
  571. ];
  572. $I->expectARequestToRemoteServiceWithAResponse(
  573. $expectation = Phiremock::on(
  574. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  575. ->andBody(Is::equalTo(http_build_query($params)))
  576. ->andHeader('Content-Type', Is::equalTo('application/json'))
  577. )->then(
  578. Respond::withStatusCode(200)
  579. )
  580. );
  581. $this->_curl->setPostParams($params)
  582. ->setHeaders([
  583. 'Content-Type' => 'application/json'
  584. ])
  585. ->post($this->_endPoint . '/test/params/post');
  586. //check for count
  587. $I->assertEquals(count($this->_curl->getRequestHeaders()), 1);
  588. //check for value
  589. $requestHeaders = $this->_curl->getRequestHeaders();
  590. $I->assertEquals($requestHeaders['Content-Type'], 'application/json');
  591. }
  592. /**
  593. * Try set a header param and check if getHeader() does return it
  594. * @param FunctionalTester $I
  595. * @throws Exception
  596. */
  597. public function setHeaderParamAndTestGetHeader(\FunctionalTester $I)
  598. {
  599. //Init
  600. $this->_curl->reset();
  601. $params = [
  602. 'key' => 'value',
  603. 'secondKey' => 'secondValue'
  604. ];
  605. $I->expectARequestToRemoteServiceWithAResponse(
  606. $expectation = Phiremock::on(
  607. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  608. ->andBody(Is::equalTo(http_build_query($params)))
  609. ->andHeader('Content-Type', Is::equalTo('application/json'))
  610. )->then(
  611. Respond::withStatusCode(200)
  612. )
  613. );
  614. $this->_curl->setPostParams($params)
  615. ->setHeaders([
  616. 'Content-Type' => 'application/json'
  617. ])
  618. ->post($this->_endPoint . '/test/params/post');
  619. //check for value
  620. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  621. }
  622. /**
  623. * Try set a single header param and check if getRequestHeader() does return it
  624. * @param FunctionalTester $I
  625. * @throws Exception
  626. */
  627. public function setSingleHeaderParamAndTestGetHeader(\FunctionalTester $I)
  628. {
  629. //Init
  630. $this->_curl->reset();
  631. $params = [
  632. 'key' => 'value',
  633. 'secondKey' => 'secondValue'
  634. ];
  635. $I->expectARequestToRemoteServiceWithAResponse(
  636. $expectation = Phiremock::on(
  637. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  638. ->andBody(Is::equalTo(http_build_query($params)))
  639. ->andHeader('Content-Type', Is::equalTo('application/json'))
  640. )->then(
  641. Respond::withStatusCode(200)
  642. )
  643. );
  644. $this->_curl->setPostParams($params)
  645. ->setHeader('Content-Type', 'application/json')
  646. ->post($this->_endPoint . '/test/params/post');
  647. //check for value
  648. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  649. }
  650. /**
  651. * Try set a single header and multiple headers at once and check if getRequestHeader() does return it
  652. * @param FunctionalTester $I
  653. * @throws Exception
  654. */
  655. public function setSingleHeaderAndMultipleHeadersAndTestGetHeader(\FunctionalTester $I)
  656. {
  657. //Init
  658. $this->_curl->reset();
  659. $params = [
  660. 'key' => 'value',
  661. 'secondKey' => 'secondValue'
  662. ];
  663. $I->expectARequestToRemoteServiceWithAResponse(
  664. $expectation = Phiremock::on(
  665. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  666. ->andBody(Is::equalTo(http_build_query($params)))
  667. ->andHeader('Content-Type', Is::equalTo('application/json'))
  668. ->andHeader('custom-type', Is::equalTo('><)#7?aJEvgavJk(*4'))
  669. )->then(
  670. Respond::withStatusCode(200)
  671. )
  672. );
  673. $this->_curl->setPostParams($params)
  674. ->setHeader('Content-Type', 'application/json')
  675. ->setHeaders([
  676. 'custom-type' => '><)#7?aJEvgavJk(*4'
  677. ])
  678. ->post($this->_endPoint . '/test/params/post');
  679. //check for value
  680. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  681. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), '><)#7?aJEvgavJk(*4');
  682. }
  683. /**
  684. * Try set a single header, multiple header and unset one header param and check if getRequestHeader() does return it
  685. * @param FunctionalTester $I
  686. * @throws Exception
  687. */
  688. public function setSingleHeaderAndMultipleHeadersAndUnsetOneTillTestGetHeader(\FunctionalTester $I)
  689. {
  690. //Init
  691. $this->_curl->reset();
  692. $params = [
  693. 'key' => 'value',
  694. 'secondKey' => 'secondValue'
  695. ];
  696. $I->expectARequestToRemoteServiceWithAResponse(
  697. $expectation = Phiremock::on(
  698. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  699. ->andBody(Is::equalTo(http_build_query($params)))
  700. ->andHeader('Content-Type', Is::equalTo('application/json'))
  701. )->then(
  702. Respond::withStatusCode(200)
  703. )
  704. );
  705. $this->_curl->setPostParams($params)
  706. ->setHeader('Content-Type', 'application/json')
  707. ->setHeaders([
  708. 'custom-type' => '><)#7?aJEvgavJk(*4'
  709. ])
  710. ->unsetHeader('custom-type')
  711. ->post($this->_endPoint . '/test/params/post');
  712. //check for value
  713. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  714. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null);
  715. }
  716. /**
  717. * Try set a single header, multiple header and unset one header param and check if getRequestHeader() does return it
  718. * @param FunctionalTester $I
  719. * @throws Exception
  720. */
  721. public function setMultipleHeadersAndSingleHeaderAndUnsetOneTillTestGetHeader(\FunctionalTester $I)
  722. {
  723. //Init
  724. $this->_curl->reset();
  725. $params = [
  726. 'key' => 'value',
  727. 'secondKey' => 'secondValue'
  728. ];
  729. $I->expectARequestToRemoteServiceWithAResponse(
  730. $expectation = Phiremock::on(
  731. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  732. ->andBody(Is::equalTo(http_build_query($params)))
  733. ->andHeader('Content-Type', Is::equalTo('application/json'))
  734. )->then(
  735. Respond::withStatusCode(200)
  736. )
  737. );
  738. $this->_curl->setPostParams($params)
  739. ->setHeaders([
  740. 'custom-type' => '><)#7?aJEvgavJk(*4'
  741. ])
  742. ->setHeader('Content-Type', 'application/json')
  743. ->unsetHeader('custom-type')
  744. ->post($this->_endPoint . '/test/params/post');
  745. //check for value
  746. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  747. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null);
  748. }
  749. /**
  750. * Try to post raw json string
  751. * @param FunctionalTester $I
  752. * @throws Exception
  753. */
  754. public function setRawPostDataTest (\FunctionalTester $I)
  755. {
  756. //Init
  757. $this->_curl->reset();
  758. $params = [
  759. 'key' => 'value',
  760. 'secondKey' => 'secondValue'
  761. ];
  762. $I->expectARequestToRemoteServiceWithAResponse(
  763. $expectation = Phiremock::on(
  764. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  765. ->andBody(Is::equalTo(json_encode($params)))
  766. ->andHeader('Content-Type', Is::equalTo('application/json'))
  767. )->then(
  768. Respond::withStatusCode(200)
  769. )
  770. );
  771. $this->_curl->setRawPostData(json_encode($params))
  772. ->setHeader('Content-Type', 'application/json')
  773. ->post($this->_endPoint . '/test/params/post');
  774. //check for value
  775. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  776. }
  777. }