1
0

multiperform_tests.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #include <cstdint>
  2. #include <gtest/gtest.h>
  3. #include <memory>
  4. #include <string>
  5. #include <cpr/api.h>
  6. #include <cpr/cpr.h>
  7. #include <curl/curl.h>
  8. #include "httpServer.hpp"
  9. #include "httpsServer.hpp"
  10. using namespace cpr;
  11. static HttpServer* server = new HttpServer();
  12. bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
  13. return true;
  14. }
  15. TEST(MultiperformAddSessionTests, MultiperformAddSingleSessionTest) {
  16. std::shared_ptr<Session> session = std::make_shared<Session>();
  17. MultiPerform multiperform;
  18. multiperform.AddSession(session);
  19. EXPECT_EQ(2, session.use_count());
  20. }
  21. TEST(MultiperformAddSessionTests, MultiperformAddMultipleSessionsTest) {
  22. MultiPerform multiperform;
  23. for (int i = 0; i < 10; ++i) {
  24. std::shared_ptr<Session> session = std::make_shared<Session>();
  25. multiperform.AddSession(session);
  26. EXPECT_EQ(2, session.use_count());
  27. }
  28. }
  29. TEST(MultiperformAddSessionTests, MultiperformAddMultipleNonDownloadSessionsTest) {
  30. MultiPerform multiperform;
  31. for (int i = 0; i < 10; ++i) {
  32. std::shared_ptr<Session> session = std::make_shared<Session>();
  33. multiperform.AddSession(session, MultiPerform::HttpMethod::GET_REQUEST);
  34. EXPECT_EQ(2, session.use_count());
  35. }
  36. }
  37. TEST(MultiperformAddSessionTests, MultiperformAddMultipleDownloadSessionsTest) {
  38. MultiPerform multiperform;
  39. for (int i = 0; i < 10; ++i) {
  40. std::shared_ptr<Session> session = std::make_shared<Session>();
  41. multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
  42. EXPECT_EQ(2, session.use_count());
  43. }
  44. }
  45. TEST(MultiperformAddSessionTests, MultiperformAddTwoMixedTypeSessionsTest) {
  46. std::shared_ptr<Session> session_1 = std::make_shared<Session>();
  47. std::shared_ptr<Session> session_2 = std::make_shared<Session>();
  48. MultiPerform multiperform;
  49. multiperform.AddSession(session_1, MultiPerform::HttpMethod::GET_REQUEST);
  50. EXPECT_EQ(2, session_1.use_count());
  51. EXPECT_THROW(multiperform.AddSession(session_2, MultiPerform::HttpMethod::DOWNLOAD_REQUEST), std::invalid_argument);
  52. }
  53. TEST(MultiperformAddSessionTests, MultiperformAddTwoMixedTypeSessionsReversTest) {
  54. std::shared_ptr<Session> session_1 = std::make_shared<Session>();
  55. std::shared_ptr<Session> session_2 = std::make_shared<Session>();
  56. MultiPerform multiperform;
  57. multiperform.AddSession(session_1, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
  58. EXPECT_EQ(2, session_1.use_count());
  59. EXPECT_THROW(multiperform.AddSession(session_2, MultiPerform::HttpMethod::GET_REQUEST), std::invalid_argument);
  60. }
  61. TEST(MultiperformRemoveSessionTests, MultiperformRemoveSingleSessionTest) {
  62. std::shared_ptr<Session> session = std::make_shared<Session>();
  63. MultiPerform multiperform;
  64. multiperform.AddSession(session);
  65. EXPECT_EQ(2, session.use_count());
  66. multiperform.RemoveSession(session);
  67. EXPECT_EQ(1, session.use_count());
  68. }
  69. TEST(MultiperformRemoveSessionTests, MultiperformRemoveMultipleSessionsTest) {
  70. MultiPerform multiperform;
  71. for (int i = 0; i < 10; ++i) {
  72. std::shared_ptr<Session> session = std::make_shared<Session>();
  73. multiperform.AddSession(session);
  74. EXPECT_EQ(2, session.use_count());
  75. multiperform.RemoveSession(session);
  76. EXPECT_EQ(1, session.use_count());
  77. }
  78. }
  79. TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) {
  80. std::shared_ptr<Session> session = std::make_shared<Session>();
  81. MultiPerform multiperform;
  82. EXPECT_THROW(multiperform.RemoveSession(session), std::invalid_argument);
  83. }
  84. TEST(MultiperformGetTests, MultiperformSingleSessionGetTest) {
  85. Url url{server->GetBaseUrl() + "/hello.html"};
  86. std::shared_ptr<Session> session = std::make_shared<Session>();
  87. session->SetUrl(url);
  88. MultiPerform multiperform;
  89. multiperform.AddSession(session);
  90. std::vector<Response> responses = multiperform.Get();
  91. EXPECT_EQ(responses.size(), 1);
  92. std::string expected_text{"Hello world!"};
  93. EXPECT_EQ(expected_text, responses.at(0).text);
  94. EXPECT_EQ(url, responses.at(0).url);
  95. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  96. EXPECT_EQ(200, responses.at(0).status_code);
  97. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  98. }
  99. TEST(MultiperformGetTests, MultiperformTwoSessionsGetTest) {
  100. MultiPerform multiperform;
  101. std::vector<Url> urls;
  102. urls.push_back({server->GetBaseUrl() + "/hello.html"});
  103. urls.push_back({server->GetBaseUrl() + "/error.html"});
  104. std::vector<std::shared_ptr<Session>> sessions;
  105. sessions.push_back(std::make_shared<Session>());
  106. sessions.push_back(std::make_shared<Session>());
  107. for (size_t i = 0; i < sessions.size(); ++i) {
  108. sessions.at(i)->SetUrl(urls.at(i));
  109. multiperform.AddSession(sessions.at(i));
  110. }
  111. std::vector<Response> responses = multiperform.Get();
  112. EXPECT_EQ(responses.size(), sessions.size());
  113. std::vector<std::string> expected_texts;
  114. expected_texts.emplace_back("Hello world!");
  115. expected_texts.emplace_back("Not Found");
  116. std::vector<std::string> expected_content_types;
  117. expected_content_types.emplace_back("text/html");
  118. expected_content_types.emplace_back("text/plain");
  119. std::vector<uint16_t> expected_status_codes;
  120. expected_status_codes.push_back(200);
  121. expected_status_codes.push_back(404);
  122. for (size_t i = 0; i < responses.size(); ++i) {
  123. EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
  124. EXPECT_EQ(urls.at(i), responses.at(i).url);
  125. EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
  126. EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
  127. EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
  128. }
  129. }
  130. TEST(MultiperformGetTests, MultiperformRemoveSessionGetTest) {
  131. MultiPerform multiperform;
  132. std::vector<Url> urls;
  133. urls.push_back({server->GetBaseUrl() + "/hello.html"});
  134. urls.push_back({server->GetBaseUrl() + "/hello.html"});
  135. std::vector<std::shared_ptr<Session>> sessions;
  136. sessions.push_back(std::make_shared<Session>());
  137. sessions.push_back(std::make_shared<Session>());
  138. for (size_t i = 0; i < sessions.size(); ++i) {
  139. sessions.at(i)->SetUrl(urls.at(i));
  140. multiperform.AddSession(sessions.at(i));
  141. }
  142. multiperform.RemoveSession(sessions.at(0));
  143. std::vector<Response> responses = multiperform.Get();
  144. EXPECT_EQ(responses.size(), 1);
  145. std::string expected_text{"Hello world!"};
  146. EXPECT_EQ(expected_text, responses.at(0).text);
  147. EXPECT_EQ(urls.at(0), responses.at(0).url);
  148. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  149. EXPECT_EQ(200, responses.at(0).status_code);
  150. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  151. }
  152. #ifndef __APPLE__
  153. /**
  154. * This test case is currently disabled for macOS/Apple systems since it fails in an nondeterministic manner.
  155. * It is probably caused by a bug inside curl_multi_perform on macOS.
  156. * Needs further investigation.
  157. * Issue: https://github.com/libcpr/cpr/issues/841
  158. **/
  159. TEST(MultiperformGetTests, MultiperformTenSessionsGetTest) {
  160. const size_t sessionCount = 10;
  161. MultiPerform multiperform;
  162. Url url{server->GetBaseUrl() + "/hello.html"};
  163. for (size_t i = 0; i < sessionCount; ++i) {
  164. std::shared_ptr<Session> session = std::make_shared<Session>();
  165. session->SetUrl(url);
  166. multiperform.AddSession(session);
  167. }
  168. std::vector<Response> responses = multiperform.Get();
  169. EXPECT_EQ(responses.size(), sessionCount);
  170. for (Response& response : responses) {
  171. EXPECT_EQ(std::string{"Hello world!"}, response.text);
  172. EXPECT_EQ(url, response.url);
  173. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  174. EXPECT_EQ(200, response.status_code);
  175. EXPECT_EQ(ErrorCode::OK, response.error.code);
  176. }
  177. }
  178. #endif
  179. TEST(MultiperformDeleteTests, MultiperformSingleSessionDeleteTest) {
  180. Url url{server->GetBaseUrl() + "/delete.html"};
  181. std::shared_ptr<Session> session = std::make_shared<Session>();
  182. session->SetUrl(url);
  183. MultiPerform multiperform;
  184. multiperform.AddSession(session);
  185. std::vector<Response> responses = multiperform.Delete();
  186. EXPECT_EQ(responses.size(), 1);
  187. std::string expected_text{"Delete success"};
  188. EXPECT_EQ(expected_text, responses.at(0).text);
  189. EXPECT_EQ(url, responses.at(0).url);
  190. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  191. EXPECT_EQ(200, responses.at(0).status_code);
  192. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  193. }
  194. TEST(MultiperformDownloadTests, MultiperformSingleSessionDownloadTest) {
  195. Url url{server->GetBaseUrl() + "/download_gzip.html"};
  196. std::shared_ptr<Session> session = std::make_shared<Session>();
  197. session->SetUrl(url);
  198. session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  199. MultiPerform multiperform;
  200. multiperform.AddSession(session);
  201. std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0});
  202. EXPECT_EQ(responses.size(), 1);
  203. EXPECT_EQ(url, responses.at(0).url);
  204. EXPECT_EQ(200, responses.at(0).status_code);
  205. EXPECT_EQ(cpr::ErrorCode::OK, responses.at(0).error.code);
  206. }
  207. TEST(MultiperformDownloadTests, MultiperformSingleSessionDownloadNonMatchingArgumentsNumberTest) {
  208. std::shared_ptr<Session> session = std::make_shared<Session>();
  209. MultiPerform multiperform;
  210. multiperform.AddSession(session);
  211. EXPECT_THROW(std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0}, WriteCallback{write_data, 0}), std::invalid_argument);
  212. }
  213. TEST(MultiperformDownloadTests, MultiperformTwoSessionsDownloadTest) {
  214. MultiPerform multiperform;
  215. std::vector<Url> urls;
  216. urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
  217. urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
  218. std::vector<std::shared_ptr<Session>> sessions;
  219. sessions.push_back(std::make_shared<Session>());
  220. sessions.push_back(std::make_shared<Session>());
  221. for (size_t i = 0; i < sessions.size(); ++i) {
  222. sessions.at(i)->SetUrl(urls.at(i));
  223. sessions.at(i)->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  224. multiperform.AddSession(sessions.at(i));
  225. }
  226. std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0}, WriteCallback{write_data, 0});
  227. EXPECT_EQ(responses.size(), sessions.size());
  228. for (size_t i = 0; i < responses.size(); ++i) {
  229. EXPECT_EQ(urls.at(i), responses.at(i).url);
  230. EXPECT_EQ(200, responses.at(i).status_code);
  231. EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
  232. }
  233. }
  234. TEST(MultiperformPutTests, MultiperformSingleSessionPutTest) {
  235. Url url{server->GetBaseUrl() + "/put.html"};
  236. std::shared_ptr<Session> session = std::make_shared<Session>();
  237. session->SetUrl(url);
  238. session->SetPayload(Payload{{"x", "5"}});
  239. MultiPerform multiperform;
  240. multiperform.AddSession(session);
  241. std::vector<Response> responses = multiperform.Put();
  242. EXPECT_EQ(responses.size(), 1);
  243. std::string expected_text{
  244. "{\n"
  245. " \"x\": 5\n"
  246. "}"};
  247. EXPECT_EQ(expected_text, responses.at(0).text);
  248. EXPECT_EQ(url, responses.at(0).url);
  249. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  250. EXPECT_EQ(200, responses.at(0).status_code);
  251. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  252. }
  253. TEST(MultiperformHeadTests, MultiperformSingleSessionHeadTest) {
  254. Url url{server->GetBaseUrl() + "/hello.html"};
  255. std::shared_ptr<Session> session = std::make_shared<Session>();
  256. session->SetUrl(url);
  257. MultiPerform multiperform;
  258. multiperform.AddSession(session);
  259. std::vector<Response> responses = multiperform.Head();
  260. EXPECT_EQ(responses.size(), 1);
  261. std::string expected_text;
  262. EXPECT_EQ(expected_text, responses.at(0).text);
  263. EXPECT_EQ(url, responses.at(0).url);
  264. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  265. EXPECT_EQ(200, responses.at(0).status_code);
  266. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  267. }
  268. TEST(MultiperformOptionsTests, MultiperformSingleSessionOptionsTest) {
  269. Url url{server->GetBaseUrl() + "/"};
  270. std::shared_ptr<Session> session = std::make_shared<Session>();
  271. session->SetUrl(url);
  272. MultiPerform multiperform;
  273. multiperform.AddSession(session);
  274. std::vector<Response> responses = multiperform.Options();
  275. EXPECT_EQ(responses.size(), 1);
  276. std::string expected_text;
  277. EXPECT_EQ(expected_text, responses.at(0).text);
  278. EXPECT_EQ(url, responses.at(0).url);
  279. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, responses.at(0).header["Access-Control-Allow-Methods"]);
  280. EXPECT_EQ(200, responses.at(0).status_code);
  281. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  282. }
  283. TEST(MultiperformPatchTests, MultiperformSingleSessionPatchTest) {
  284. Url url{server->GetBaseUrl() + "/patch.html"};
  285. std::shared_ptr<Session> session = std::make_shared<Session>();
  286. session->SetUrl(url);
  287. session->SetPayload(Payload{{"x", "5"}});
  288. MultiPerform multiperform;
  289. multiperform.AddSession(session);
  290. std::vector<Response> responses = multiperform.Patch();
  291. EXPECT_EQ(responses.size(), 1);
  292. std::string expected_text{
  293. "{\n"
  294. " \"x\": 5\n"
  295. "}"};
  296. EXPECT_EQ(expected_text, responses.at(0).text);
  297. EXPECT_EQ(url, responses.at(0).url);
  298. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  299. EXPECT_EQ(200, responses.at(0).status_code);
  300. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  301. }
  302. TEST(MultiperformPostTests, MultiperformSingleSessionPostTest) {
  303. Url url{server->GetBaseUrl() + "/url_post.html"};
  304. std::shared_ptr<Session> session = std::make_shared<Session>();
  305. session->SetUrl(url);
  306. session->SetPayload(Payload{{"x", "5"}});
  307. MultiPerform multiperform;
  308. multiperform.AddSession(session);
  309. std::vector<Response> responses = multiperform.Post();
  310. EXPECT_EQ(responses.size(), 1);
  311. std::string expected_text{
  312. "{\n"
  313. " \"x\": 5\n"
  314. "}"};
  315. EXPECT_EQ(expected_text, responses.at(0).text);
  316. EXPECT_EQ(url, responses.at(0).url);
  317. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  318. EXPECT_EQ(201, responses.at(0).status_code);
  319. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  320. }
  321. TEST(MultiperformPerformTests, MultiperformSingleGetPerformTest) {
  322. Url url{server->GetBaseUrl() + "/hello.html"};
  323. std::shared_ptr<Session> session = std::make_shared<Session>();
  324. session->SetUrl(url);
  325. MultiPerform multiperform;
  326. multiperform.AddSession(session, MultiPerform::HttpMethod::GET_REQUEST);
  327. std::vector<Response> responses = multiperform.Perform();
  328. EXPECT_EQ(responses.size(), 1);
  329. std::string expected_text{"Hello world!"};
  330. EXPECT_EQ(expected_text, responses.at(0).text);
  331. EXPECT_EQ(url, responses.at(0).url);
  332. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  333. EXPECT_EQ(200, responses.at(0).status_code);
  334. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  335. }
  336. TEST(MultiperformPerformTests, MultiperformTwoGetPerformTest) {
  337. MultiPerform multiperform;
  338. std::vector<Url> urls;
  339. urls.push_back({server->GetBaseUrl() + "/hello.html"});
  340. urls.push_back({server->GetBaseUrl() + "/error.html"});
  341. std::vector<std::shared_ptr<Session>> sessions;
  342. sessions.push_back(std::make_shared<Session>());
  343. sessions.push_back(std::make_shared<Session>());
  344. for (size_t i = 0; i < sessions.size(); ++i) {
  345. sessions.at(i)->SetUrl(urls.at(i));
  346. multiperform.AddSession(sessions.at(i), MultiPerform::HttpMethod::GET_REQUEST);
  347. }
  348. std::vector<Response> responses = multiperform.Perform();
  349. EXPECT_EQ(responses.size(), sessions.size());
  350. std::vector<std::string> expected_texts;
  351. expected_texts.emplace_back("Hello world!");
  352. expected_texts.emplace_back("Not Found");
  353. std::vector<std::string> expected_content_types;
  354. expected_content_types.emplace_back("text/html");
  355. expected_content_types.emplace_back("text/plain");
  356. std::vector<uint16_t> expected_status_codes;
  357. expected_status_codes.push_back(200);
  358. expected_status_codes.push_back(404);
  359. for (size_t i = 0; i < responses.size(); ++i) {
  360. EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
  361. EXPECT_EQ(urls.at(i), responses.at(i).url);
  362. EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
  363. EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
  364. EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
  365. }
  366. }
  367. TEST(MultiperformPerformTests, MultiperformMixedMethodsPerformTest) {
  368. MultiPerform multiperform;
  369. std::vector<Url> urls;
  370. urls.push_back({server->GetBaseUrl() + "/hello.html"});
  371. urls.push_back({server->GetBaseUrl() + "/delete.html"});
  372. urls.push_back({server->GetBaseUrl() + "/error.html"});
  373. urls.push_back({server->GetBaseUrl() + "/url_post.html"});
  374. std::vector<std::shared_ptr<Session>> sessions;
  375. sessions.push_back(std::make_shared<Session>());
  376. sessions.push_back(std::make_shared<Session>());
  377. sessions.push_back(std::make_shared<Session>());
  378. sessions.push_back(std::make_shared<Session>());
  379. std::vector<MultiPerform::HttpMethod> methods;
  380. methods.push_back(MultiPerform::HttpMethod::GET_REQUEST);
  381. methods.push_back(MultiPerform::HttpMethod::DELETE_REQUEST);
  382. methods.push_back(MultiPerform::HttpMethod::GET_REQUEST);
  383. methods.push_back(MultiPerform::HttpMethod::POST_REQUEST);
  384. for (size_t i = 0; i < sessions.size(); ++i) {
  385. sessions.at(i)->SetUrl(urls.at(i));
  386. if (methods.at(i) == MultiPerform::HttpMethod::POST_REQUEST) {
  387. sessions.at(i)->SetPayload(Payload{{"x", "5"}});
  388. }
  389. multiperform.AddSession(sessions.at(i), methods.at(i));
  390. }
  391. std::vector<Response> responses = multiperform.Perform();
  392. EXPECT_EQ(responses.size(), sessions.size());
  393. std::vector<std::string> expected_texts;
  394. expected_texts.emplace_back("Hello world!");
  395. expected_texts.emplace_back("Delete success");
  396. expected_texts.emplace_back("Not Found");
  397. expected_texts.emplace_back(
  398. "{\n"
  399. " \"x\": 5\n"
  400. "}");
  401. std::vector<std::string> expected_content_types;
  402. expected_content_types.emplace_back("text/html");
  403. expected_content_types.emplace_back("text/html");
  404. expected_content_types.emplace_back("text/plain");
  405. expected_content_types.emplace_back("application/json");
  406. std::vector<uint16_t> expected_status_codes;
  407. expected_status_codes.push_back(200);
  408. expected_status_codes.push_back(200);
  409. expected_status_codes.push_back(404);
  410. expected_status_codes.push_back(201);
  411. for (size_t i = 0; i < responses.size(); ++i) {
  412. EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
  413. EXPECT_EQ(urls.at(i), responses.at(i).url);
  414. EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
  415. EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
  416. EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
  417. }
  418. }
  419. TEST(MultiperformPerformDownloadTests, MultiperformSinglePerformDownloadTest) {
  420. Url url{server->GetBaseUrl() + "/download_gzip.html"};
  421. std::shared_ptr<Session> session = std::make_shared<Session>();
  422. session->SetUrl(url);
  423. session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  424. MultiPerform multiperform;
  425. multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
  426. std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0});
  427. EXPECT_EQ(responses.size(), 1);
  428. EXPECT_EQ(url, responses.at(0).url);
  429. EXPECT_EQ(200, responses.at(0).status_code);
  430. EXPECT_EQ(cpr::ErrorCode::OK, responses.at(0).error.code);
  431. }
  432. TEST(MultiperformDownloadTests, MultiperformSinglePerformDownloadNonMatchingArgumentsNumberTest) {
  433. std::shared_ptr<Session> session = std::make_shared<Session>();
  434. MultiPerform multiperform;
  435. multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
  436. EXPECT_THROW(std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0}, WriteCallback{write_data, 0}), std::invalid_argument);
  437. }
  438. TEST(MultiperformPerformDownloadTests, MultiperformTwoPerformDownloadTest) {
  439. MultiPerform multiperform;
  440. std::vector<Url> urls;
  441. urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
  442. urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
  443. std::vector<std::shared_ptr<Session>> sessions;
  444. sessions.push_back(std::make_shared<Session>());
  445. sessions.push_back(std::make_shared<Session>());
  446. for (size_t i = 0; i < sessions.size(); ++i) {
  447. sessions.at(i)->SetUrl(urls.at(i));
  448. sessions.at(i)->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  449. multiperform.AddSession(sessions.at(i), MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
  450. }
  451. std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0}, WriteCallback{write_data, 0});
  452. EXPECT_EQ(responses.size(), sessions.size());
  453. for (size_t i = 0; i < responses.size(); ++i) {
  454. EXPECT_EQ(urls.at(i), responses.at(i).url);
  455. EXPECT_EQ(200, responses.at(i).status_code);
  456. EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
  457. }
  458. }
  459. TEST(MultiperformAPITests, MultiperformApiSingleGetTest) {
  460. std::vector<Response> responses = MultiGet(std::tuple<Url>{Url{server->GetBaseUrl() + "/hello.html"}});
  461. EXPECT_EQ(responses.size(), 1);
  462. std::string expected_text{"Hello world!"};
  463. EXPECT_EQ(expected_text, responses.at(0).text);
  464. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, responses.at(0).url);
  465. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  466. EXPECT_EQ(200, responses.at(0).status_code);
  467. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  468. }
  469. TEST(MultiperformAPITests, MultiperformApiTwoGetsTest) {
  470. std::vector<Response> responses = MultiGet(std::tuple<Url, Timeout>{Url{server->GetBaseUrl() + "/long_timeout.html"}, Timeout{1000}}, std::tuple<Url>{Url{server->GetBaseUrl() + "/error.html"}});
  471. EXPECT_EQ(responses.size(), 2);
  472. std::vector<Url> urls;
  473. urls.push_back({server->GetBaseUrl() + "/long_timeout.html"});
  474. urls.push_back({server->GetBaseUrl() + "/error.html"});
  475. std::vector<std::string> expected_texts;
  476. expected_texts.emplace_back("");
  477. expected_texts.emplace_back("Not Found");
  478. std::vector<std::string> expected_content_types;
  479. expected_content_types.emplace_back("");
  480. expected_content_types.emplace_back("text/plain");
  481. std::vector<uint16_t> expected_status_codes;
  482. expected_status_codes.push_back(0);
  483. expected_status_codes.push_back(404);
  484. std::vector<ErrorCode> expected_error_codes;
  485. expected_error_codes.push_back(ErrorCode::OPERATION_TIMEDOUT);
  486. expected_error_codes.push_back(ErrorCode::OK);
  487. for (size_t i = 0; i < responses.size(); ++i) {
  488. EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
  489. EXPECT_EQ(urls.at(i), responses.at(i).url);
  490. EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
  491. EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
  492. EXPECT_EQ(expected_error_codes.at(i), responses.at(i).error.code);
  493. }
  494. }
  495. TEST(MultiperformAPITests, MultiperformApiSingleDeleteTest) {
  496. std::vector<Response> responses = MultiDelete(std::tuple<Url>{Url{server->GetBaseUrl() + "/delete.html"}});
  497. EXPECT_EQ(responses.size(), 1);
  498. std::string expected_text{"Delete success"};
  499. EXPECT_EQ(expected_text, responses.at(0).text);
  500. EXPECT_EQ(Url{server->GetBaseUrl() + "/delete.html"}, responses.at(0).url);
  501. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  502. EXPECT_EQ(200, responses.at(0).status_code);
  503. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  504. }
  505. TEST(MultiperformAPITests, MultiperformApiSinglePutTest) {
  506. std::vector<Response> responses = MultiPut(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/put.html"}, Payload{{"x", "5"}}});
  507. EXPECT_EQ(responses.size(), 1);
  508. std::string expected_text{
  509. "{\n"
  510. " \"x\": 5\n"
  511. "}"};
  512. EXPECT_EQ(expected_text, responses.at(0).text);
  513. EXPECT_EQ(Url{server->GetBaseUrl() + "/put.html"}, responses.at(0).url);
  514. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  515. EXPECT_EQ(200, responses.at(0).status_code);
  516. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  517. }
  518. TEST(MultiperformAPITests, MultiperformApiSingleHeadTest) {
  519. std::vector<Response> responses = MultiHead(std::tuple<Url>{Url{server->GetBaseUrl() + "/hello.html"}});
  520. EXPECT_EQ(responses.size(), 1);
  521. std::string expected_text;
  522. EXPECT_EQ(expected_text, responses.at(0).text);
  523. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, responses.at(0).url);
  524. EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
  525. EXPECT_EQ(200, responses.at(0).status_code);
  526. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  527. }
  528. TEST(MultiperformAPITests, MultiperformApiSingleOptionsTest) {
  529. std::vector<Response> responses = MultiOptions(std::tuple<Url>{Url{server->GetBaseUrl() + "/"}});
  530. EXPECT_EQ(responses.size(), 1);
  531. std::string expected_text;
  532. EXPECT_EQ(expected_text, responses.at(0).text);
  533. EXPECT_EQ(Url{server->GetBaseUrl() + "/"}, responses.at(0).url);
  534. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, responses.at(0).header["Access-Control-Allow-Methods"]);
  535. EXPECT_EQ(200, responses.at(0).status_code);
  536. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  537. }
  538. TEST(MultiperformAPITests, MultiperformApiSinglePatchTest) {
  539. std::vector<Response> responses = MultiPatch(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/patch.html"}, Payload{{"x", "5"}}});
  540. EXPECT_EQ(responses.size(), 1);
  541. std::string expected_text{
  542. "{\n"
  543. " \"x\": 5\n"
  544. "}"};
  545. EXPECT_EQ(expected_text, responses.at(0).text);
  546. EXPECT_EQ(Url{server->GetBaseUrl() + "/patch.html"}, responses.at(0).url);
  547. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  548. EXPECT_EQ(200, responses.at(0).status_code);
  549. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  550. }
  551. TEST(MultiperformAPITests, MultiperformApiSinglePostTest) {
  552. std::vector<Response> responses = MultiPost(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/url_post.html"}, Payload{{"x", "5"}}});
  553. EXPECT_EQ(responses.size(), 1);
  554. std::string expected_text{
  555. "{\n"
  556. " \"x\": 5\n"
  557. "}"};
  558. EXPECT_EQ(expected_text, responses.at(0).text);
  559. EXPECT_EQ(Url{server->GetBaseUrl() + "/url_post.html"}, responses.at(0).url);
  560. EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
  561. EXPECT_EQ(201, responses.at(0).status_code);
  562. EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
  563. }
  564. int main(int argc, char** argv) {
  565. ::testing::InitGoogleTest(&argc, argv);
  566. ::testing::AddGlobalTestEnvironment(server);
  567. return RUN_ALL_TESTS();
  568. }