1
0

callback_tests.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #include <cstddef>
  2. #include <gtest/gtest.h>
  3. #include <chrono>
  4. #include <string>
  5. #include <thread>
  6. #include <vector>
  7. #include <cpr/cpr.h>
  8. #include "cpr/cprtypes.h"
  9. #include "httpServer.hpp"
  10. using namespace cpr;
  11. static HttpServer* server = new HttpServer();
  12. std::chrono::milliseconds sleep_time{50};
  13. std::chrono::seconds zero{0};
  14. int status_callback(int& status_code, Response r) {
  15. status_code = r.status_code;
  16. return r.status_code;
  17. }
  18. int status_callback_ref(int& status_code, const Response& r) {
  19. status_code = r.status_code;
  20. return r.status_code;
  21. }
  22. std::string text_callback(std::string& expected_text, Response r) {
  23. expected_text = r.text;
  24. return r.text;
  25. }
  26. std::string text_callback_ref(std::string& expected_text, const Response& r) {
  27. expected_text = r.text;
  28. return r.text;
  29. }
  30. TEST(CallbackGetTests, CallbackGetLambdaStatusTest) {
  31. Url url{server->GetBaseUrl() + "/hello.html"};
  32. int status_code = 0;
  33. auto future = cpr::GetCallback(
  34. [&status_code](Response r) {
  35. status_code = r.status_code;
  36. return r.status_code;
  37. },
  38. url);
  39. std::this_thread::sleep_for(sleep_time);
  40. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  41. EXPECT_EQ(status_code, future.get());
  42. }
  43. TEST(CallbackGetTests, CallbackGetLambdaTextTest) {
  44. Url url{server->GetBaseUrl() + "/hello.html"};
  45. std::string expected_text{};
  46. auto future = cpr::GetCallback(
  47. [&expected_text](Response r) {
  48. expected_text = r.text;
  49. return r.text;
  50. },
  51. url);
  52. std::this_thread::sleep_for(sleep_time);
  53. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  54. EXPECT_EQ(expected_text, future.get());
  55. }
  56. TEST(CallbackGetTests, CallbackGetLambdaStatusReferenceTest) {
  57. Url url{server->GetBaseUrl() + "/hello.html"};
  58. int status_code = 0;
  59. auto future = cpr::GetCallback(
  60. [&status_code](const Response& r) {
  61. status_code = r.status_code;
  62. return r.status_code;
  63. },
  64. url);
  65. std::this_thread::sleep_for(sleep_time);
  66. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  67. EXPECT_EQ(status_code, future.get());
  68. }
  69. TEST(CallbackGetTests, CallbackGetLambdaTextReferenceTest) {
  70. Url url{server->GetBaseUrl() + "/hello.html"};
  71. std::string expected_text{};
  72. auto future = cpr::GetCallback(
  73. [&expected_text](const Response& r) {
  74. expected_text = r.text;
  75. return r.text;
  76. },
  77. url);
  78. std::this_thread::sleep_for(sleep_time);
  79. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  80. EXPECT_EQ(expected_text, future.get());
  81. }
  82. TEST(CallbackGetTests, CallbackGetFunctionStatusTest) {
  83. Url url{server->GetBaseUrl() + "/hello.html"};
  84. int status_code = 0;
  85. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  86. auto future = cpr::GetCallback(callback, url);
  87. std::this_thread::sleep_for(sleep_time);
  88. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  89. EXPECT_EQ(status_code, future.get());
  90. }
  91. TEST(CallbackGetTests, CallbackGetFunctionTextTest) {
  92. Url url{server->GetBaseUrl() + "/hello.html"};
  93. std::string expected_text{};
  94. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  95. auto future = cpr::GetCallback(callback, url);
  96. std::this_thread::sleep_for(sleep_time);
  97. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  98. EXPECT_EQ(expected_text, future.get());
  99. }
  100. TEST(CallbackGetTests, CallbackGetFunctionStatusReferenceTest) {
  101. Url url{server->GetBaseUrl() + "/hello.html"};
  102. int status_code = 0;
  103. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  104. auto future = cpr::GetCallback(callback, url);
  105. std::this_thread::sleep_for(sleep_time);
  106. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  107. EXPECT_EQ(status_code, future.get());
  108. }
  109. TEST(CallbackGetTests, CallbackGetFunctionTextReferenceTest) {
  110. Url url{server->GetBaseUrl() + "/hello.html"};
  111. std::string expected_text{};
  112. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  113. auto future = cpr::GetCallback(callback, url);
  114. std::this_thread::sleep_for(sleep_time);
  115. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  116. EXPECT_EQ(expected_text, future.get());
  117. }
  118. TEST(CallbackDeleteTests, CallbackDeleteLambdaStatusTest) {
  119. Url url{server->GetBaseUrl() + "/delete.html"};
  120. int status_code = 0;
  121. auto future = cpr::DeleteCallback(
  122. [&status_code](Response r) {
  123. status_code = r.status_code;
  124. return r.status_code;
  125. },
  126. url);
  127. std::this_thread::sleep_for(sleep_time);
  128. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  129. EXPECT_EQ(status_code, future.get());
  130. }
  131. TEST(CallbackDeleteTests, CallbackDeleteLambdaTextTest) {
  132. Url url{server->GetBaseUrl() + "/delete.html"};
  133. std::string expected_text{};
  134. auto future = cpr::DeleteCallback(
  135. [&expected_text](Response r) {
  136. expected_text = r.text;
  137. return r.text;
  138. },
  139. url);
  140. std::this_thread::sleep_for(sleep_time);
  141. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  142. EXPECT_EQ(expected_text, future.get());
  143. }
  144. TEST(CallbackDeleteTests, CallbackDeleteLambdaStatusReferenceTest) {
  145. Url url{server->GetBaseUrl() + "/delete.html"};
  146. int status_code = 0;
  147. auto future = cpr::DeleteCallback(
  148. [&status_code](const Response& r) {
  149. status_code = r.status_code;
  150. return r.status_code;
  151. },
  152. url);
  153. std::this_thread::sleep_for(sleep_time);
  154. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  155. EXPECT_EQ(status_code, future.get());
  156. }
  157. TEST(CallbackDeleteTests, CallbackDeleteLambdaTextReferenceTest) {
  158. Url url{server->GetBaseUrl() + "/delete.html"};
  159. std::string expected_text{};
  160. auto future = cpr::DeleteCallback(
  161. [&expected_text](const Response& r) {
  162. expected_text = r.text;
  163. return r.text;
  164. },
  165. url);
  166. std::this_thread::sleep_for(sleep_time);
  167. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  168. EXPECT_EQ(expected_text, future.get());
  169. }
  170. TEST(CallbackDeleteTests, CallbackDeleteFunctionStatusTest) {
  171. Url url{server->GetBaseUrl() + "/delete.html"};
  172. int status_code = 0;
  173. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  174. auto future = cpr::DeleteCallback(callback, url);
  175. std::this_thread::sleep_for(sleep_time);
  176. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  177. EXPECT_EQ(status_code, future.get());
  178. }
  179. TEST(CallbackDeleteTests, CallbackDeleteFunctionTextTest) {
  180. Url url{server->GetBaseUrl() + "/delete.html"};
  181. std::string expected_text{};
  182. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  183. auto future = cpr::DeleteCallback(callback, url);
  184. std::this_thread::sleep_for(sleep_time);
  185. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  186. EXPECT_EQ(expected_text, future.get());
  187. }
  188. TEST(CallbackDeleteTests, CallbackDeleteFunctionStatusReferenceTest) {
  189. Url url{server->GetBaseUrl() + "/delete.html"};
  190. int status_code = 0;
  191. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  192. auto future = cpr::DeleteCallback(callback, url);
  193. std::this_thread::sleep_for(sleep_time);
  194. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  195. EXPECT_EQ(status_code, future.get());
  196. }
  197. TEST(CallbackDeleteTests, CallbackDeleteFunctionTextReferenceTest) {
  198. Url url{server->GetBaseUrl() + "/delete.html"};
  199. std::string expected_text{};
  200. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  201. auto future = cpr::DeleteCallback(callback, url);
  202. std::this_thread::sleep_for(sleep_time);
  203. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  204. EXPECT_EQ(expected_text, future.get());
  205. }
  206. TEST(CallbackHeadTests, CallbackHeadLambdaStatusTest) {
  207. Url url{server->GetBaseUrl() + "/hello.html"};
  208. int status_code = 0;
  209. auto future = cpr::HeadCallback(
  210. [&status_code](Response r) {
  211. status_code = r.status_code;
  212. return r.status_code;
  213. },
  214. url);
  215. std::this_thread::sleep_for(sleep_time);
  216. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  217. EXPECT_EQ(status_code, future.get());
  218. }
  219. TEST(CallbackHeadTests, CallbackHeadLambdaTextTest) {
  220. Url url{server->GetBaseUrl() + "/hello.html"};
  221. std::string expected_text{};
  222. auto future = cpr::HeadCallback(
  223. [&expected_text](Response r) {
  224. expected_text = r.text;
  225. return r.text;
  226. },
  227. url);
  228. std::this_thread::sleep_for(sleep_time);
  229. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  230. EXPECT_EQ(expected_text, future.get());
  231. }
  232. TEST(CallbackHeadTests, CallbackHeadLambdaStatusReferenceTest) {
  233. Url url{server->GetBaseUrl() + "/hello.html"};
  234. int status_code = 0;
  235. auto future = cpr::HeadCallback(
  236. [&status_code](const Response& r) {
  237. status_code = r.status_code;
  238. return r.status_code;
  239. },
  240. url);
  241. std::this_thread::sleep_for(sleep_time);
  242. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  243. EXPECT_EQ(status_code, future.get());
  244. }
  245. TEST(CallbackHeadTests, CallbackHeadLambdaTextReferenceTest) {
  246. Url url{server->GetBaseUrl() + "/hello.html"};
  247. std::string expected_text{};
  248. auto future = cpr::HeadCallback(
  249. [&expected_text](const Response& r) {
  250. expected_text = r.text;
  251. return r.text;
  252. },
  253. url);
  254. std::this_thread::sleep_for(sleep_time);
  255. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  256. EXPECT_EQ(expected_text, future.get());
  257. }
  258. TEST(CallbackHeadTests, CallbackHeadFunctionStatusTest) {
  259. Url url{server->GetBaseUrl() + "/hello.html"};
  260. int status_code = 0;
  261. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  262. auto future = cpr::HeadCallback(callback, url);
  263. std::this_thread::sleep_for(sleep_time);
  264. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  265. EXPECT_EQ(status_code, future.get());
  266. }
  267. TEST(CallbackHeadTests, CallbackHeadFunctionTextTest) {
  268. Url url{server->GetBaseUrl() + "/hello.html"};
  269. std::string expected_text{};
  270. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  271. auto future = cpr::HeadCallback(callback, url);
  272. std::this_thread::sleep_for(sleep_time);
  273. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  274. EXPECT_EQ(expected_text, future.get());
  275. }
  276. TEST(CallbackHeadTests, CallbackHeadFunctionStatusReferenceTest) {
  277. Url url{server->GetBaseUrl() + "/hello.html"};
  278. int status_code = 0;
  279. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  280. auto future = cpr::HeadCallback(callback, url);
  281. std::this_thread::sleep_for(sleep_time);
  282. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  283. EXPECT_EQ(status_code, future.get());
  284. }
  285. TEST(CallbackHeadTests, CallbackHeadFunctionTextReferenceTest) {
  286. Url url{server->GetBaseUrl() + "/hello.html"};
  287. std::string expected_text{};
  288. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  289. auto future = cpr::HeadCallback(callback, url);
  290. std::this_thread::sleep_for(sleep_time);
  291. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  292. EXPECT_EQ(expected_text, future.get());
  293. }
  294. TEST(CallbackPostTests, CallbackPostLambdaStatusTest) {
  295. Url url{server->GetBaseUrl() + "/url_post.html"};
  296. Payload payload{{"x", "5"}};
  297. int status_code = 0;
  298. auto future = cpr::PostCallback(
  299. [&status_code](Response r) {
  300. status_code = r.status_code;
  301. return r.status_code;
  302. },
  303. url, payload);
  304. std::this_thread::sleep_for(sleep_time);
  305. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  306. EXPECT_EQ(status_code, future.get());
  307. }
  308. TEST(CallbackPostTests, CallbackPostLambdaTextTest) {
  309. Url url{server->GetBaseUrl() + "/url_post.html"};
  310. Payload payload{{"x", "5"}};
  311. std::string expected_text{};
  312. auto future = cpr::PostCallback(
  313. [&expected_text](Response r) {
  314. expected_text = r.text;
  315. return r.text;
  316. },
  317. url, payload);
  318. std::this_thread::sleep_for(sleep_time);
  319. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  320. EXPECT_EQ(expected_text, future.get());
  321. }
  322. TEST(CallbackPostTests, CallbackPostLambdaStatusReferenceTest) {
  323. Url url{server->GetBaseUrl() + "/url_post.html"};
  324. Payload payload{{"x", "5"}};
  325. int status_code = 0;
  326. auto future = cpr::PostCallback(
  327. [&status_code](const Response& r) {
  328. status_code = r.status_code;
  329. return r.status_code;
  330. },
  331. url, payload);
  332. std::this_thread::sleep_for(sleep_time);
  333. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  334. EXPECT_EQ(status_code, future.get());
  335. }
  336. TEST(CallbackPostTests, CallbackPostLambdaTextReferenceTest) {
  337. Url url{server->GetBaseUrl() + "/url_post.html"};
  338. Payload payload{{"x", "5"}};
  339. std::string expected_text{};
  340. auto future = cpr::PostCallback(
  341. [&expected_text](const Response& r) {
  342. expected_text = r.text;
  343. return r.text;
  344. },
  345. url, payload);
  346. std::this_thread::sleep_for(sleep_time);
  347. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  348. EXPECT_EQ(expected_text, future.get());
  349. }
  350. TEST(CallbackPostTests, CallbackPostFunctionStatusTest) {
  351. Url url{server->GetBaseUrl() + "/url_post.html"};
  352. Payload payload{{"x", "5"}};
  353. int status_code = 0;
  354. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  355. auto future = cpr::PostCallback(callback, url, payload);
  356. std::this_thread::sleep_for(sleep_time);
  357. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  358. EXPECT_EQ(status_code, future.get());
  359. }
  360. TEST(CallbackPostTests, CallbackPostFunctionTextTest) {
  361. Url url{server->GetBaseUrl() + "/url_post.html"};
  362. Payload payload{{"x", "5"}};
  363. std::string expected_text{};
  364. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  365. auto future = cpr::PostCallback(callback, url, payload);
  366. std::this_thread::sleep_for(sleep_time);
  367. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  368. EXPECT_EQ(expected_text, future.get());
  369. }
  370. TEST(CallbackPostTests, CallbackPostFunctionStatusReferenceTest) {
  371. Url url{server->GetBaseUrl() + "/url_post.html"};
  372. Payload payload{{"x", "5"}};
  373. int status_code = 0;
  374. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  375. auto future = cpr::PostCallback(callback, url, payload);
  376. std::this_thread::sleep_for(sleep_time);
  377. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  378. EXPECT_EQ(status_code, future.get());
  379. }
  380. TEST(CallbackPostTests, CallbackPostFunctionTextReferenceTest) {
  381. Url url{server->GetBaseUrl() + "/url_post.html"};
  382. Payload payload{{"x", "5"}};
  383. std::string expected_text{};
  384. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  385. auto future = cpr::PostCallback(callback, url, payload);
  386. std::this_thread::sleep_for(sleep_time);
  387. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  388. EXPECT_EQ(expected_text, future.get());
  389. }
  390. TEST(CallbackPutTests, CallbackPutLambdaStatusTest) {
  391. Url url{server->GetBaseUrl() + "/url_post.html"};
  392. Payload payload{{"x", "5"}};
  393. int status_code = 0;
  394. auto future = cpr::PutCallback(
  395. [&status_code](Response r) {
  396. status_code = r.status_code;
  397. return r.status_code;
  398. },
  399. url, payload);
  400. std::this_thread::sleep_for(sleep_time);
  401. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  402. EXPECT_EQ(status_code, future.get());
  403. }
  404. TEST(CallbackPutTests, CallbackPutLambdaTextTest) {
  405. Url url{server->GetBaseUrl() + "/url_post.html"};
  406. Payload payload{{"x", "5"}};
  407. std::string expected_text{};
  408. auto future = cpr::PutCallback(
  409. [&expected_text](Response r) {
  410. expected_text = r.text;
  411. return r.text;
  412. },
  413. url, payload);
  414. std::this_thread::sleep_for(sleep_time);
  415. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  416. EXPECT_EQ(expected_text, future.get());
  417. }
  418. TEST(CallbackPutTests, CallbackPutLambdaStatusReferenceTest) {
  419. Url url{server->GetBaseUrl() + "/url_post.html"};
  420. Payload payload{{"x", "5"}};
  421. int status_code = 0;
  422. auto future = cpr::PutCallback(
  423. [&status_code](const Response& r) {
  424. status_code = r.status_code;
  425. return r.status_code;
  426. },
  427. url, payload);
  428. std::this_thread::sleep_for(sleep_time);
  429. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  430. EXPECT_EQ(status_code, future.get());
  431. }
  432. TEST(CallbackPutTests, CallbackPutLambdaTextReferenceTest) {
  433. Url url{server->GetBaseUrl() + "/url_post.html"};
  434. Payload payload{{"x", "5"}};
  435. std::string expected_text{};
  436. auto future = cpr::PutCallback(
  437. [&expected_text](const Response& r) {
  438. expected_text = r.text;
  439. return r.text;
  440. },
  441. url, payload);
  442. std::this_thread::sleep_for(sleep_time);
  443. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  444. EXPECT_EQ(expected_text, future.get());
  445. }
  446. TEST(CallbackPutTests, CallbackPutFunctionStatusTest) {
  447. Url url{server->GetBaseUrl() + "/url_post.html"};
  448. Payload payload{{"x", "5"}};
  449. int status_code = 0;
  450. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  451. auto future = cpr::PutCallback(callback, url, payload);
  452. std::this_thread::sleep_for(sleep_time);
  453. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  454. EXPECT_EQ(status_code, future.get());
  455. }
  456. TEST(CallbackPutTests, CallbackPutFunctionTextTest) {
  457. Url url{server->GetBaseUrl() + "/url_post.html"};
  458. Payload payload{{"x", "5"}};
  459. std::string expected_text{};
  460. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  461. auto future = cpr::PutCallback(callback, url, payload);
  462. std::this_thread::sleep_for(sleep_time);
  463. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  464. EXPECT_EQ(expected_text, future.get());
  465. }
  466. TEST(CallbackPutTests, CallbackPutFunctionStatusReferenceTest) {
  467. Url url{server->GetBaseUrl() + "/url_post.html"};
  468. Payload payload{{"x", "5"}};
  469. int status_code = 0;
  470. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  471. auto future = cpr::PutCallback(callback, url, payload);
  472. std::this_thread::sleep_for(sleep_time);
  473. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  474. EXPECT_EQ(status_code, future.get());
  475. }
  476. TEST(CallbackPutTests, CallbackPutFunctionTextReferenceTest) {
  477. Url url{server->GetBaseUrl() + "/url_post.html"};
  478. Payload payload{{"x", "5"}};
  479. std::string expected_text{};
  480. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  481. auto future = cpr::PutCallback(callback, url, payload);
  482. std::this_thread::sleep_for(sleep_time);
  483. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  484. EXPECT_EQ(expected_text, future.get());
  485. }
  486. TEST(CallbackOptionsTests, CallbackOptionsLambdaStatusTest) {
  487. Url url{server->GetBaseUrl() + "/hello.html"};
  488. int status_code = 0;
  489. auto future = cpr::OptionsCallback(
  490. [&status_code](Response r) {
  491. status_code = r.status_code;
  492. return r.status_code;
  493. },
  494. url);
  495. std::this_thread::sleep_for(sleep_time);
  496. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  497. EXPECT_EQ(status_code, future.get());
  498. }
  499. TEST(CallbackOptionsTests, CallbackOptionsLambdaTextTest) {
  500. Url url{server->GetBaseUrl() + "/hello.html"};
  501. std::string expected_text{};
  502. auto future = cpr::OptionsCallback(
  503. [&expected_text](Response r) {
  504. expected_text = r.text;
  505. return r.text;
  506. },
  507. url);
  508. std::this_thread::sleep_for(sleep_time);
  509. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  510. EXPECT_EQ(expected_text, future.get());
  511. }
  512. TEST(CallbackOptionsTests, CallbackOptionsLambdaStatusReferenceTest) {
  513. Url url{server->GetBaseUrl() + "/hello.html"};
  514. int status_code = 0;
  515. auto future = cpr::OptionsCallback(
  516. [&status_code](const Response& r) {
  517. status_code = r.status_code;
  518. return r.status_code;
  519. },
  520. url);
  521. std::this_thread::sleep_for(sleep_time);
  522. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  523. EXPECT_EQ(status_code, future.get());
  524. }
  525. TEST(CallbackOptionsTests, CallbackOptionsLambdaTextReferenceTest) {
  526. Url url{server->GetBaseUrl() + "/hello.html"};
  527. std::string expected_text{};
  528. auto future = cpr::OptionsCallback(
  529. [&expected_text](const Response& r) {
  530. expected_text = r.text;
  531. return r.text;
  532. },
  533. url);
  534. std::this_thread::sleep_for(sleep_time);
  535. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  536. EXPECT_EQ(expected_text, future.get());
  537. }
  538. TEST(CallbackOptionsTests, CallbackOptionsFunctionStatusTest) {
  539. Url url{server->GetBaseUrl() + "/hello.html"};
  540. int status_code = 0;
  541. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  542. auto future = cpr::OptionsCallback(callback, url);
  543. std::this_thread::sleep_for(sleep_time);
  544. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  545. EXPECT_EQ(status_code, future.get());
  546. }
  547. TEST(CallbackOptionsTests, CallbackOptionsFunctionTextTest) {
  548. Url url{server->GetBaseUrl() + "/hello.html"};
  549. std::string expected_text{};
  550. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  551. auto future = cpr::OptionsCallback(callback, url);
  552. std::this_thread::sleep_for(sleep_time);
  553. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  554. EXPECT_EQ(expected_text, future.get());
  555. }
  556. TEST(CallbackOptionsTests, CallbackOptionsFunctionStatusReferenceTest) {
  557. Url url{server->GetBaseUrl() + "/hello.html"};
  558. int status_code = 0;
  559. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  560. auto future = cpr::OptionsCallback(callback, url);
  561. std::this_thread::sleep_for(sleep_time);
  562. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  563. EXPECT_EQ(status_code, future.get());
  564. }
  565. TEST(CallbackOptionsTests, CallbackOptionsFunctionTextReferenceTest) {
  566. Url url{server->GetBaseUrl() + "/hello.html"};
  567. std::string expected_text{};
  568. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  569. auto future = cpr::OptionsCallback(callback, url);
  570. std::this_thread::sleep_for(sleep_time);
  571. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  572. EXPECT_EQ(expected_text, future.get());
  573. }
  574. TEST(CallbackPatchTests, CallbackPatchLambdaStatusTest) {
  575. Url url{server->GetBaseUrl() + "/url_post.html"};
  576. Payload payload{{"x", "5"}};
  577. int status_code = 0;
  578. auto future = cpr::PatchCallback(
  579. [&status_code](Response r) {
  580. status_code = r.status_code;
  581. return r.status_code;
  582. },
  583. url, payload);
  584. std::this_thread::sleep_for(sleep_time);
  585. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  586. EXPECT_EQ(status_code, future.get());
  587. }
  588. TEST(CallbackPatchTests, CallbackPatchLambdaTextTest) {
  589. Url url{server->GetBaseUrl() + "/url_post.html"};
  590. Payload payload{{"x", "5"}};
  591. std::string expected_text{};
  592. auto future = cpr::PatchCallback(
  593. [&expected_text](Response r) {
  594. expected_text = r.text;
  595. return r.text;
  596. },
  597. url, payload);
  598. std::this_thread::sleep_for(sleep_time);
  599. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  600. EXPECT_EQ(expected_text, future.get());
  601. }
  602. TEST(CallbackPatchTests, CallbackPatchLambdaStatusReferenceTest) {
  603. Url url{server->GetBaseUrl() + "/url_post.html"};
  604. Payload payload{{"x", "5"}};
  605. int status_code = 0;
  606. auto future = cpr::PatchCallback(
  607. [&status_code](const Response& r) {
  608. status_code = r.status_code;
  609. return r.status_code;
  610. },
  611. url, payload);
  612. std::this_thread::sleep_for(sleep_time);
  613. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  614. EXPECT_EQ(status_code, future.get());
  615. }
  616. TEST(CallbackPatchTests, CallbackPatchLambdaTextReferenceTest) {
  617. Url url{server->GetBaseUrl() + "/url_post.html"};
  618. Payload payload{{"x", "5"}};
  619. std::string expected_text{};
  620. auto future = cpr::PatchCallback(
  621. [&expected_text](const Response& r) {
  622. expected_text = r.text;
  623. return r.text;
  624. },
  625. url, payload);
  626. std::this_thread::sleep_for(sleep_time);
  627. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  628. EXPECT_EQ(expected_text, future.get());
  629. }
  630. TEST(CallbackPatchTests, CallbackPatchFunctionStatusTest) {
  631. Url url{server->GetBaseUrl() + "/url_post.html"};
  632. Payload payload{{"x", "5"}};
  633. int status_code = 0;
  634. auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
  635. auto future = cpr::PatchCallback(callback, url, payload);
  636. std::this_thread::sleep_for(sleep_time);
  637. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  638. EXPECT_EQ(status_code, future.get());
  639. }
  640. TEST(CallbackPatchTests, CallbackPatchFunctionTextTest) {
  641. Url url{server->GetBaseUrl() + "/url_post.html"};
  642. Payload payload{{"x", "5"}};
  643. std::string expected_text{};
  644. auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
  645. auto future = cpr::PatchCallback(callback, url, payload);
  646. std::this_thread::sleep_for(sleep_time);
  647. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  648. EXPECT_EQ(expected_text, future.get());
  649. }
  650. TEST(CallbackPatchTests, CallbackPatchFunctionStatusReferenceTest) {
  651. Url url{server->GetBaseUrl() + "/url_post.html"};
  652. Payload payload{{"x", "5"}};
  653. int status_code = 0;
  654. auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
  655. auto future = cpr::PatchCallback(callback, url, payload);
  656. std::this_thread::sleep_for(sleep_time);
  657. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  658. EXPECT_EQ(status_code, future.get());
  659. }
  660. TEST(CallbackPatchTests, CallbackPatchFunctionTextReferenceTest) {
  661. Url url{server->GetBaseUrl() + "/url_post.html"};
  662. Payload payload{{"x", "5"}};
  663. std::string expected_text{};
  664. auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
  665. auto future = cpr::PatchCallback(callback, url, payload);
  666. std::this_thread::sleep_for(sleep_time);
  667. EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
  668. EXPECT_EQ(expected_text, future.get());
  669. }
  670. TEST(CallbackDataTests, CallbackReadFunctionCancelTest) {
  671. Url url{server->GetBaseUrl() + "/url_post.html"};
  672. Response response = cpr::Post(url, cpr::ReadCallback([](char* /*buffer*/, size_t& /*size*/, intptr_t /*userdata*/) -> size_t { return false; }));
  673. EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
  674. }
  675. TEST(CallbackDataTests, CallbackReadFunctionTextTest) {
  676. Url url{server->GetBaseUrl() + "/url_post.html"};
  677. std::string expected_text{
  678. "{\n"
  679. " \"x\": 5\n"
  680. "}"};
  681. unsigned count = 0;
  682. Response response = cpr::Post(url, cpr::ReadCallback{3, [&](char* buffer, size_t& size, intptr_t /*userdata*/) -> size_t {
  683. std::string data;
  684. ++count;
  685. switch (count) {
  686. case 1:
  687. data = "x=";
  688. break;
  689. case 2:
  690. data = "5";
  691. break;
  692. default:
  693. return false;
  694. }
  695. std::copy(data.begin(), data.end(), buffer);
  696. size = data.size();
  697. return true;
  698. }});
  699. EXPECT_EQ(2, count);
  700. EXPECT_EQ(expected_text, response.text);
  701. }
  702. TEST(CallbackDataTests, CallbackReadFunctionTextTestPut) {
  703. Url url{server->GetBaseUrl() + "/put.html"};
  704. std::string expected_text{
  705. "{\n"
  706. " \"x\": 5\n"
  707. "}"};
  708. unsigned count = 0;
  709. Response response = cpr::Put(url, cpr::ReadCallback{3, [&](char* buffer, size_t& size, intptr_t /*userdata*/) -> size_t {
  710. std::string data;
  711. ++count;
  712. switch (count) {
  713. case 1:
  714. data = "x=";
  715. break;
  716. case 2:
  717. data = "5";
  718. break;
  719. default:
  720. return false;
  721. }
  722. std::copy(data.begin(), data.end(), buffer);
  723. size = data.size();
  724. return true;
  725. }});
  726. EXPECT_EQ(2, count);
  727. EXPECT_EQ(expected_text, response.text);
  728. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  729. EXPECT_EQ(200, response.status_code);
  730. EXPECT_EQ(ErrorCode::OK, response.error.code);
  731. }
  732. /**
  733. * Checks if the "Transfer-Encoding" header will be kept when using headers and a read callback.
  734. * Issue: https://github.com/whoshuu/cpr/issues/517
  735. **/
  736. TEST(CallbackDataTests, CallbackReadFunctionHeaderTest) {
  737. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  738. std::string data = "Test";
  739. Response response = cpr::Post(url,
  740. cpr::ReadCallback{-1,
  741. [&](char* /*buffer*/, size_t& size, intptr_t /*userdata*/) -> size_t {
  742. size = 0;
  743. return true;
  744. }},
  745. Header{{"TestHeader", "42"}});
  746. EXPECT_EQ(url, response.url);
  747. EXPECT_EQ(200, response.status_code);
  748. // Check Header:
  749. EXPECT_EQ(std::string{"42"}, response.header["TestHeader"]); // Set by us
  750. EXPECT_TRUE(response.header.find("TestHeader") != response.header.end());
  751. EXPECT_EQ(std::string{"chunked"}, response.header["Transfer-Encoding"]); // Set by the read callback
  752. EXPECT_TRUE(response.header.find("Transfer-Encoding") != response.header.end());
  753. }
  754. /* cesanta mongoose doesn't support chunked requests yet
  755. TEST(CallbackDataTests, CallbackReadFunctionChunkedTest) {
  756. Url url{server->GetBaseUrl() + "/url_post.html"};
  757. std::string expected_text{
  758. "{\n"
  759. " \"x\": 5\n"
  760. "}"};
  761. unsigned count = 0;
  762. Response response = cpr::Post(url, cpr::ReadCallback{[&count](char* buffer, size_t & size) -> size_t {
  763. std::string data;
  764. ++ count;
  765. switch (count) {
  766. case 1:
  767. data = "x=";
  768. break;
  769. case 2:
  770. data = "5";
  771. break;
  772. default:
  773. data = "";
  774. break;
  775. }
  776. std::copy(data.begin(), data.end(), buffer);
  777. size = data.size();
  778. return true;
  779. }});
  780. EXPECT_EQ(3, count);
  781. EXPECT_EQ(expected_text, response.text);
  782. }
  783. */
  784. TEST(CallbackDataTests, CallbackHeaderFunctionCancelTest) {
  785. Url url{server->GetBaseUrl() + "/url_post.html"};
  786. Response response = Post(url, HeaderCallback{[](std::string /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
  787. EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
  788. }
  789. TEST(CallbackDataTests, CallbackHeaderFunctionTextTest) {
  790. Url url{server->GetBaseUrl() + "/url_post.html"};
  791. std::vector<std::string> expected_headers{"HTTP/1.1 201 Created\r\n", "Content-Type: application/json\r\n", "\r\n"};
  792. std::set<std::string> response_headers;
  793. Post(url, HeaderCallback{[&response_headers](std::string header, intptr_t /*userdata*/) -> bool {
  794. response_headers.insert(header);
  795. return true;
  796. }});
  797. for (std::string& header : expected_headers) {
  798. std::cout << header << std::endl;
  799. EXPECT_TRUE(response_headers.count(header));
  800. }
  801. }
  802. TEST(CallbackDataTests, CallbackWriteFunctionCancelTest) {
  803. Url url{server->GetBaseUrl() + "/url_post.html"};
  804. Response response = Post(url, WriteCallback{[](std::string /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
  805. EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
  806. }
  807. TEST(CallbackDataTests, CallbackWriteFunctionTextTest) {
  808. Url url{server->GetBaseUrl() + "/url_post.html"};
  809. std::string expected_text{
  810. "{\n"
  811. " \"x\": 5\n"
  812. "}"};
  813. std::string response_text;
  814. Post(url, Payload{{"x", "5"}}, WriteCallback{[&response_text](std::string header, intptr_t /*userdata*/) -> bool {
  815. response_text.append(header);
  816. return true;
  817. }});
  818. EXPECT_EQ(expected_text, response_text);
  819. }
  820. TEST(CallbackDataTests, CallbackProgressFunctionCancelTest) {
  821. Url url{server->GetBaseUrl() + "/url_post.html"};
  822. Response response = Post(url, ProgressCallback{[](size_t /*downloadTotal*/, size_t /*downloadNow*/, size_t /*uploadTotal*/, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool { return false; }});
  823. EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
  824. }
  825. TEST(CallbackDataTests, CallbackProgressFunctionTotalTest) {
  826. Url url{server->GetBaseUrl() + "/url_post.html"};
  827. Body body{"x=5"};
  828. size_t response_upload = 0, response_download = 0;
  829. Response response = Post(url, body, ProgressCallback{[&](size_t downloadTotal, size_t /*downloadNow*/, size_t uploadTotal, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool {
  830. response_upload = uploadTotal;
  831. response_download = downloadTotal;
  832. return true;
  833. }});
  834. EXPECT_EQ(body.str().length(), response_upload);
  835. EXPECT_EQ(response.text.length(), response_download);
  836. }
  837. TEST(CallbackDataTests, CallbackDebugFunctionTextTest) {
  838. Url url{server->GetBaseUrl() + "/url_post.html"};
  839. Body body{"x=5"};
  840. std::string debug_body;
  841. Response response = Post(url, body, DebugCallback{[&](DebugCallback::InfoType type, std::string data, intptr_t /*userdata*/) {
  842. if (type == DebugCallback::InfoType::DATA_OUT) {
  843. debug_body = data;
  844. }
  845. }});
  846. EXPECT_EQ(body.str(), debug_body);
  847. }
  848. int main(int argc, char** argv) {
  849. ::testing::InitGoogleTest(&argc, argv);
  850. ::testing::AddGlobalTestEnvironment(server);
  851. return RUN_ALL_TESTS();
  852. }