session_tests.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. #include <cstdint>
  2. #include <cstdlib>
  3. #include <gtest/gtest.h>
  4. #include <chrono>
  5. #include <string>
  6. #include <cpr/cpr.h>
  7. #include <curl/curl.h>
  8. #include "httpServer.hpp"
  9. using namespace cpr;
  10. using namespace std::chrono_literals;
  11. static HttpServer* server = new HttpServer();
  12. std::chrono::milliseconds sleep_time{50};
  13. std::chrono::seconds zero{0};
  14. bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
  15. return true;
  16. }
  17. TEST(RedirectTests, TemporaryDefaultRedirectTest) {
  18. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  19. Session session;
  20. session.SetUrl(url);
  21. Response response = session.Get();
  22. std::string expected_text{"Hello world!"};
  23. EXPECT_EQ(expected_text, response.text);
  24. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
  25. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  26. EXPECT_EQ(200, response.status_code);
  27. EXPECT_EQ(ErrorCode::OK, response.error.code);
  28. }
  29. TEST(RedirectTests, NoTemporaryRedirectTest) {
  30. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  31. Session session;
  32. session.SetUrl(url);
  33. session.SetRedirect(Redirect(false));
  34. Response response = session.Get();
  35. std::string expected_text{"Moved Temporarily"};
  36. EXPECT_EQ(expected_text, response.text);
  37. EXPECT_EQ(url, response.url);
  38. EXPECT_EQ(std::string{}, response.header["content-type"]);
  39. EXPECT_EQ(302, response.status_code);
  40. EXPECT_EQ(ErrorCode::OK, response.error.code);
  41. }
  42. TEST(RedirectTests, PermanentDefaultRedirectTest) {
  43. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  44. Session session;
  45. session.SetUrl(url);
  46. Response response = session.Get();
  47. std::string expected_text{"Hello world!"};
  48. EXPECT_EQ(expected_text, response.text);
  49. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
  50. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  51. EXPECT_EQ(200, response.status_code);
  52. EXPECT_EQ(ErrorCode::OK, response.error.code);
  53. }
  54. TEST(RedirectTests, NoPermanentRedirectTest) {
  55. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  56. Session session;
  57. session.SetUrl(url);
  58. session.SetRedirect(Redirect(false));
  59. Response response = session.Get();
  60. std::string expected_text{"Moved Permanently"};
  61. EXPECT_EQ(expected_text, response.text);
  62. EXPECT_EQ(url, response.url);
  63. EXPECT_EQ(std::string{}, response.header["content-type"]);
  64. EXPECT_EQ(301, response.status_code);
  65. EXPECT_EQ(ErrorCode::OK, response.error.code);
  66. }
  67. TEST(MaxRedirectsTests, ZeroMaxRedirectsSuccessTest) {
  68. Url url{server->GetBaseUrl() + "/hello.html"};
  69. Session session;
  70. session.SetUrl(url);
  71. session.SetRedirect(Redirect(0L));
  72. Response response = session.Get();
  73. std::string expected_text{"Hello world!"};
  74. EXPECT_EQ(expected_text, response.text);
  75. EXPECT_EQ(url, response.url);
  76. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  77. EXPECT_EQ(200, response.status_code);
  78. EXPECT_EQ(ErrorCode::OK, response.error.code);
  79. }
  80. TEST(MaxRedirectsTests, ZeroMaxRedirectsFailureTest) {
  81. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  82. Session session;
  83. session.SetUrl(url);
  84. session.SetRedirect(Redirect(0L));
  85. Response response = session.Get();
  86. EXPECT_EQ(std::string{}, response.text);
  87. EXPECT_EQ(url, response.url);
  88. EXPECT_EQ(std::string{}, response.header["content-type"]);
  89. EXPECT_EQ(301, response.status_code);
  90. EXPECT_EQ(ErrorCode::TOO_MANY_REDIRECTS, response.error.code);
  91. }
  92. TEST(MaxRedirectsTests, OneMaxRedirectsSuccessTest) {
  93. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  94. Session session;
  95. session.SetUrl(url);
  96. session.SetRedirect(Redirect(1L));
  97. Response response = session.Get();
  98. std::string expected_text{"Hello world!"};
  99. EXPECT_EQ(expected_text, response.text);
  100. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
  101. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  102. EXPECT_EQ(200, response.status_code);
  103. EXPECT_EQ(ErrorCode::OK, response.error.code);
  104. }
  105. TEST(MaxRedirectsTests, OneMaxRedirectsFailureTest) {
  106. Url url{server->GetBaseUrl() + "/two_redirects.html"};
  107. Session session;
  108. session.SetUrl(url);
  109. session.SetRedirect(Redirect(1L));
  110. Response response = session.Get();
  111. EXPECT_EQ(std::string{}, response.text);
  112. EXPECT_EQ(Url{server->GetBaseUrl() + "/permanent_redirect.html"}, response.url);
  113. EXPECT_EQ(std::string{}, response.header["content-type"]);
  114. EXPECT_EQ(301, response.status_code);
  115. EXPECT_EQ(ErrorCode::TOO_MANY_REDIRECTS, response.error.code);
  116. }
  117. TEST(MaxRedirectsTests, TwoMaxRedirectsSuccessTest) {
  118. Url url{server->GetBaseUrl() + "/two_redirects.html"};
  119. Session session;
  120. session.SetUrl(url);
  121. session.SetRedirect(Redirect(2L));
  122. Response response = session.Get();
  123. std::string expected_text{"Hello world!"};
  124. EXPECT_EQ(expected_text, response.text);
  125. EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
  126. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  127. EXPECT_EQ(200, response.status_code);
  128. EXPECT_EQ(ErrorCode::OK, response.error.code);
  129. }
  130. TEST(MultipleGetTests, BasicMultipleGetTest) {
  131. Url url{server->GetBaseUrl() + "/hello.html"};
  132. Session session;
  133. session.SetUrl(url);
  134. for (size_t i = 0; i < 100; ++i) {
  135. Response response = session.Get();
  136. std::string expected_text{"Hello world!"};
  137. EXPECT_EQ(expected_text, response.text);
  138. EXPECT_EQ(url, response.url);
  139. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  140. EXPECT_EQ(200, response.status_code);
  141. EXPECT_EQ(ErrorCode::OK, response.error.code);
  142. }
  143. }
  144. TEST(MultipleGetTests, UrlChangeMultipleGetTest) {
  145. Session session;
  146. {
  147. Url url{server->GetBaseUrl() + "/hello.html"};
  148. session.SetUrl(url);
  149. Response response = session.Get();
  150. std::string expected_text{"Hello world!"};
  151. EXPECT_EQ(expected_text, response.text);
  152. EXPECT_EQ(url, response.url);
  153. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  154. EXPECT_EQ(200, response.status_code);
  155. EXPECT_EQ(ErrorCode::OK, response.error.code);
  156. }
  157. {
  158. Url url{server->GetBaseUrl() + "/basic.json"};
  159. session.SetUrl(url);
  160. Response response = session.Get();
  161. std::string expected_text{
  162. "[\n"
  163. " {\n"
  164. " \"first_key\": \"first_value\",\n"
  165. " \"second_key\": \"second_value\"\n"
  166. " }\n"
  167. "]"};
  168. EXPECT_EQ(expected_text, response.text);
  169. EXPECT_EQ(url, response.url);
  170. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  171. EXPECT_EQ(200, response.status_code);
  172. EXPECT_EQ(ErrorCode::OK, response.error.code);
  173. }
  174. }
  175. TEST(MultipleGetTests, HeaderMultipleGetTest) {
  176. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  177. Session session;
  178. session.SetUrl(url);
  179. session.SetHeader(Header{{"hello", "world"}});
  180. for (size_t i = 0; i < 100; ++i) {
  181. Response response = session.Get();
  182. std::string expected_text{"Header reflect GET"};
  183. EXPECT_EQ(expected_text, response.text);
  184. EXPECT_EQ(url, response.url);
  185. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  186. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  187. EXPECT_EQ(200, response.status_code);
  188. EXPECT_EQ(ErrorCode::OK, response.error.code);
  189. }
  190. }
  191. TEST(MultipleGetTests, HeaderChangeMultipleGetTest) {
  192. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  193. Session session;
  194. session.SetUrl(url);
  195. session.SetHeader(Header{{"hello", "world"}});
  196. {
  197. Response response = session.Get();
  198. std::string expected_text{"Header reflect GET"};
  199. EXPECT_EQ(expected_text, response.text);
  200. EXPECT_EQ(url, response.url);
  201. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  202. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  203. EXPECT_EQ(200, response.status_code);
  204. EXPECT_EQ(ErrorCode::OK, response.error.code);
  205. }
  206. session.SetHeader(Header{{"key", "value"}});
  207. {
  208. Response response = session.Get();
  209. std::string expected_text{"Header reflect GET"};
  210. EXPECT_EQ(expected_text, response.text);
  211. EXPECT_EQ(url, response.url);
  212. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  213. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  214. EXPECT_EQ(200, response.status_code);
  215. EXPECT_EQ(ErrorCode::OK, response.error.code);
  216. }
  217. }
  218. TEST(MultipleGetTests, ParameterMultipleGetTest) {
  219. Url url{server->GetBaseUrl() + "/hello.html"};
  220. Session session;
  221. session.SetUrl(url);
  222. session.SetParameters({{"hello", "world"}});
  223. for (size_t i = 0; i < 100; ++i) {
  224. Response response = session.Get();
  225. std::string expected_text{"Hello world!"};
  226. EXPECT_EQ(expected_text, response.text);
  227. EXPECT_EQ(Url{url + "?hello=world"}, response.url);
  228. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  229. EXPECT_EQ(200, response.status_code);
  230. EXPECT_EQ(ErrorCode::OK, response.error.code);
  231. }
  232. }
  233. TEST(MultipleGetTests, ParameterChangeMultipleGetTest) {
  234. Url url{server->GetBaseUrl() + "/hello.html"};
  235. Session session;
  236. session.SetUrl(url);
  237. session.SetParameters({{"hello", "world"}});
  238. {
  239. Response response = session.Get();
  240. std::string expected_text{"Hello world!"};
  241. EXPECT_EQ(expected_text, response.text);
  242. EXPECT_EQ(Url{url + "?hello=world"}, response.url);
  243. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  244. EXPECT_EQ(200, response.status_code);
  245. EXPECT_EQ(ErrorCode::OK, response.error.code);
  246. }
  247. session.SetUrl(url);
  248. session.SetParameters({{"key", "value"}});
  249. {
  250. Response response = session.Get();
  251. std::string expected_text{"Hello world!"};
  252. EXPECT_EQ(expected_text, response.text);
  253. EXPECT_EQ(Url{url + "?key=value"}, response.url);
  254. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  255. EXPECT_EQ(200, response.status_code);
  256. EXPECT_EQ(ErrorCode::OK, response.error.code);
  257. }
  258. }
  259. TEST(MultipleGetTests, BasicAuthenticationMultipleGetTest) {
  260. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  261. Session session;
  262. session.SetUrl(url);
  263. session.SetAuth(Authentication{"user", "password", AuthMode::BASIC});
  264. for (size_t i = 0; i < 100; ++i) {
  265. Response response = session.Get();
  266. std::string expected_text{"Header reflect GET"};
  267. EXPECT_EQ(expected_text, response.text);
  268. EXPECT_EQ(url, response.url);
  269. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  270. EXPECT_EQ(200, response.status_code);
  271. EXPECT_EQ(ErrorCode::OK, response.error.code);
  272. }
  273. }
  274. TEST(MultipleGetTests, BasicAuthenticationChangeMultipleGetTest) {
  275. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  276. Session session;
  277. session.SetUrl(url);
  278. session.SetAuth(Authentication{"user", "password", AuthMode::BASIC});
  279. {
  280. Response response = session.Get();
  281. std::string expected_text{"Header reflect GET"};
  282. EXPECT_EQ(expected_text, response.text);
  283. EXPECT_EQ(url, response.url);
  284. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  285. EXPECT_EQ(200, response.status_code);
  286. EXPECT_EQ(ErrorCode::OK, response.error.code);
  287. }
  288. session.SetAuth(Authentication{"user", "bad_password", AuthMode::BASIC});
  289. {
  290. Response response = session.Get();
  291. EXPECT_EQ(std::string{"Unauthorized"}, response.text);
  292. EXPECT_EQ(url, response.url);
  293. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  294. EXPECT_EQ(401, response.status_code);
  295. EXPECT_EQ(ErrorCode::OK, response.error.code);
  296. }
  297. session.SetAuth(Authentication{"bad_user", "password", AuthMode::BASIC});
  298. {
  299. Response response = session.Get();
  300. EXPECT_EQ(std::string{"Unauthorized"}, response.text);
  301. EXPECT_EQ(url, response.url);
  302. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  303. EXPECT_EQ(401, response.status_code);
  304. EXPECT_EQ(ErrorCode::OK, response.error.code);
  305. }
  306. }
  307. TEST(ParameterTests, ParameterSingleTest) {
  308. Url url{server->GetBaseUrl() + "/hello.html"};
  309. Session session;
  310. session.SetUrl(url);
  311. Parameters parameters{{"hello", "world"}};
  312. session.SetParameters(parameters);
  313. Response response = session.Get();
  314. std::string expected_text{"Hello world!"};
  315. EXPECT_EQ(expected_text, response.text);
  316. EXPECT_EQ(Url{url + "?hello=world"}, response.url);
  317. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  318. EXPECT_EQ(200, response.status_code);
  319. EXPECT_EQ(ErrorCode::OK, response.error.code);
  320. }
  321. TEST(ParameterTests, ParameterMultipleTest) {
  322. Url url{server->GetBaseUrl() + "/hello.html"};
  323. Session session;
  324. session.SetUrl(url);
  325. Parameters parameters{{"hello", "world"}, {"key", "value"}};
  326. session.SetParameters(parameters);
  327. Response response = session.Get();
  328. std::string expected_text{"Hello world!"};
  329. EXPECT_EQ(expected_text, response.text);
  330. EXPECT_EQ(Url{url + "?hello=world&key=value"}, response.url);
  331. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  332. EXPECT_EQ(200, response.status_code);
  333. EXPECT_EQ(ErrorCode::OK, response.error.code);
  334. }
  335. TEST(FullRequestUrlTest, GetFullRequestUrlNoParametersTest) {
  336. Url url{server->GetBaseUrl() + "/hello.html"};
  337. Session session;
  338. session.SetUrl(url);
  339. std::string expected_text{server->GetBaseUrl() + "/hello.html"};
  340. EXPECT_EQ(expected_text, session.GetFullRequestUrl());
  341. }
  342. TEST(FullRequestUrlTest, GetFullRequestUrlOneParameterTest) {
  343. Url url{server->GetBaseUrl() + "/hello.html"};
  344. Session session;
  345. session.SetUrl(url);
  346. Parameters parameters{{"hello", "world"}};
  347. session.SetParameters(parameters);
  348. std::string expected_text{server->GetBaseUrl() + "/hello.html" + "?hello=world"};
  349. EXPECT_EQ(expected_text, session.GetFullRequestUrl());
  350. }
  351. TEST(FullRequestUrlTest, GetFullRequestUrlMultipleParametersTest) {
  352. Url url{server->GetBaseUrl() + "/hello.html"};
  353. Session session;
  354. session.SetUrl(url);
  355. Parameters parameters{{"hello", "world"}, {"key", "value"}};
  356. session.SetParameters(parameters);
  357. std::string expected_text{server->GetBaseUrl() + "/hello.html" + "?hello=world&key=value"};
  358. EXPECT_EQ(expected_text, session.GetFullRequestUrl());
  359. }
  360. TEST(TimeoutTests, SetTimeoutTest) {
  361. Url url{server->GetBaseUrl() + "/hello.html"};
  362. Session session;
  363. session.SetUrl(url);
  364. session.SetTimeout(0L);
  365. Response response = session.Get();
  366. std::string expected_text{"Hello world!"};
  367. EXPECT_EQ(expected_text, response.text);
  368. EXPECT_EQ(url, response.url);
  369. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  370. EXPECT_EQ(200, response.status_code);
  371. EXPECT_EQ(ErrorCode::OK, response.error.code);
  372. }
  373. TEST(TimeoutTests, SetTimeoutLongTest) {
  374. Url url{server->GetBaseUrl() + "/hello.html"};
  375. Session session;
  376. session.SetUrl(url);
  377. session.SetTimeout(10000L);
  378. Response response = session.Get();
  379. std::string expected_text{"Hello world!"};
  380. EXPECT_EQ(expected_text, response.text);
  381. EXPECT_EQ(url, response.url);
  382. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  383. EXPECT_EQ(200, response.status_code);
  384. EXPECT_EQ(ErrorCode::OK, response.error.code);
  385. }
  386. TEST(TimeoutTests, SetTimeoutLowSpeed) {
  387. Url url{server->GetBaseUrl() + "/low_speed_timeout.html"};
  388. Session session;
  389. session.SetUrl(url);
  390. session.SetTimeout(1000);
  391. Response response = session.Get();
  392. EXPECT_EQ(url, response.url);
  393. // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
  394. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  395. }
  396. TEST(TimeoutTests, SetChronoTimeoutTest) {
  397. Url url{server->GetBaseUrl() + "/hello.html"};
  398. Session session;
  399. session.SetUrl(url);
  400. session.SetTimeout(std::chrono::milliseconds{0});
  401. Response response = session.Get();
  402. std::string expected_text{"Hello world!"};
  403. EXPECT_EQ(expected_text, response.text);
  404. EXPECT_EQ(url, response.url);
  405. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  406. EXPECT_EQ(200, response.status_code);
  407. EXPECT_EQ(ErrorCode::OK, response.error.code);
  408. }
  409. TEST(TimeoutTests, SetChronoTimeoutLongTest) {
  410. Url url{server->GetBaseUrl() + "/hello.html"};
  411. Session session;
  412. session.SetUrl(url);
  413. session.SetTimeout(std::chrono::milliseconds{10000});
  414. Response response = session.Get();
  415. std::string expected_text{"Hello world!"};
  416. EXPECT_EQ(expected_text, response.text);
  417. EXPECT_EQ(url, response.url);
  418. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  419. // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
  420. EXPECT_EQ(ErrorCode::OK, response.error.code);
  421. }
  422. TEST(TimeoutTests, SetChronoLiteralTimeoutTest) {
  423. Url url{server->GetBaseUrl() + "/hello.html"};
  424. Session session;
  425. session.SetUrl(url);
  426. session.SetTimeout(2s);
  427. Response response = session.Get();
  428. std::string expected_text{"Hello world!"};
  429. EXPECT_EQ(expected_text, response.text);
  430. EXPECT_EQ(url, response.url);
  431. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  432. EXPECT_EQ(200, response.status_code);
  433. EXPECT_EQ(ErrorCode::OK, response.error.code);
  434. }
  435. TEST(TimeoutTests, SetChronoLiteralTimeoutLowSpeed) {
  436. Url url{server->GetBaseUrl() + "/low_speed_timeout.html"};
  437. Session session;
  438. session.SetUrl(url);
  439. session.SetTimeout(1000ms);
  440. Response response = session.Get();
  441. EXPECT_EQ(url, response.url);
  442. // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
  443. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  444. }
  445. TEST(ConnectTimeoutTests, SetConnectTimeoutTest) {
  446. Url url{server->GetBaseUrl() + "/hello.html"};
  447. Session session;
  448. session.SetUrl(url);
  449. session.SetConnectTimeout(0L);
  450. Response response = session.Get();
  451. std::string expected_text{"Hello world!"};
  452. EXPECT_EQ(expected_text, response.text);
  453. EXPECT_EQ(url, response.url);
  454. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  455. EXPECT_EQ(200, response.status_code);
  456. EXPECT_EQ(ErrorCode::OK, response.error.code);
  457. }
  458. TEST(ConnectTimeoutTests, SetConnectTimeoutLongTest) {
  459. Url url{server->GetBaseUrl() + "/hello.html"};
  460. Session session;
  461. session.SetUrl(url);
  462. session.SetConnectTimeout(10000L);
  463. Response response = session.Get();
  464. std::string expected_text{"Hello world!"};
  465. EXPECT_EQ(expected_text, response.text);
  466. EXPECT_EQ(url, response.url);
  467. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  468. EXPECT_EQ(200, response.status_code);
  469. EXPECT_EQ(ErrorCode::OK, response.error.code);
  470. }
  471. TEST(ConnectTimeoutTests, SetChronoConnectTimeoutTest) {
  472. Url url{server->GetBaseUrl() + "/hello.html"};
  473. Session session;
  474. session.SetUrl(url);
  475. session.SetConnectTimeout(std::chrono::milliseconds{0});
  476. Response response = session.Get();
  477. std::string expected_text{"Hello world!"};
  478. EXPECT_EQ(expected_text, response.text);
  479. EXPECT_EQ(url, response.url);
  480. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  481. EXPECT_EQ(200, response.status_code);
  482. EXPECT_EQ(ErrorCode::OK, response.error.code);
  483. }
  484. TEST(ConnectTimeoutTests, SetChronoConnectTimeoutLongTest) {
  485. Url url{server->GetBaseUrl() + "/hello.html"};
  486. Session session;
  487. session.SetUrl(url);
  488. session.SetConnectTimeout(std::chrono::milliseconds{10000});
  489. Response response = session.Get();
  490. std::string expected_text{"Hello world!"};
  491. EXPECT_EQ(expected_text, response.text);
  492. EXPECT_EQ(url, response.url);
  493. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  494. EXPECT_EQ(200, response.status_code);
  495. EXPECT_EQ(ErrorCode::OK, response.error.code);
  496. }
  497. TEST(LowSpeedTests, SetLowSpeedTest) {
  498. Url url{server->GetBaseUrl() + "/hello.html"};
  499. Session session;
  500. session.SetUrl(url);
  501. session.SetLowSpeed({1, 1});
  502. Response response = session.Get();
  503. std::string expected_text{"Hello world!"};
  504. EXPECT_EQ(expected_text, response.text);
  505. EXPECT_EQ(url, response.url);
  506. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  507. EXPECT_EQ(200, response.status_code);
  508. EXPECT_EQ(ErrorCode::OK, response.error.code);
  509. }
  510. TEST(PayloadTests, SetPayloadTest) {
  511. Url url{server->GetBaseUrl() + "/url_post.html"};
  512. Session session;
  513. session.SetUrl(url);
  514. session.SetPayload({{"x", "5"}});
  515. Response response = session.Post();
  516. std::string expected_text{
  517. "{\n"
  518. " \"x\": 5\n"
  519. "}"};
  520. EXPECT_EQ(expected_text, response.text);
  521. EXPECT_EQ(url, response.url);
  522. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  523. EXPECT_EQ(201, response.status_code);
  524. EXPECT_EQ(ErrorCode::OK, response.error.code);
  525. }
  526. TEST(PayloadTests, SetPayloadLValueTest) {
  527. Url url{server->GetBaseUrl() + "/url_post.html"};
  528. Session session;
  529. session.SetUrl(url);
  530. Payload payload{{"x", "5"}};
  531. session.SetPayload(payload);
  532. Response response = session.Post();
  533. std::string expected_text{
  534. "{\n"
  535. " \"x\": 5\n"
  536. "}"};
  537. EXPECT_EQ(expected_text, response.text);
  538. EXPECT_EQ(url, response.url);
  539. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  540. EXPECT_EQ(201, response.status_code);
  541. EXPECT_EQ(ErrorCode::OK, response.error.code);
  542. }
  543. TEST(MultipartTests, SetMultipartTest) {
  544. Url url{server->GetBaseUrl() + "/form_post.html"};
  545. Session session;
  546. session.SetUrl(url);
  547. session.SetMultipart({{"x", "5"}});
  548. Response response = session.Post();
  549. std::string expected_text{
  550. "{\n"
  551. " \"x\": \"5\"\n"
  552. "}"};
  553. EXPECT_EQ(expected_text, response.text);
  554. EXPECT_EQ(url, response.url);
  555. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  556. EXPECT_EQ(201, response.status_code);
  557. EXPECT_EQ(ErrorCode::OK, response.error.code);
  558. }
  559. TEST(MultipartTests, SetMultipartValueTest) {
  560. Url url{server->GetBaseUrl() + "/form_post.html"};
  561. Session session;
  562. session.SetUrl(url);
  563. Multipart multipart{{"x", "5"}};
  564. session.SetMultipart(multipart);
  565. Response response = session.Post();
  566. std::string expected_text{
  567. "{\n"
  568. " \"x\": \"5\"\n"
  569. "}"};
  570. EXPECT_EQ(expected_text, response.text);
  571. EXPECT_EQ(url, response.url);
  572. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  573. EXPECT_EQ(201, response.status_code);
  574. EXPECT_EQ(ErrorCode::OK, response.error.code);
  575. }
  576. TEST(BodyTests, SetBodyTest) {
  577. Url url{server->GetBaseUrl() + "/url_post.html"};
  578. Session session;
  579. session.SetUrl(url);
  580. session.SetBody(Body{"x=5"});
  581. Response response = session.Post();
  582. std::string expected_text{
  583. "{\n"
  584. " \"x\": 5\n"
  585. "}"};
  586. EXPECT_EQ(expected_text, response.text);
  587. EXPECT_EQ(url, response.url);
  588. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  589. EXPECT_EQ(201, response.status_code);
  590. EXPECT_EQ(ErrorCode::OK, response.error.code);
  591. }
  592. TEST(BodyTests, SetBodyValueTest) {
  593. Url url{server->GetBaseUrl() + "/url_post.html"};
  594. Session session;
  595. session.SetUrl(url);
  596. Body body{"x=5"};
  597. session.SetBody(body);
  598. Response response = session.Post();
  599. std::string expected_text{
  600. "{\n"
  601. " \"x\": 5\n"
  602. "}"};
  603. EXPECT_EQ(expected_text, response.text);
  604. EXPECT_EQ(url, response.url);
  605. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  606. EXPECT_EQ(201, response.status_code);
  607. EXPECT_EQ(ErrorCode::OK, response.error.code);
  608. }
  609. TEST(DigestTests, SetDigestTest) {
  610. Url url{server->GetBaseUrl() + "/digest_auth.html"};
  611. Session session;
  612. session.SetUrl(url);
  613. session.SetAuth({"user", "password", AuthMode::DIGEST});
  614. Response response = session.Get();
  615. std::string expected_text{"Header reflect GET"};
  616. EXPECT_EQ(expected_text, response.text);
  617. EXPECT_EQ(url, response.url);
  618. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  619. EXPECT_EQ(200, response.status_code);
  620. EXPECT_EQ(ErrorCode::OK, response.error.code);
  621. }
  622. TEST(UserAgentTests, SetUserAgentTest) {
  623. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  624. UserAgent userAgent{"Test User Agent"};
  625. Session session;
  626. session.SetUrl(url);
  627. session.SetUserAgent(userAgent);
  628. Response response = session.Get();
  629. std::string expected_text{"Header reflect GET"};
  630. EXPECT_EQ(expected_text, response.text);
  631. EXPECT_EQ(url, response.url);
  632. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  633. EXPECT_EQ(userAgent, response.header["User-Agent"]);
  634. EXPECT_EQ(200, response.status_code);
  635. EXPECT_EQ(ErrorCode::OK, response.error.code);
  636. }
  637. TEST(UserAgentTests, SetUserAgentStringViewTest) {
  638. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  639. UserAgent userAgent{std::string_view{"Test User Agent"}};
  640. Session session;
  641. session.SetUrl(url);
  642. session.SetUserAgent(userAgent);
  643. Response response = session.Get();
  644. std::string expected_text{"Header reflect GET"};
  645. EXPECT_EQ(expected_text, response.text);
  646. EXPECT_EQ(url, response.url);
  647. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  648. EXPECT_EQ(userAgent, response.header["User-Agent"]);
  649. EXPECT_EQ(200, response.status_code);
  650. EXPECT_EQ(ErrorCode::OK, response.error.code);
  651. }
  652. TEST(CookiesTests, BasicCookiesTest) {
  653. Url url{server->GetBaseUrl() + "/basic_cookies.html"};
  654. Session session{};
  655. session.SetUrl(url);
  656. Response response = session.Get();
  657. Cookies res_cookies{response.cookies};
  658. std::string expected_text{"Basic Cookies"};
  659. cpr::Cookies expectedCookies{
  660. {"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  661. {"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  662. };
  663. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  664. EXPECT_EQ(200, response.status_code);
  665. EXPECT_EQ(ErrorCode::OK, response.error.code);
  666. EXPECT_EQ(expected_text, response.text);
  667. for (auto cookie = res_cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != res_cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
  668. EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
  669. EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
  670. EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
  671. EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
  672. EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
  673. EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
  674. EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
  675. }
  676. }
  677. TEST(CookiesTests, ClientSetCookiesTest) {
  678. Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
  679. {
  680. Session session{};
  681. session.SetUrl(url);
  682. session.SetCookies(Cookies{
  683. {"SID", "31d4d96e407aad42"},
  684. {"lang", "en-US"},
  685. });
  686. Response response = session.Get();
  687. std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
  688. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  689. EXPECT_EQ(200, response.status_code);
  690. EXPECT_EQ(ErrorCode::OK, response.error.code);
  691. EXPECT_EQ(expected_text, response.text);
  692. }
  693. {
  694. Session session{};
  695. session.SetUrl(url);
  696. Cookies cookie{
  697. {"SID", "31d4d96e407aad42"},
  698. {"lang", "en-US"},
  699. };
  700. session.SetCookies(cookie);
  701. Response response = session.Get();
  702. std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
  703. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  704. EXPECT_EQ(200, response.status_code);
  705. EXPECT_EQ(ErrorCode::OK, response.error.code);
  706. EXPECT_EQ(expected_text, response.text);
  707. }
  708. }
  709. TEST(CookiesTests, RedirectionWithChangingCookiesTest) {
  710. Url url{server->GetBaseUrl() + "/redirection_with_changing_cookies.html"};
  711. {
  712. Session session{};
  713. session.SetUrl(url);
  714. session.SetCookies(Cookies{
  715. {"SID", "31d4d96e407aad42"},
  716. {"lang", "en-US"},
  717. });
  718. session.SetRedirect(Redirect(0L));
  719. Response response = session.Get();
  720. std::string expected_text{"Received cookies are: SID=31d4d96e407aad42; lang=en-US;"};
  721. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  722. EXPECT_EQ(200, response.status_code);
  723. EXPECT_EQ(ErrorCode::OK, response.error.code);
  724. EXPECT_EQ(expected_text, response.text);
  725. }
  726. {
  727. Session session{};
  728. session.SetUrl(url);
  729. session.SetRedirect(Redirect(1L));
  730. Response response = session.Get();
  731. std::string expected_text{"Received cookies are: lang=en-US; SID=31d4d96e407aad42"};
  732. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  733. EXPECT_EQ(200, response.status_code);
  734. EXPECT_EQ(ErrorCode::OK, response.error.code);
  735. EXPECT_EQ(expected_text, response.text);
  736. }
  737. {
  738. Session session{};
  739. session.SetUrl(url);
  740. session.SetCookies(Cookies{
  741. {"SID", "empty_sid"},
  742. });
  743. session.SetRedirect(Redirect(1L));
  744. Response response = session.Get();
  745. std::string expected_text{"Received cookies are: lang=en-US; SID=31d4d96e407aad42; SID=empty_sid;"};
  746. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  747. EXPECT_EQ(200, response.status_code);
  748. EXPECT_EQ(ErrorCode::OK, response.error.code);
  749. EXPECT_EQ(expected_text, response.text);
  750. }
  751. }
  752. TEST(DifferentMethodTests, GetPostTest) {
  753. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  754. Session session;
  755. session.SetUrl(url);
  756. {
  757. Response response = session.Get();
  758. std::string expected_text{"Header reflect GET"};
  759. EXPECT_EQ(expected_text, response.text);
  760. EXPECT_EQ(url, response.url);
  761. EXPECT_EQ(200, response.status_code);
  762. EXPECT_EQ(ErrorCode::OK, response.error.code);
  763. }
  764. {
  765. Response response = session.Post();
  766. std::string expected_text{"Header reflect POST"};
  767. EXPECT_EQ(expected_text, response.text);
  768. EXPECT_EQ(url, response.url);
  769. EXPECT_EQ(200, response.status_code);
  770. EXPECT_EQ(ErrorCode::OK, response.error.code);
  771. }
  772. }
  773. TEST(DifferentMethodTests, PostGetTest) {
  774. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  775. Session session;
  776. session.SetUrl(url);
  777. {
  778. Response response = session.Post();
  779. std::string expected_text{"Header reflect POST"};
  780. EXPECT_EQ(expected_text, response.text);
  781. EXPECT_EQ(url, response.url);
  782. EXPECT_EQ(200, response.status_code);
  783. EXPECT_EQ(ErrorCode::OK, response.error.code);
  784. }
  785. {
  786. Response response = session.Get();
  787. std::string expected_text{"Header reflect GET"};
  788. EXPECT_EQ(expected_text, response.text);
  789. EXPECT_EQ(url, response.url);
  790. EXPECT_EQ(200, response.status_code);
  791. EXPECT_EQ(ErrorCode::OK, response.error.code);
  792. }
  793. }
  794. TEST(DifferentMethodTests, GetPostGetTest) {
  795. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  796. Session session;
  797. session.SetUrl(url);
  798. {
  799. Response response = session.Get();
  800. std::string expected_text{"Header reflect GET"};
  801. EXPECT_EQ(expected_text, response.text);
  802. EXPECT_EQ(url, response.url);
  803. EXPECT_EQ(200, response.status_code);
  804. EXPECT_EQ(ErrorCode::OK, response.error.code);
  805. }
  806. {
  807. Response response = session.Post();
  808. std::string expected_text{"Header reflect POST"};
  809. EXPECT_EQ(expected_text, response.text);
  810. EXPECT_EQ(url, response.url);
  811. EXPECT_EQ(200, response.status_code);
  812. EXPECT_EQ(ErrorCode::OK, response.error.code);
  813. }
  814. {
  815. Response response = session.Get();
  816. std::string expected_text{"Header reflect GET"};
  817. EXPECT_EQ(expected_text, response.text);
  818. EXPECT_EQ(url, response.url);
  819. EXPECT_EQ(200, response.status_code);
  820. EXPECT_EQ(ErrorCode::OK, response.error.code);
  821. }
  822. }
  823. TEST(DifferentMethodTests, PostGetPostTest) {
  824. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  825. Session session;
  826. session.SetUrl(url);
  827. {
  828. Response response = session.Post();
  829. std::string expected_text{"Header reflect POST"};
  830. EXPECT_EQ(expected_text, response.text);
  831. EXPECT_EQ(url, response.url);
  832. EXPECT_EQ(200, response.status_code);
  833. EXPECT_EQ(ErrorCode::OK, response.error.code);
  834. }
  835. {
  836. Response response = session.Get();
  837. std::string expected_text{"Header reflect GET"};
  838. EXPECT_EQ(expected_text, response.text);
  839. EXPECT_EQ(url, response.url);
  840. EXPECT_EQ(200, response.status_code);
  841. EXPECT_EQ(ErrorCode::OK, response.error.code);
  842. }
  843. {
  844. Response response = session.Post();
  845. std::string expected_text{"Header reflect POST"};
  846. EXPECT_EQ(expected_text, response.text);
  847. EXPECT_EQ(url, response.url);
  848. EXPECT_EQ(200, response.status_code);
  849. EXPECT_EQ(ErrorCode::OK, response.error.code);
  850. }
  851. }
  852. TEST(DifferentMethodTests, MultipleGetPostTest) {
  853. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  854. Session session;
  855. session.SetUrl(url);
  856. for (size_t i = 0; i < 100; ++i) {
  857. {
  858. Response response = session.Get();
  859. std::string expected_text{"Header reflect GET"};
  860. EXPECT_EQ(expected_text, response.text);
  861. EXPECT_EQ(url, response.url);
  862. EXPECT_EQ(200, response.status_code);
  863. EXPECT_EQ(ErrorCode::OK, response.error.code);
  864. }
  865. {
  866. Response response = session.Post();
  867. std::string expected_text{"Header reflect POST"};
  868. EXPECT_EQ(expected_text, response.text);
  869. EXPECT_EQ(url, response.url);
  870. EXPECT_EQ(200, response.status_code);
  871. EXPECT_EQ(ErrorCode::OK, response.error.code);
  872. }
  873. }
  874. }
  875. TEST(DifferentMethodTests, MultipleDeleteHeadPutGetPostTest) {
  876. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  877. Url urlPost{server->GetBaseUrl() + "/post_reflect.html"};
  878. Url urlPut{server->GetBaseUrl() + "/put.html"};
  879. Session session;
  880. for (size_t i = 0; i < 10; ++i) {
  881. {
  882. session.SetUrl(url);
  883. Response response = session.Delete();
  884. std::string expected_text{"Header reflect DELETE"};
  885. EXPECT_EQ(expected_text, response.text);
  886. EXPECT_EQ(url, response.url);
  887. EXPECT_EQ(200, response.status_code);
  888. EXPECT_EQ(ErrorCode::OK, response.error.code);
  889. }
  890. {
  891. session.SetUrl(urlPost);
  892. std::string expectedBody = "a1b2c3Post";
  893. session.SetBody(expectedBody);
  894. Response response = session.Post();
  895. EXPECT_EQ(expectedBody, response.text);
  896. EXPECT_EQ(urlPost, response.url);
  897. EXPECT_EQ(200, response.status_code);
  898. EXPECT_EQ(ErrorCode::OK, response.error.code);
  899. }
  900. {
  901. session.SetUrl(url);
  902. Response response = session.Get();
  903. std::string expected_text{"Header reflect GET"};
  904. EXPECT_EQ(expected_text, response.text);
  905. EXPECT_EQ(url, response.url);
  906. EXPECT_EQ(200, response.status_code);
  907. EXPECT_EQ(ErrorCode::OK, response.error.code);
  908. }
  909. {
  910. session.SetUrl(urlPut);
  911. session.SetPayload({{"x", "5"}});
  912. Response response = session.Put();
  913. std::string expected_text{
  914. "{\n"
  915. " \"x\": 5\n"
  916. "}"};
  917. EXPECT_EQ(expected_text, response.text);
  918. EXPECT_EQ(urlPut, response.url);
  919. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  920. EXPECT_EQ(200, response.status_code);
  921. EXPECT_EQ(ErrorCode::OK, response.error.code);
  922. }
  923. {
  924. session.SetUrl(url);
  925. Response response = session.Head();
  926. std::string expected_text{"Header reflect HEAD"};
  927. EXPECT_EQ(url, response.url);
  928. EXPECT_EQ(200, response.status_code);
  929. EXPECT_EQ(ErrorCode::OK, response.error.code);
  930. }
  931. }
  932. }
  933. TEST(CurlHolderManipulateTests, CustomOptionTest) {
  934. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  935. Session session;
  936. session.SetUrl(url);
  937. curl_easy_setopt(session.GetCurlHolder()->handle, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST | CURLSSLOPT_NO_REVOKE);
  938. {
  939. Response response = session.Get();
  940. std::string expected_text{"Header reflect GET"};
  941. EXPECT_EQ(expected_text, response.text);
  942. EXPECT_EQ(url, response.url);
  943. EXPECT_EQ(200, response.status_code);
  944. EXPECT_EQ(ErrorCode::OK, response.error.code);
  945. }
  946. {
  947. Response response = session.Post();
  948. std::string expected_text{"Header reflect POST"};
  949. EXPECT_EQ(expected_text, response.text);
  950. EXPECT_EQ(url, response.url);
  951. EXPECT_EQ(200, response.status_code);
  952. EXPECT_EQ(ErrorCode::OK, response.error.code);
  953. }
  954. }
  955. TEST(LocalPortTests, SetLocalPortTest) {
  956. Url url{server->GetBaseUrl() + "/local_port.html"};
  957. Session session;
  958. session.SetUrl(url);
  959. std::uint16_t const local_port = 60252; // beware of HttpServer::GetPort when changing
  960. std::uint16_t const local_port_range = 5000;
  961. session.SetLocalPort(local_port);
  962. session.SetLocalPortRange(local_port_range);
  963. // expected response: body contains port number in specified range
  964. // NOTE: even when trying up to 5000 ports there is the chance that all of them are occupied.
  965. // It would be possible to also check here for ErrorCode::INTERNAL_ERROR but that somehow seems
  966. // wrong as then this test would pass in case SetLocalPort does not work at all
  967. // or in other words: we have to assume that at least one port in the specified range is free.
  968. Response response = session.Get();
  969. EXPECT_EQ(200, response.status_code);
  970. EXPECT_EQ(ErrorCode::OK, response.error.code);
  971. std::uint16_t port_from_response = std::strtoul(response.text.c_str(), nullptr, 10);
  972. EXPECT_EQ(errno, 0);
  973. EXPECT_GE(port_from_response, local_port);
  974. EXPECT_LE(port_from_response, local_port + local_port_range);
  975. }
  976. TEST(LocalPortTests, SetOptionTest) {
  977. Url url{server->GetBaseUrl() + "/local_port.html"};
  978. Session session;
  979. session.SetUrl(url);
  980. std::uint16_t const local_port = 60551; // beware of HttpServer::GetPort when changing
  981. std::uint16_t const local_port_range = 5000;
  982. session.SetOption(LocalPort(local_port));
  983. session.SetOption(LocalPortRange(local_port_range));
  984. // expected response: body contains port number in specified range
  985. // NOTE: even when trying up to 5000 ports there is the chance that all of them are occupied.
  986. // It would be possible to also check here for ErrorCode::INTERNAL_ERROR but that somehow seems
  987. // wrong as then this test would pass in case SetOption(LocalPort) does not work at all
  988. // or in other words: we have to assume that at least one port in the specified range is free.
  989. Response response = session.Get();
  990. EXPECT_EQ(200, response.status_code);
  991. EXPECT_EQ(ErrorCode::OK, response.error.code);
  992. unsigned long port_from_response = std::strtoul(response.text.c_str(), nullptr, 10);
  993. EXPECT_EQ(errno, 0);
  994. EXPECT_GE(port_from_response, local_port);
  995. EXPECT_LE(port_from_response, local_port + local_port_range);
  996. }
  997. // The tests using the port of the server as a source port for curl fail for windows.
  998. // The reason probably is that Windows allows two sockets to bind to the same port if the full hostname is different.
  999. // In these tests, mongoose binds to http://127.0.0.1:61936, while libcurl binds to a different hostname, but still port 61936.
  1000. // This seems to be okay for Windows, however, these tests expect an error like on Linux and MacOS
  1001. // We therefore, simply skip the tests if Windows is used
  1002. #ifndef _WIN32
  1003. TEST(LocalPortTests, SetLocalPortTestOccupied) {
  1004. Url url{server->GetBaseUrl() + "/local_port.html"};
  1005. Session session;
  1006. session.SetUrl(url);
  1007. session.SetLocalPort(server->GetPort());
  1008. // expected response: request cannot be made as port is already occupied
  1009. Response response = session.Get();
  1010. EXPECT_EQ(ErrorCode::INTERNAL_ERROR, response.error.code);
  1011. }
  1012. TEST(LocalPortTests, SetOptionTestOccupied) {
  1013. Url url{server->GetBaseUrl() + "/local_port.html"};
  1014. Session session;
  1015. session.SetUrl(url);
  1016. session.SetOption(LocalPort(server->GetPort()));
  1017. // expected response: request cannot be made as port is already occupied
  1018. Response response = session.Get();
  1019. EXPECT_EQ(ErrorCode::INTERNAL_ERROR, response.error.code);
  1020. }
  1021. #endif // _WIN32
  1022. TEST(BasicTests, ReserveResponseString) {
  1023. Url url{server->GetBaseUrl() + "/hello.html"};
  1024. Session session;
  1025. session.SetUrl(url);
  1026. session.SetReserveSize(4096);
  1027. Response response = session.Get();
  1028. std::string expected_text{"Hello world!"};
  1029. EXPECT_EQ(expected_text, response.text);
  1030. EXPECT_GE(response.text.capacity(), 4096);
  1031. EXPECT_EQ(url, response.url);
  1032. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1033. EXPECT_EQ(200, response.status_code);
  1034. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1035. }
  1036. TEST(BasicTests, AcceptEncodingTestWithMethodsStringMap) {
  1037. Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
  1038. Session session;
  1039. session.SetUrl(url);
  1040. session.SetAcceptEncoding({{AcceptEncodingMethods::deflate, AcceptEncodingMethods::gzip, AcceptEncodingMethods::zlib}});
  1041. Response response = session.Get();
  1042. std::string expected_text{"deflate, gzip, zlib"};
  1043. EXPECT_EQ(expected_text, response.text);
  1044. EXPECT_EQ(url, response.url);
  1045. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1046. EXPECT_EQ(200, response.status_code);
  1047. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1048. }
  1049. TEST(BasicTests, AcceptEncodingTestWithMethodsStringMapLValue) {
  1050. Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
  1051. Session session;
  1052. session.SetUrl(url);
  1053. AcceptEncoding accept_encoding{{AcceptEncodingMethods::deflate, AcceptEncodingMethods::gzip, AcceptEncodingMethods::zlib}};
  1054. session.SetAcceptEncoding(accept_encoding);
  1055. Response response = session.Get();
  1056. std::string expected_text{"deflate, gzip, zlib"};
  1057. EXPECT_EQ(expected_text, response.text);
  1058. EXPECT_EQ(url, response.url);
  1059. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1060. EXPECT_EQ(200, response.status_code);
  1061. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1062. }
  1063. TEST(BasicTests, AcceptEncodingTestWithCostomizedString) {
  1064. Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
  1065. Session session;
  1066. session.SetUrl(url);
  1067. session.SetAcceptEncoding({{"deflate", "gzip", "zlib"}});
  1068. Response response = session.Get();
  1069. std::string expected_text{"deflate, gzip, zlib"};
  1070. EXPECT_EQ(expected_text, response.text);
  1071. EXPECT_EQ(url, response.url);
  1072. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1073. EXPECT_EQ(200, response.status_code);
  1074. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1075. }
  1076. TEST(BasicTests, AcceptEncodingTestWithCostomizedStringLValue) {
  1077. Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
  1078. Session session;
  1079. session.SetUrl(url);
  1080. AcceptEncoding accept_encoding{{"deflate", "gzip", "zlib"}};
  1081. session.SetAcceptEncoding(accept_encoding);
  1082. Response response = session.Get();
  1083. std::string expected_text{"deflate, gzip, zlib"};
  1084. EXPECT_EQ(expected_text, response.text);
  1085. EXPECT_EQ(url, response.url);
  1086. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1087. EXPECT_EQ(200, response.status_code);
  1088. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1089. }
  1090. TEST(BasicTests, DisableHeaderExpect100ContinueTest) {
  1091. Url url{server->GetBaseUrl() + "/check_expect_100_continue.html"};
  1092. std::string filename{"test_file"};
  1093. std::string content{std::string(1024 * 1024, 'a')};
  1094. std::ofstream test_file;
  1095. test_file.open(filename);
  1096. test_file << content;
  1097. test_file.close();
  1098. Session session{};
  1099. session.SetUrl(url);
  1100. session.SetMultipart({{"file", File{"test_file"}}});
  1101. Response response = session.Post();
  1102. std::string expected_text{""};
  1103. EXPECT_EQ(expected_text, response.text);
  1104. EXPECT_EQ(url, response.url);
  1105. EXPECT_EQ(200, response.status_code);
  1106. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1107. }
  1108. TEST(AsyncRequestsTests, AsyncGetTest) {
  1109. Url url{server->GetBaseUrl() + "/hello.html"};
  1110. std::shared_ptr<Session> session = std::make_shared<Session>();
  1111. session->SetUrl(url);
  1112. cpr::AsyncResponse future = session->GetAsync();
  1113. std::string expected_text{"Hello world!"};
  1114. cpr::Response response = future.get();
  1115. EXPECT_EQ(expected_text, response.text);
  1116. EXPECT_EQ(url, response.url);
  1117. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1118. EXPECT_EQ(200, response.status_code);
  1119. }
  1120. TEST(AsyncRequestsTests, AsyncGetMultipleTest) {
  1121. Url url{server->GetBaseUrl() + "/hello.html"};
  1122. std::vector<AsyncResponse> responses;
  1123. std::vector<std::shared_ptr<Session>> sessions;
  1124. for (size_t i = 0; i < 10; ++i) {
  1125. std::shared_ptr<Session> session = std::make_shared<Session>();
  1126. session->SetUrl(url);
  1127. sessions.emplace_back(session);
  1128. responses.emplace_back(session->GetAsync());
  1129. }
  1130. for (cpr::AsyncResponse& future : responses) {
  1131. std::string expected_text{"Hello world!"};
  1132. cpr::Response response = future.get();
  1133. EXPECT_EQ(expected_text, response.text);
  1134. EXPECT_EQ(url, response.url);
  1135. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1136. EXPECT_EQ(200, response.status_code);
  1137. }
  1138. }
  1139. TEST(AsyncRequestsTests, AsyncGetMultipleTemporarySessionTest) {
  1140. Url url{server->GetBaseUrl() + "/hello.html"};
  1141. std::vector<AsyncResponse> responses;
  1142. for (size_t i = 0; i < 10; ++i) {
  1143. std::shared_ptr<Session> session = std::make_shared<Session>();
  1144. session->SetUrl(url);
  1145. responses.emplace_back(session->GetAsync());
  1146. }
  1147. for (cpr::AsyncResponse& future : responses) {
  1148. std::string expected_text{"Hello world!"};
  1149. cpr::Response response = future.get();
  1150. EXPECT_EQ(expected_text, response.text);
  1151. EXPECT_EQ(url, response.url);
  1152. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1153. EXPECT_EQ(200, response.status_code);
  1154. }
  1155. }
  1156. TEST(AsyncRequestsTests, AsyncGetMultipleReflectTest) {
  1157. Url url{server->GetBaseUrl() + "/hello.html"};
  1158. std::vector<AsyncResponse> responses;
  1159. for (size_t i = 0; i < 100; ++i) {
  1160. std::shared_ptr<Session> session = std::make_shared<Session>();
  1161. session->SetUrl(url);
  1162. session->SetParameters({{"key", std::to_string(i)}});
  1163. responses.emplace_back(session->GetAsync());
  1164. }
  1165. int i = 0;
  1166. for (cpr::AsyncResponse& future : responses) {
  1167. cpr::Response response = future.get();
  1168. std::string expected_text{"Hello world!"};
  1169. Url expected_url{url + "?key=" + std::to_string(i)};
  1170. EXPECT_EQ(expected_text, response.text);
  1171. EXPECT_EQ(expected_url, response.url);
  1172. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1173. EXPECT_EQ(200, response.status_code);
  1174. ++i;
  1175. }
  1176. }
  1177. TEST(AsyncRequestsTests, AsyncWritebackDownloadTest) {
  1178. std::shared_ptr<Session> session = std::make_shared<Session>();
  1179. cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
  1180. session->SetUrl(url);
  1181. session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
  1182. cpr::AsyncResponse future = session->DownloadAsync(cpr::WriteCallback{write_data, 0});
  1183. cpr::Response response = future.get();
  1184. EXPECT_EQ(url, response.url);
  1185. EXPECT_EQ(200, response.status_code);
  1186. EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
  1187. }
  1188. TEST(AsyncRequestsTests, AsyncPostTest) {
  1189. Url url{server->GetBaseUrl() + "/url_post.html"};
  1190. std::shared_ptr<Session> session = std::make_shared<Session>();
  1191. session->SetUrl(url);
  1192. session->SetPayload({{"x", "5"}});
  1193. cpr::AsyncResponse future = session->PostAsync();
  1194. cpr::Response response = future.get();
  1195. std::string expected_text{
  1196. "{\n"
  1197. " \"x\": 5\n"
  1198. "}"};
  1199. EXPECT_EQ(expected_text, response.text);
  1200. EXPECT_EQ(url, response.url);
  1201. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  1202. EXPECT_EQ(201, response.status_code);
  1203. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1204. }
  1205. TEST(AsyncRequestsTests, AsyncPutTest) {
  1206. Url url{server->GetBaseUrl() + "/put.html"};
  1207. std::shared_ptr<Session> session = std::make_shared<Session>();
  1208. session->SetUrl(url);
  1209. session->SetPayload({{"x", "5"}});
  1210. cpr::AsyncResponse future = session->PutAsync();
  1211. cpr::Response response = future.get();
  1212. std::string expected_text{
  1213. "{\n"
  1214. " \"x\": 5\n"
  1215. "}"};
  1216. EXPECT_EQ(expected_text, response.text);
  1217. EXPECT_EQ(url, response.url);
  1218. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  1219. EXPECT_EQ(200, response.status_code);
  1220. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1221. }
  1222. TEST(AsyncRequestsTests, AsyncHeadTest) {
  1223. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1224. std::shared_ptr<Session> session = std::make_shared<Session>();
  1225. session->SetUrl(url);
  1226. cpr::AsyncResponse future = session->HeadAsync();
  1227. cpr::Response response = future.get();
  1228. std::string expected_text{""};
  1229. EXPECT_EQ(expected_text, response.text);
  1230. EXPECT_EQ(url, response.url);
  1231. EXPECT_EQ(200, response.status_code);
  1232. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1233. }
  1234. TEST(AsyncRequestsTests, AsyncDeleteTest) {
  1235. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1236. std::shared_ptr<Session> session = std::make_shared<Session>();
  1237. session->SetUrl(url);
  1238. cpr::AsyncResponse future = session->DeleteAsync();
  1239. cpr::Response response = future.get();
  1240. std::string expected_text{"Header reflect DELETE"};
  1241. EXPECT_EQ(expected_text, response.text);
  1242. EXPECT_EQ(url, response.url);
  1243. EXPECT_EQ(200, response.status_code);
  1244. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1245. }
  1246. TEST(AsyncRequestsTests, AsyncOptionsTest) {
  1247. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1248. std::shared_ptr<Session> session = std::make_shared<Session>();
  1249. session->SetUrl(url);
  1250. cpr::AsyncResponse future = session->OptionsAsync();
  1251. cpr::Response response = future.get();
  1252. std::string expected_text{"Header reflect OPTIONS"};
  1253. EXPECT_EQ(expected_text, response.text);
  1254. EXPECT_EQ(url, response.url);
  1255. EXPECT_EQ(200, response.status_code);
  1256. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1257. }
  1258. TEST(AsyncRequestsTests, AsyncPatchTest) {
  1259. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1260. std::shared_ptr<Session> session = std::make_shared<Session>();
  1261. session->SetUrl(url);
  1262. cpr::AsyncResponse future = session->PatchAsync();
  1263. cpr::Response response = future.get();
  1264. std::string expected_text{"Header reflect PATCH"};
  1265. EXPECT_EQ(expected_text, response.text);
  1266. EXPECT_EQ(url, response.url);
  1267. EXPECT_EQ(200, response.status_code);
  1268. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1269. }
  1270. TEST(CallbackTests, GetCallbackTest) {
  1271. Url url{server->GetBaseUrl() + "/hello.html"};
  1272. std::shared_ptr<Session> session = std::make_shared<Session>();
  1273. session->SetUrl(url);
  1274. auto future = session->GetCallback([](Response r) { return r; });
  1275. std::this_thread::sleep_for(sleep_time);
  1276. cpr::Response response = future.get();
  1277. std::string expected_text{"Hello world!"};
  1278. EXPECT_EQ(expected_text, response.text);
  1279. EXPECT_EQ(url, response.url);
  1280. EXPECT_EQ(200, response.status_code);
  1281. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1282. }
  1283. TEST(CallbackTests, PostCallbackTest) {
  1284. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1285. std::shared_ptr<Session> session = std::make_shared<Session>();
  1286. session->SetUrl(url);
  1287. auto future = session->PostCallback([](Response r) { return r; });
  1288. std::this_thread::sleep_for(sleep_time);
  1289. cpr::Response response = future.get();
  1290. std::string expected_text{"Header reflect POST"};
  1291. EXPECT_EQ(expected_text, response.text);
  1292. EXPECT_EQ(url, response.url);
  1293. EXPECT_EQ(200, response.status_code);
  1294. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1295. }
  1296. TEST(CallbackTests, PutCallbackTest) {
  1297. Url url{server->GetBaseUrl() + "/put.html"};
  1298. std::shared_ptr<Session> session = std::make_shared<Session>();
  1299. session->SetUrl(url);
  1300. session->SetPayload({{"x", "5"}});
  1301. auto future = session->PutCallback([](Response r) { return r; });
  1302. std::this_thread::sleep_for(sleep_time);
  1303. cpr::Response response = future.get();
  1304. std::string expected_text{
  1305. "{\n"
  1306. " \"x\": 5\n"
  1307. "}"};
  1308. EXPECT_EQ(expected_text, response.text);
  1309. EXPECT_EQ(url, response.url);
  1310. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  1311. EXPECT_EQ(200, response.status_code);
  1312. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1313. }
  1314. TEST(CallbackTests, HeadCallbackTest) {
  1315. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1316. std::shared_ptr<Session> session = std::make_shared<Session>();
  1317. session->SetUrl(url);
  1318. auto future = session->HeadCallback([](Response r) { return r; });
  1319. std::this_thread::sleep_for(sleep_time);
  1320. cpr::Response response = future.get();
  1321. std::string expected_text{""};
  1322. EXPECT_EQ(expected_text, response.text);
  1323. EXPECT_EQ(url, response.url);
  1324. EXPECT_EQ(200, response.status_code);
  1325. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1326. }
  1327. TEST(CallbackTests, DeleteCallbackTest) {
  1328. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1329. std::shared_ptr<Session> session = std::make_shared<Session>();
  1330. session->SetUrl(url);
  1331. auto future = session->DeleteCallback([](Response r) { return r; });
  1332. std::this_thread::sleep_for(sleep_time);
  1333. cpr::Response response = future.get();
  1334. std::string expected_text{"Header reflect DELETE"};
  1335. EXPECT_EQ(expected_text, response.text);
  1336. EXPECT_EQ(url, response.url);
  1337. EXPECT_EQ(200, response.status_code);
  1338. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1339. }
  1340. TEST(CallbackTests, OptionsCallbackTest) {
  1341. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1342. std::shared_ptr<Session> session = std::make_shared<Session>();
  1343. session->SetUrl(url);
  1344. auto future = session->OptionsCallback([](Response r) { return r; });
  1345. std::this_thread::sleep_for(sleep_time);
  1346. cpr::Response response = future.get();
  1347. std::string expected_text{"Header reflect OPTIONS"};
  1348. EXPECT_EQ(expected_text, response.text);
  1349. EXPECT_EQ(url, response.url);
  1350. EXPECT_EQ(200, response.status_code);
  1351. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1352. }
  1353. TEST(CallbackTests, PatchCallbackTest) {
  1354. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  1355. std::shared_ptr<Session> session = std::make_shared<Session>();
  1356. session->SetUrl(url);
  1357. auto future = session->PatchCallback([](Response r) { return r; });
  1358. std::this_thread::sleep_for(sleep_time);
  1359. cpr::Response response = future.get();
  1360. std::string expected_text{"Header reflect PATCH"};
  1361. EXPECT_EQ(expected_text, response.text);
  1362. EXPECT_EQ(url, response.url);
  1363. EXPECT_EQ(200, response.status_code);
  1364. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1365. }
  1366. int main(int argc, char** argv) {
  1367. ::testing::InitGoogleTest(&argc, argv);
  1368. ::testing::AddGlobalTestEnvironment(server);
  1369. return RUN_ALL_TESTS();
  1370. }