httpMockCest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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', 'application/x-javascript;charset=UTF-8')
  553. )
  554. );
  555. $this->_curl->get($this->_endPoint . '/test/httpStatus/header');
  556. $I->assertEquals($this->_curl->responseCharset, 'utf-8');
  557. }
  558. /**
  559. * Try set a header param and check if getHeaders() does return it
  560. * @param FunctionalTester $I
  561. * @throws Exception
  562. */
  563. public function setHeaderParamAndTestGetHeaders(\FunctionalTester $I)
  564. {
  565. //Init
  566. $this->_curl->reset();
  567. $params = [
  568. 'key' => 'value',
  569. 'secondKey' => 'secondValue'
  570. ];
  571. $I->expectARequestToRemoteServiceWithAResponse(
  572. $expectation = Phiremock::on(
  573. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  574. ->andBody(Is::equalTo(http_build_query($params)))
  575. ->andHeader('Content-Type', Is::equalTo('application/json'))
  576. )->then(
  577. Respond::withStatusCode(200)
  578. )
  579. );
  580. $this->_curl->setPostParams($params)
  581. ->setHeaders([
  582. 'Content-Type' => 'application/json'
  583. ])
  584. ->post($this->_endPoint . '/test/params/post');
  585. //check for count
  586. $I->assertEquals(count($this->_curl->getRequestHeaders()), 1);
  587. //check for value
  588. $requestHeaders = $this->_curl->getRequestHeaders();
  589. $I->assertEquals($requestHeaders['Content-Type'], 'application/json');
  590. }
  591. /**
  592. * Try set a header param and check if getHeader() does return it
  593. * @param FunctionalTester $I
  594. * @throws Exception
  595. */
  596. public function setHeaderParamAndTestGetHeader(\FunctionalTester $I)
  597. {
  598. //Init
  599. $this->_curl->reset();
  600. $params = [
  601. 'key' => 'value',
  602. 'secondKey' => 'secondValue'
  603. ];
  604. $I->expectARequestToRemoteServiceWithAResponse(
  605. $expectation = Phiremock::on(
  606. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  607. ->andBody(Is::equalTo(http_build_query($params)))
  608. ->andHeader('Content-Type', Is::equalTo('application/json'))
  609. )->then(
  610. Respond::withStatusCode(200)
  611. )
  612. );
  613. $this->_curl->setPostParams($params)
  614. ->setHeaders([
  615. 'Content-Type' => 'application/json'
  616. ])
  617. ->post($this->_endPoint . '/test/params/post');
  618. //check for value
  619. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  620. }
  621. /**
  622. * Try set a single header param and check if getRequestHeader() does return it
  623. * @param FunctionalTester $I
  624. * @throws Exception
  625. */
  626. public function setSingleHeaderParamAndTestGetHeader(\FunctionalTester $I)
  627. {
  628. //Init
  629. $this->_curl->reset();
  630. $params = [
  631. 'key' => 'value',
  632. 'secondKey' => 'secondValue'
  633. ];
  634. $I->expectARequestToRemoteServiceWithAResponse(
  635. $expectation = Phiremock::on(
  636. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  637. ->andBody(Is::equalTo(http_build_query($params)))
  638. ->andHeader('Content-Type', Is::equalTo('application/json'))
  639. )->then(
  640. Respond::withStatusCode(200)
  641. )
  642. );
  643. $this->_curl->setPostParams($params)
  644. ->setHeader('Content-Type', 'application/json')
  645. ->post($this->_endPoint . '/test/params/post');
  646. //check for value
  647. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  648. }
  649. /**
  650. * Try set a single header and multiple headers at once and check if getRequestHeader() does return it
  651. * @param FunctionalTester $I
  652. * @throws Exception
  653. */
  654. public function setSingleHeaderAndMultipleHeadersAndTestGetHeader(\FunctionalTester $I)
  655. {
  656. //Init
  657. $this->_curl->reset();
  658. $params = [
  659. 'key' => 'value',
  660. 'secondKey' => 'secondValue'
  661. ];
  662. $I->expectARequestToRemoteServiceWithAResponse(
  663. $expectation = Phiremock::on(
  664. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  665. ->andBody(Is::equalTo(http_build_query($params)))
  666. ->andHeader('Content-Type', Is::equalTo('application/json'))
  667. ->andHeader('custom-type', Is::equalTo('><)#7?aJEvgavJk(*4'))
  668. )->then(
  669. Respond::withStatusCode(200)
  670. )
  671. );
  672. $this->_curl->setPostParams($params)
  673. ->setHeader('Content-Type', 'application/json')
  674. ->setHeaders([
  675. 'custom-type' => '><)#7?aJEvgavJk(*4'
  676. ])
  677. ->post($this->_endPoint . '/test/params/post');
  678. //check for value
  679. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  680. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), '><)#7?aJEvgavJk(*4');
  681. }
  682. /**
  683. * Try set a single header, multiple header and unset one header param and check if getRequestHeader() does return it
  684. * @param FunctionalTester $I
  685. * @throws Exception
  686. */
  687. public function setSingleHeaderAndMultipleHeadersAndUnsetOneTillTestGetHeader(\FunctionalTester $I)
  688. {
  689. //Init
  690. $this->_curl->reset();
  691. $params = [
  692. 'key' => 'value',
  693. 'secondKey' => 'secondValue'
  694. ];
  695. $I->expectARequestToRemoteServiceWithAResponse(
  696. $expectation = Phiremock::on(
  697. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  698. ->andBody(Is::equalTo(http_build_query($params)))
  699. ->andHeader('Content-Type', Is::equalTo('application/json'))
  700. )->then(
  701. Respond::withStatusCode(200)
  702. )
  703. );
  704. $this->_curl->setPostParams($params)
  705. ->setHeader('Content-Type', 'application/json')
  706. ->setHeaders([
  707. 'custom-type' => '><)#7?aJEvgavJk(*4'
  708. ])
  709. ->unsetHeader('custom-type')
  710. ->post($this->_endPoint . '/test/params/post');
  711. //check for value
  712. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  713. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null);
  714. }
  715. /**
  716. * Try set a single header, multiple header and unset one header param and check if getRequestHeader() does return it
  717. * @param FunctionalTester $I
  718. * @throws Exception
  719. */
  720. public function setMultipleHeadersAndSingleHeaderAndUnsetOneTillTestGetHeader(\FunctionalTester $I)
  721. {
  722. //Init
  723. $this->_curl->reset();
  724. $params = [
  725. 'key' => 'value',
  726. 'secondKey' => 'secondValue'
  727. ];
  728. $I->expectARequestToRemoteServiceWithAResponse(
  729. $expectation = Phiremock::on(
  730. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  731. ->andBody(Is::equalTo(http_build_query($params)))
  732. ->andHeader('Content-Type', Is::equalTo('application/json'))
  733. )->then(
  734. Respond::withStatusCode(200)
  735. )
  736. );
  737. $this->_curl->setPostParams($params)
  738. ->setHeaders([
  739. 'custom-type' => '><)#7?aJEvgavJk(*4'
  740. ])
  741. ->setHeader('Content-Type', 'application/json')
  742. ->unsetHeader('custom-type')
  743. ->post($this->_endPoint . '/test/params/post');
  744. //check for value
  745. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  746. $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null);
  747. }
  748. /**
  749. * Try to post raw json string
  750. * @param FunctionalTester $I
  751. * @throws Exception
  752. */
  753. public function setRawPostDataTest (\FunctionalTester $I)
  754. {
  755. //Init
  756. $this->_curl->reset();
  757. $params = [
  758. 'key' => 'value',
  759. 'secondKey' => 'secondValue'
  760. ];
  761. $I->expectARequestToRemoteServiceWithAResponse(
  762. $expectation = Phiremock::on(
  763. A::postRequest()->andUrl(Is::equalTo('/test/params/post'))
  764. ->andBody(Is::equalTo(json_encode($params)))
  765. ->andHeader('Content-Type', Is::equalTo('application/json'))
  766. )->then(
  767. Respond::withStatusCode(200)
  768. )
  769. );
  770. $this->_curl->setRawPostData(json_encode($params))
  771. ->setHeader('Content-Type', 'application/json')
  772. ->post($this->_endPoint . '/test/params/post');
  773. //check for value
  774. $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json');
  775. }
  776. /**
  777. * Simple HTTP ok
  778. * @param FunctionalTester $I
  779. * @throws Exception
  780. */
  781. public function simpleOptionsOkTest(\FunctionalTester $I)
  782. {
  783. $I->expectARequestToRemoteServiceWithAResponse(
  784. Phiremock::on(
  785. A::optionsRequest()->andUrl(Is::equalTo('/test/httpStatus/204'))
  786. )->then(
  787. Respond::withStatusCode(204)
  788. )
  789. );
  790. $this->_curl->options($this->_endPoint . '/test/httpStatus/204');
  791. $I->assertEquals($this->_curl->responseCode, 204);
  792. }
  793. }