get_tests.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. #include <gtest/gtest.h>
  2. #include <memory>
  3. #include <string>
  4. #include "cpr/cpr.h"
  5. #include "cpr/cprtypes.h"
  6. #include "cpr/redirect.h"
  7. #include "cpr/session.h"
  8. #include "httpServer.hpp"
  9. using namespace cpr;
  10. static HttpServer* server = new HttpServer();
  11. TEST(BasicTests, HelloWorldTest) {
  12. Url url{server->GetBaseUrl() + "/hello.html"};
  13. Response response = cpr::Get(url);
  14. std::string expected_text{"Hello world!"};
  15. EXPECT_EQ(expected_text, response.text);
  16. EXPECT_EQ(url, response.url);
  17. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  18. EXPECT_EQ(200, response.status_code);
  19. EXPECT_EQ(ErrorCode::OK, response.error.code);
  20. }
  21. TEST(BasicTests, HelloWorldStringViewUrlTest) {
  22. Url url{static_cast<std::string_view>(server->GetBaseUrl() + "/hello.html")};
  23. Response response = cpr::Get(url);
  24. std::string expected_text{"Hello world!"};
  25. EXPECT_EQ(expected_text, response.text);
  26. EXPECT_EQ(url, response.url);
  27. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  28. EXPECT_EQ(200, response.status_code);
  29. EXPECT_EQ(ErrorCode::OK, response.error.code);
  30. }
  31. TEST(BasicTests, HelloWorldNoInterfaceTest) {
  32. Url url{server->GetBaseUrl() + "/hello.html"};
  33. Interface iface{""}; // Do not specify any specific interface
  34. Response response = cpr::Get(url, iface);
  35. std::string expected_text{"Hello world!"};
  36. EXPECT_EQ(expected_text, response.text);
  37. EXPECT_EQ(url, response.url);
  38. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  39. EXPECT_EQ(200, response.status_code);
  40. EXPECT_EQ(ErrorCode::OK, response.error.code);
  41. }
  42. TEST(BasicTests, HelloWorldNoInterfaceStringViewTest) {
  43. Url url{server->GetBaseUrl() + "/hello.html"};
  44. Interface iface{std::string_view{}}; // Do not specify any specific interface
  45. Response response = cpr::Get(url, iface);
  46. std::string expected_text{"Hello world!"};
  47. EXPECT_EQ(expected_text, response.text);
  48. EXPECT_EQ(url, response.url);
  49. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  50. EXPECT_EQ(200, response.status_code);
  51. EXPECT_EQ(ErrorCode::OK, response.error.code);
  52. }
  53. TEST(BasicTests, TimeoutTest) {
  54. Url url{server->GetBaseUrl() + "/hello.html"};
  55. Response response = cpr::Get(url, Timeout{0L});
  56. std::string expected_text{"Hello world!"};
  57. EXPECT_EQ(expected_text, response.text);
  58. EXPECT_EQ(url, response.url);
  59. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  60. EXPECT_EQ(200, response.status_code);
  61. EXPECT_EQ(ErrorCode::OK, response.error.code);
  62. }
  63. TEST(BasicTests, BasicJsonTest) {
  64. Url url{server->GetBaseUrl() + "/basic.json"};
  65. Response response = cpr::Get(url);
  66. std::string expected_text{
  67. "[\n"
  68. " {\n"
  69. " \"first_key\": \"first_value\",\n"
  70. " \"second_key\": \"second_value\"\n"
  71. " }\n"
  72. "]"};
  73. EXPECT_EQ(expected_text, response.text);
  74. EXPECT_EQ(url, response.url);
  75. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  76. EXPECT_EQ(200, response.status_code);
  77. EXPECT_EQ(ErrorCode::OK, response.error.code);
  78. }
  79. TEST(BasicTests, ResourceNotFoundTest) {
  80. Url url{server->GetBaseUrl() + "/error.html"};
  81. Response response = cpr::Get(url);
  82. std::string expected_text{"Not Found"};
  83. EXPECT_EQ(expected_text, response.text);
  84. EXPECT_EQ(url, response.url);
  85. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  86. EXPECT_EQ(404, response.status_code);
  87. EXPECT_EQ(ErrorCode::OK, response.error.code);
  88. }
  89. TEST(BasicTests, BadHostTest) {
  90. Url url{"http://bad_host/"};
  91. Response response = cpr::Get(url);
  92. EXPECT_EQ(std::string{}, response.text);
  93. EXPECT_EQ(url, response.url);
  94. EXPECT_EQ(0, response.status_code);
  95. EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
  96. }
  97. TEST(CookiesTests, BasicCookiesTest) {
  98. Url url{server->GetBaseUrl() + "/basic_cookies.html"};
  99. Response response = cpr::Get(url);
  100. cpr::Cookies res_cookies{response.cookies};
  101. std::string expected_text{"Basic Cookies"};
  102. cpr::Cookies expectedCookies{
  103. {"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  104. {"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  105. };
  106. EXPECT_EQ(expected_text, response.text);
  107. EXPECT_EQ(url, response.url);
  108. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  109. EXPECT_EQ(200, response.status_code);
  110. EXPECT_EQ(ErrorCode::OK, response.error.code);
  111. for (auto cookie = res_cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != res_cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
  112. EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
  113. EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
  114. EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
  115. EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
  116. EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
  117. EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
  118. EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
  119. }
  120. }
  121. TEST(CookiesTests, EmptyCookieTest) {
  122. Url url{server->GetBaseUrl() + "/empty_cookies.html"};
  123. Response response = cpr::Get(url);
  124. cpr::Cookies res_cookies{response.cookies};
  125. std::string expected_text{"Empty Cookies"};
  126. cpr::Cookies expectedCookies{
  127. {"SID", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  128. {"lang", "", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  129. };
  130. EXPECT_EQ(url, response.url);
  131. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  132. EXPECT_EQ(200, response.status_code);
  133. EXPECT_EQ(ErrorCode::OK, response.error.code);
  134. EXPECT_EQ(expected_text, response.text);
  135. for (auto cookie = res_cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != res_cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
  136. EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
  137. EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
  138. EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
  139. EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
  140. EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
  141. EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
  142. EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
  143. }
  144. }
  145. TEST(CookiesTests, ClientSetCookiesTest) {
  146. Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
  147. Cookies cookies{
  148. {"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  149. {"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  150. };
  151. Response response = cpr::Get(url, cookies);
  152. std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
  153. EXPECT_EQ(expected_text, response.text);
  154. EXPECT_EQ(url, response.url);
  155. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  156. EXPECT_EQ(200, response.status_code);
  157. EXPECT_EQ(ErrorCode::OK, response.error.code);
  158. }
  159. TEST(ParameterTests, SingleParameterTest) {
  160. Url url{server->GetBaseUrl() + "/hello.html"};
  161. Parameters parameters{{"key", "value"}};
  162. Response response = cpr::Get(url, parameters);
  163. std::string expected_text{"Hello world!"};
  164. EXPECT_EQ(expected_text, response.text);
  165. EXPECT_EQ(Url{url + "?key=value"}, response.url);
  166. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  167. EXPECT_EQ(200, response.status_code);
  168. EXPECT_EQ(ErrorCode::OK, response.error.code);
  169. }
  170. TEST(ParameterTests, SingleParameterOnlyKeyTest) {
  171. Url url{server->GetBaseUrl() + "/hello.html"};
  172. Parameters parameters{{"key", ""}};
  173. Response response = cpr::Get(url, parameters);
  174. std::string expected_text{"Hello world!"};
  175. EXPECT_EQ(expected_text, response.text);
  176. EXPECT_EQ(Url{url + "?key"}, response.url);
  177. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  178. EXPECT_EQ(200, response.status_code);
  179. }
  180. TEST(ParameterTests, MultipleParametersTest) {
  181. Url url{server->GetBaseUrl() + "/hello.html"};
  182. Response response = cpr::Get(url, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});
  183. std::string expected_text{"Hello world!"};
  184. EXPECT_EQ(expected_text, response.text);
  185. EXPECT_EQ(Url{url + "?key=value&hello=world&test=case"}, response.url);
  186. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  187. EXPECT_EQ(200, response.status_code);
  188. EXPECT_EQ(ErrorCode::OK, response.error.code);
  189. }
  190. TEST(ParameterTests, MultipleDynamicParametersTest) {
  191. Url url{server->GetBaseUrl() + "/hello.html"};
  192. Parameters parameters{{"key", "value"}};
  193. parameters.Add({"hello", "world"});
  194. parameters.Add({"test", "case"});
  195. Response response = cpr::Get(url, parameters);
  196. std::string expected_text{"Hello world!"};
  197. EXPECT_EQ(expected_text, response.text);
  198. EXPECT_EQ(Url{url + "?key=value&hello=world&test=case"}, response.url);
  199. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  200. EXPECT_EQ(200, response.status_code);
  201. EXPECT_EQ(ErrorCode::OK, response.error.code);
  202. }
  203. TEST(BasicAuthenticationTests, BasicAuthenticationSuccessTest) {
  204. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  205. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC});
  206. std::string expected_text{"Header reflect GET"};
  207. EXPECT_EQ(expected_text, response.text);
  208. EXPECT_EQ(url, response.url);
  209. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  210. EXPECT_EQ(200, response.status_code);
  211. EXPECT_EQ(ErrorCode::OK, response.error.code);
  212. }
  213. TEST(BasicAuthenticationTests, BasicBearerSuccessTest) {
  214. Url url{server->GetBaseUrl() + "/bearer_token.html"};
  215. #if CPR_LIBCURL_VERSION_NUM >= 0x073D00
  216. Response response = cpr::Get(url, Bearer{"the_token"});
  217. #else
  218. Response response = cpr::Get(url, Header{{"Authorization", "Bearer the_token"}});
  219. #endif
  220. std::string expected_text{"Header reflect GET"};
  221. EXPECT_EQ(expected_text, response.text);
  222. EXPECT_EQ(url, response.url);
  223. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  224. EXPECT_EQ(200, response.status_code);
  225. EXPECT_EQ(ErrorCode::OK, response.error.code);
  226. }
  227. TEST(BasicAuthenticationTests, BasicDigestSuccessTest) {
  228. Url url{server->GetBaseUrl() + "/digest_auth.html"};
  229. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::DIGEST});
  230. std::string expected_text{"Header reflect GET"};
  231. EXPECT_EQ(expected_text, response.text);
  232. EXPECT_EQ(url, response.url);
  233. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  234. EXPECT_EQ(200, response.status_code);
  235. EXPECT_EQ(ErrorCode::OK, response.error.code);
  236. }
  237. TEST(BasicAthenticationParameterTests, BasicAuthenticationSuccessSingleParameterTest) {
  238. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  239. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"hello", "world"}});
  240. std::string expected_text{"Header reflect GET"};
  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. TEST(BasicAuthenticationParameterTests, BasicAuthenticationSuccessMultipleParametersTest) {
  248. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  249. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});
  250. std::string expected_text{"Header reflect GET"};
  251. EXPECT_EQ(expected_text, response.text);
  252. EXPECT_EQ(Url{url + "?key=value&hello=world&test=case"}, response.url);
  253. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  254. EXPECT_EQ(200, response.status_code);
  255. EXPECT_EQ(ErrorCode::OK, response.error.code);
  256. }
  257. TEST(BasicAuthenticationParameterTests, BasicAuthenticationSuccessSingleParameterReverseTest) {
  258. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  259. Response response = cpr::Get(url, Parameters{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC});
  260. std::string expected_text{"Header reflect GET"};
  261. EXPECT_EQ(expected_text, response.text);
  262. EXPECT_EQ(Url{url + "?hello=world"}, response.url);
  263. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  264. EXPECT_EQ(200, response.status_code);
  265. EXPECT_EQ(ErrorCode::OK, response.error.code);
  266. }
  267. TEST(BasicAuthenticationParameterTests, BasicAuthenticationSuccessMultipleParametersReverseTest) {
  268. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  269. Response response = cpr::Get(url, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}}, Authentication{"user", "password", AuthMode::BASIC});
  270. std::string expected_text{"Header reflect GET"};
  271. EXPECT_EQ(expected_text, response.text);
  272. EXPECT_EQ(Url{url + "?key=value&hello=world&test=case"}, response.url);
  273. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  274. EXPECT_EQ(200, response.status_code);
  275. EXPECT_EQ(ErrorCode::OK, response.error.code);
  276. }
  277. TEST(BasicAuthenticationHeaderTests, BasicAuthenticationSuccessSingleHeaderTest) {
  278. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  279. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{{"hello", "world"}});
  280. std::string expected_text{"Header reflect GET"};
  281. EXPECT_EQ(expected_text, response.text);
  282. EXPECT_EQ(url, response.url);
  283. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  284. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  285. EXPECT_EQ(200, response.status_code);
  286. EXPECT_EQ(ErrorCode::OK, response.error.code);
  287. }
  288. TEST(BasicAuthenticationHeaderTests, BasicAuthenticationSuccessMultipleHeadersTest) {
  289. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  290. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{{"key", "value"}, {"hello", "world"}, {"test", "case"}});
  291. std::string expected_text{"Header reflect GET"};
  292. EXPECT_EQ(expected_text, response.text);
  293. EXPECT_EQ(url, response.url);
  294. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  295. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  296. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  297. EXPECT_EQ(std::string{"case"}, response.header["test"]);
  298. EXPECT_EQ(200, response.status_code);
  299. EXPECT_EQ(ErrorCode::OK, response.error.code);
  300. }
  301. TEST(BasicAuthenticationHeaderTests, BasicAuthenticationSuccessSingleHeaderReverseTest) {
  302. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  303. Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC});
  304. std::string expected_text{"Header reflect GET"};
  305. EXPECT_EQ(expected_text, response.text);
  306. EXPECT_EQ(url, response.url);
  307. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  308. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  309. EXPECT_EQ(200, response.status_code);
  310. EXPECT_EQ(ErrorCode::OK, response.error.code);
  311. }
  312. TEST(BasicAuthenticationHeaderTests, BasicAuthenticationSuccessMultipleHeadersReverseTest) {
  313. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  314. Response response = cpr::Get(url, Header{{"key", "value"}, {"hello", "world"}, {"test", "case"}}, Authentication{"user", "password", AuthMode::BASIC});
  315. std::string expected_text{"Header reflect GET"};
  316. EXPECT_EQ(expected_text, response.text);
  317. EXPECT_EQ(url, response.url);
  318. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  319. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  320. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  321. EXPECT_EQ(std::string{"case"}, response.header["test"]);
  322. EXPECT_EQ(200, response.status_code);
  323. EXPECT_EQ(ErrorCode::OK, response.error.code);
  324. }
  325. TEST(BasicAuthenticationTests, BasicAuthenticationNullFailureTest) {
  326. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  327. Response response = cpr::Get(url);
  328. EXPECT_EQ("Unauthorized", response.text);
  329. EXPECT_EQ(url, response.url);
  330. EXPECT_EQ("text/plain", response.header["content-type"]);
  331. EXPECT_EQ(401, response.status_code);
  332. EXPECT_EQ(ErrorCode::OK, response.error.code);
  333. }
  334. TEST(BasicAuthenticationTests, BasicAuthenticationFailureTest) {
  335. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  336. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC});
  337. EXPECT_EQ("Unauthorized", response.text);
  338. EXPECT_EQ(url, response.url);
  339. EXPECT_EQ("text/plain", response.header["content-type"]);
  340. EXPECT_EQ(401, response.status_code);
  341. EXPECT_EQ(ErrorCode::OK, response.error.code);
  342. }
  343. TEST(BasicAuthenticationParameterTests, BasicAuthenticationFailureSingleParameterTest) {
  344. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  345. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"hello", "world"}});
  346. EXPECT_EQ("Unauthorized", response.text);
  347. EXPECT_EQ(Url{url + "?hello=world"}, response.url);
  348. EXPECT_EQ("text/plain", response.header["content-type"]);
  349. EXPECT_EQ(401, response.status_code);
  350. EXPECT_EQ(ErrorCode::OK, response.error.code);
  351. }
  352. TEST(BasicAuthenticationParameterTests, BasicAuthenticationFailureMultipleParametersTest) {
  353. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  354. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"key", "value"}, {"hello", "world"}, {"test", "case"}});
  355. EXPECT_EQ("Unauthorized", response.text);
  356. EXPECT_EQ(Url{url + "?key=value&hello=world&test=case"}, response.url);
  357. EXPECT_EQ("text/plain", response.header["content-type"]);
  358. EXPECT_EQ(401, response.status_code);
  359. EXPECT_EQ(ErrorCode::OK, response.error.code);
  360. }
  361. TEST(HeaderTests, HeaderJsonTest) {
  362. Url url{server->GetBaseUrl() + "/basic.json"};
  363. Response response = cpr::Get(url, Header{{"content-type", "application/json"}});
  364. std::string expected_text{
  365. "[\n"
  366. " {\n"
  367. " \"first_key\": \"first_value\",\n"
  368. " \"second_key\": \"second_value\"\n"
  369. " }\n"
  370. "]"};
  371. EXPECT_EQ(expected_text, response.text);
  372. EXPECT_EQ(url, response.url);
  373. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  374. EXPECT_EQ(200, response.status_code);
  375. EXPECT_EQ(ErrorCode::OK, response.error.code);
  376. }
  377. TEST(HeaderTests, HeaderReflectNoneTest) {
  378. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  379. Response response = cpr::Get(url);
  380. std::string expected_text{"Header reflect GET"};
  381. EXPECT_EQ(expected_text, response.text);
  382. EXPECT_EQ(url, response.url);
  383. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  384. EXPECT_EQ(std::string{}, response.header["hello"]);
  385. EXPECT_EQ(200, response.status_code);
  386. EXPECT_EQ(ErrorCode::OK, response.error.code);
  387. }
  388. TEST(HeaderTests, HeaderReflectUpdateHeaderAddSessionTest) {
  389. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  390. Session session;
  391. session.SetHeader(Header{{"Header1", "Value1"}});
  392. session.SetUrl(url);
  393. Response response = session.Get();
  394. std::string expected_text{"Header reflect GET"};
  395. EXPECT_EQ(expected_text, response.text);
  396. EXPECT_EQ(url, response.url);
  397. EXPECT_EQ(std::string{"Value1"}, response.header["Header1"]);
  398. EXPECT_EQ(std::string{}, response.header["Header2"]);
  399. EXPECT_EQ(200, response.status_code);
  400. EXPECT_EQ(ErrorCode::OK, response.error.code);
  401. session.UpdateHeader(Header{{"Header2", "Value2"}});
  402. response = session.Get();
  403. EXPECT_EQ(expected_text, response.text);
  404. EXPECT_EQ(url, response.url);
  405. EXPECT_EQ(std::string{"Value1"}, response.header["Header1"]);
  406. EXPECT_EQ(std::string{"Value2"}, response.header["Header2"]);
  407. EXPECT_EQ(200, response.status_code);
  408. EXPECT_EQ(ErrorCode::OK, response.error.code);
  409. }
  410. /**
  411. * Test case for #532
  412. * https://github.com/whoshuu/cpr/issues/532
  413. **/
  414. TEST(HeaderTests, SessionHeaderReflectTest) {
  415. std::unique_ptr<cpr::Session> session(new cpr::Session());
  416. session->SetUrl({server->GetBaseUrl() + "/header_reflect.html"});
  417. session->SetBody("Some Body to post");
  418. session->SetHeader({{"Content-Type", "application/json"}});
  419. cpr::Response response = session->Post();
  420. EXPECT_EQ(200, response.status_code);
  421. EXPECT_EQ(ErrorCode::OK, response.error.code);
  422. EXPECT_EQ(std::string{"Header reflect POST"}, response.text);
  423. EXPECT_EQ(std::string{"application/json"}, response.header["Content-Type"]);
  424. }
  425. TEST(HeaderTests, HeaderReflectUpdateHeaderUpdateSessionTest) {
  426. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  427. Session session;
  428. session.SetHeader(Header{{"Header1", "Value1"}});
  429. session.SetUrl(url);
  430. Response response = session.Get();
  431. std::string expected_text{"Header reflect GET"};
  432. EXPECT_EQ(expected_text, response.text);
  433. EXPECT_EQ(url, response.url);
  434. EXPECT_EQ(std::string{"Value1"}, response.header["Header1"]);
  435. EXPECT_EQ(std::string{}, response.header["Header2"]);
  436. EXPECT_EQ(200, response.status_code);
  437. EXPECT_EQ(ErrorCode::OK, response.error.code);
  438. session.UpdateHeader(Header{{"Header1", "Value2"}});
  439. response = session.Get();
  440. EXPECT_EQ(expected_text, response.text);
  441. EXPECT_EQ(url, response.url);
  442. EXPECT_EQ(std::string{"Value2"}, response.header["Header1"]);
  443. EXPECT_EQ(std::string{}, response.header["Header2"]);
  444. EXPECT_EQ(200, response.status_code);
  445. EXPECT_EQ(ErrorCode::OK, response.error.code);
  446. }
  447. TEST(HeaderTests, HeaderReflectEmptyTest) {
  448. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  449. Response response = cpr::Get(url, Header{});
  450. std::string expected_text{"Header reflect GET"};
  451. EXPECT_EQ(expected_text, response.text);
  452. EXPECT_EQ(url, response.url);
  453. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  454. EXPECT_EQ(std::string{}, response.header["hello"]);
  455. EXPECT_EQ(200, response.status_code);
  456. EXPECT_EQ(ErrorCode::OK, response.error.code);
  457. }
  458. TEST(HeaderTests, HeaderReflectSingleTest) {
  459. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  460. Response response = cpr::Get(url, Header{{"hello", "world"}});
  461. std::string expected_text{"Header reflect GET"};
  462. EXPECT_EQ(expected_text, response.text);
  463. EXPECT_EQ(url, response.url);
  464. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  465. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  466. EXPECT_EQ(200, response.status_code);
  467. EXPECT_EQ(ErrorCode::OK, response.error.code);
  468. }
  469. TEST(HeaderTests, HeaderReflectMultipleTest) {
  470. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  471. Response response = cpr::Get(url, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}});
  472. std::string expected_text{"Header reflect GET"};
  473. EXPECT_EQ(expected_text, response.text);
  474. EXPECT_EQ(url, response.url);
  475. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  476. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  477. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  478. EXPECT_EQ(std::string{"case"}, response.header["test"]);
  479. EXPECT_EQ(200, response.status_code);
  480. EXPECT_EQ(ErrorCode::OK, response.error.code);
  481. }
  482. TEST(HeaderTests, HeaderReflectCaseInsensitiveTest) {
  483. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  484. Response response = cpr::Get(url, Header{{"HeLlO", "wOrLd"}});
  485. std::string expected_text{"Header reflect GET"};
  486. EXPECT_EQ(expected_text, response.text);
  487. EXPECT_EQ(url, response.url);
  488. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  489. EXPECT_EQ(std::string{"wOrLd"}, response.header["hello"]);
  490. EXPECT_EQ(std::string{"wOrLd"}, response.header["HELLO"]);
  491. EXPECT_EQ(std::string{"wOrLd"}, response.header["hElLo"]);
  492. EXPECT_EQ(200, response.status_code);
  493. EXPECT_EQ(ErrorCode::OK, response.error.code);
  494. }
  495. TEST(HeaderTests, SetEmptyHeaderTest) {
  496. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  497. Response response = cpr::Get(url, Header{{"hello", ""}});
  498. std::string expected_text{"Header reflect GET"};
  499. EXPECT_EQ(expected_text, response.text);
  500. EXPECT_EQ(url, response.url);
  501. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  502. EXPECT_EQ(std::string{}, response.header["hello"]);
  503. EXPECT_EQ(200, response.status_code);
  504. EXPECT_EQ(ErrorCode::OK, response.error.code);
  505. }
  506. TEST(ParameterHeaderTests, HeaderReflectNoneParametersTest) {
  507. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  508. Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});
  509. std::string expected_text{"Header reflect GET"};
  510. EXPECT_EQ(expected_text, response.text);
  511. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  512. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  513. EXPECT_EQ(std::string{}, response.header["hello"]);
  514. EXPECT_EQ(200, response.status_code);
  515. EXPECT_EQ(ErrorCode::OK, response.error.code);
  516. }
  517. TEST(ParameterHeaderTests, HeaderReflectEmptyParametersTest) {
  518. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  519. Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});
  520. std::string expected_text{"Header reflect GET"};
  521. EXPECT_EQ(expected_text, response.text);
  522. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  523. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  524. EXPECT_EQ(std::string{}, response.header["hello"]);
  525. EXPECT_EQ(200, response.status_code);
  526. EXPECT_EQ(ErrorCode::OK, response.error.code);
  527. }
  528. TEST(ParameterHeaderTests, HeaderReflectSingleParametersTest) {
  529. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  530. Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});
  531. std::string expected_text{"Header reflect GET"};
  532. EXPECT_EQ(expected_text, response.text);
  533. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  534. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  535. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  536. EXPECT_EQ(200, response.status_code);
  537. EXPECT_EQ(ErrorCode::OK, response.error.code);
  538. }
  539. TEST(ParameterHeaderTests, HeaderReflectMultipleParametersTest) {
  540. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  541. Response response = cpr::Get(url, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});
  542. std::string expected_text{"Header reflect GET"};
  543. EXPECT_EQ(expected_text, response.text);
  544. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  545. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  546. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  547. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  548. EXPECT_EQ(std::string{"case"}, response.header["test"]);
  549. EXPECT_EQ(200, response.status_code);
  550. EXPECT_EQ(ErrorCode::OK, response.error.code);
  551. }
  552. TEST(ParameterHeaderTests, HeaderReflectCaseInsensitiveParametersTest) {
  553. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  554. Response response = cpr::Get(url, Header{{"HeLlO", "wOrLd"}}, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}});
  555. std::string expected_text{"Header reflect GET"};
  556. EXPECT_EQ(expected_text, response.text);
  557. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  558. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  559. EXPECT_EQ(std::string{"wOrLd"}, response.header["hello"]);
  560. EXPECT_EQ(std::string{"wOrLd"}, response.header["HELLO"]);
  561. EXPECT_EQ(std::string{"wOrLd"}, response.header["hElLo"]);
  562. EXPECT_EQ(200, response.status_code);
  563. EXPECT_EQ(ErrorCode::OK, response.error.code);
  564. }
  565. TEST(ParameterHeaderTests, HeaderReflectEmptyParametersReverseTest) {
  566. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  567. Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{});
  568. std::string expected_text{"Header reflect GET"};
  569. EXPECT_EQ(expected_text, response.text);
  570. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  571. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  572. EXPECT_EQ(std::string{}, response.header["hello"]);
  573. EXPECT_EQ(200, response.status_code);
  574. EXPECT_EQ(ErrorCode::OK, response.error.code);
  575. }
  576. TEST(ParameterHeaderTests, HeaderReflectSingleParametersReverseTest) {
  577. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  578. Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"hello", "world"}});
  579. std::string expected_text{"Header reflect GET"};
  580. EXPECT_EQ(expected_text, response.text);
  581. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  582. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  583. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  584. EXPECT_EQ(200, response.status_code);
  585. EXPECT_EQ(ErrorCode::OK, response.error.code);
  586. }
  587. TEST(ParameterHeaderTests, HeaderReflectMultipleParametersReverseTest) {
  588. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  589. Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"hello", "world"}, {"key", "value"}, {"test", "case"}});
  590. std::string expected_text{"Header reflect GET"};
  591. EXPECT_EQ(expected_text, response.text);
  592. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  593. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  594. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  595. EXPECT_EQ(std::string{"value"}, response.header["key"]);
  596. EXPECT_EQ(std::string{"case"}, response.header["test"]);
  597. EXPECT_EQ(200, response.status_code);
  598. EXPECT_EQ(ErrorCode::OK, response.error.code);
  599. }
  600. TEST(ParameterHeaderTests, HeaderReflectCaseInsensitiveParametersReverseTest) {
  601. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  602. Response response = cpr::Get(url, Parameters{{"one", "two"}, {"three", "four"}, {"five", "six"}}, Header{{"HeLlO", "wOrLd"}});
  603. std::string expected_text{"Header reflect GET"};
  604. EXPECT_EQ(expected_text, response.text);
  605. EXPECT_EQ(Url{url + "?one=two&three=four&five=six"}, response.url);
  606. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  607. EXPECT_EQ(std::string{"wOrLd"}, response.header["hello"]);
  608. EXPECT_EQ(std::string{"wOrLd"}, response.header["HELLO"]);
  609. EXPECT_EQ(std::string{"wOrLd"}, response.header["hElLo"]);
  610. EXPECT_EQ(200, response.status_code);
  611. EXPECT_EQ(ErrorCode::OK, response.error.code);
  612. }
  613. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderAATest) {
  614. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  615. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{}, Header{});
  616. std::string expected_text{"Header reflect GET"};
  617. EXPECT_EQ(expected_text, response.text);
  618. EXPECT_EQ(url, response.url);
  619. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  620. EXPECT_EQ(std::string{}, response.header["hello"]);
  621. EXPECT_EQ(200, response.status_code);
  622. EXPECT_EQ(ErrorCode::OK, response.error.code);
  623. }
  624. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderABTest) {
  625. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  626. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{}, Header{});
  627. EXPECT_EQ("Unauthorized", response.text);
  628. EXPECT_EQ(url, response.url);
  629. EXPECT_EQ("text/plain", response.header["content-type"]);
  630. EXPECT_EQ(std::string{}, response.header["hello"]);
  631. EXPECT_EQ(401, response.status_code);
  632. EXPECT_EQ(ErrorCode::OK, response.error.code);
  633. }
  634. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderACTest) {
  635. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  636. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"one", "two"}}, Header{});
  637. std::string expected_text{"Header reflect GET"};
  638. EXPECT_EQ(expected_text, response.text);
  639. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  640. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  641. EXPECT_EQ(std::string{}, response.header["hello"]);
  642. EXPECT_EQ(200, response.status_code);
  643. EXPECT_EQ(ErrorCode::OK, response.error.code);
  644. }
  645. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderADTest) {
  646. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  647. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"one", "two"}}, Header{});
  648. EXPECT_EQ("Unauthorized", response.text);
  649. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  650. EXPECT_EQ("text/plain", response.header["content-type"]);
  651. EXPECT_EQ(std::string{}, response.header["hello"]);
  652. EXPECT_EQ(401, response.status_code);
  653. EXPECT_EQ(ErrorCode::OK, response.error.code);
  654. }
  655. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderAETest) {
  656. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  657. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{}, Header{{"hello", "world"}});
  658. std::string expected_text{"Header reflect GET"};
  659. EXPECT_EQ(expected_text, response.text);
  660. EXPECT_EQ(url, response.url);
  661. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  662. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  663. EXPECT_EQ(200, response.status_code);
  664. EXPECT_EQ(ErrorCode::OK, response.error.code);
  665. }
  666. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderAFTest) {
  667. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  668. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{}, Header{{"hello", "world"}});
  669. EXPECT_EQ("Unauthorized", response.text);
  670. EXPECT_EQ(url, response.url);
  671. EXPECT_EQ("text/plain", response.header["content-type"]);
  672. EXPECT_EQ(std::string{}, response.header["hello"]);
  673. EXPECT_EQ(401, response.status_code);
  674. EXPECT_EQ(ErrorCode::OK, response.error.code);
  675. }
  676. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderAGTest) {
  677. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  678. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"one", "two"}}, Header{{"hello", "world"}});
  679. std::string expected_text{"Header reflect GET"};
  680. EXPECT_EQ(expected_text, response.text);
  681. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  682. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  683. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  684. EXPECT_EQ(200, response.status_code);
  685. EXPECT_EQ(ErrorCode::OK, response.error.code);
  686. }
  687. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderAHTest) {
  688. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  689. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"one", "two"}}, Header{{"hello", "world"}});
  690. EXPECT_EQ("Unauthorized", response.text);
  691. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  692. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  693. EXPECT_EQ(std::string{}, response.header["hello"]);
  694. EXPECT_EQ(401, response.status_code);
  695. EXPECT_EQ(ErrorCode::OK, response.error.code);
  696. }
  697. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBATest) {
  698. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  699. Response response = cpr::Get(url, Parameters{}, Header{}, Authentication{"user", "password", AuthMode::BASIC});
  700. std::string expected_text{"Header reflect GET"};
  701. EXPECT_EQ(expected_text, response.text);
  702. EXPECT_EQ(url, response.url);
  703. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  704. EXPECT_EQ(std::string{}, response.header["hello"]);
  705. EXPECT_EQ(200, response.status_code);
  706. EXPECT_EQ(ErrorCode::OK, response.error.code);
  707. }
  708. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBBTest) {
  709. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  710. Response response = cpr::Get(url, Parameters{}, Header{}, Authentication{"user", "bad_password", AuthMode::BASIC});
  711. EXPECT_EQ("Unauthorized", response.text);
  712. EXPECT_EQ(url, response.url);
  713. EXPECT_EQ("text/plain", response.header["content-type"]);
  714. EXPECT_EQ(std::string{}, response.header["hello"]);
  715. EXPECT_EQ(401, response.status_code);
  716. EXPECT_EQ(ErrorCode::OK, response.error.code);
  717. }
  718. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBCTest) {
  719. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  720. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{}, Authentication{"user", "password", AuthMode::BASIC});
  721. std::string expected_text{"Header reflect GET"};
  722. EXPECT_EQ(expected_text, response.text);
  723. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  724. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  725. EXPECT_EQ(std::string{}, response.header["hello"]);
  726. EXPECT_EQ(200, response.status_code);
  727. EXPECT_EQ(ErrorCode::OK, response.error.code);
  728. }
  729. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBDTest) {
  730. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  731. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{}, Authentication{"user", "bad_password", AuthMode::BASIC});
  732. EXPECT_EQ("Unauthorized", response.text);
  733. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  734. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  735. EXPECT_EQ(std::string{}, response.header["hello"]);
  736. EXPECT_EQ(401, response.status_code);
  737. EXPECT_EQ(ErrorCode::OK, response.error.code);
  738. }
  739. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBETest) {
  740. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  741. Response response = cpr::Get(url, Parameters{}, Header{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC});
  742. std::string expected_text{"Header reflect GET"};
  743. EXPECT_EQ(expected_text, response.text);
  744. EXPECT_EQ(url, response.url);
  745. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  746. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  747. EXPECT_EQ(200, response.status_code);
  748. EXPECT_EQ(ErrorCode::OK, response.error.code);
  749. }
  750. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBFTest) {
  751. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  752. Response response = cpr::Get(url, Parameters{}, Header{{"hello", "world"}}, Authentication{"user", "bad_password", AuthMode::BASIC});
  753. EXPECT_EQ("Unauthorized", response.text);
  754. EXPECT_EQ(url, response.url);
  755. EXPECT_EQ("text/plain", response.header["content-type"]);
  756. EXPECT_EQ(std::string{}, response.header["hello"]);
  757. EXPECT_EQ(401, response.status_code);
  758. EXPECT_EQ(ErrorCode::OK, response.error.code);
  759. }
  760. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBGTest) {
  761. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  762. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC});
  763. std::string expected_text{"Header reflect GET"};
  764. EXPECT_EQ(expected_text, response.text);
  765. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  766. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  767. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  768. EXPECT_EQ(200, response.status_code);
  769. EXPECT_EQ(ErrorCode::OK, response.error.code);
  770. }
  771. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderBHTest) {
  772. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  773. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Header{{"hello", "world"}}, Authentication{"user", "bad_password", AuthMode::BASIC});
  774. EXPECT_EQ("Unauthorized", response.text);
  775. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  776. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  777. EXPECT_EQ(std::string{}, response.header["hello"]);
  778. EXPECT_EQ(401, response.status_code);
  779. EXPECT_EQ(ErrorCode::OK, response.error.code);
  780. }
  781. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCATest) {
  782. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  783. Response response = cpr::Get(url, Header{}, Authentication{"user", "password", AuthMode::BASIC}, Parameters{});
  784. std::string expected_text{"Header reflect GET"};
  785. EXPECT_EQ(expected_text, response.text);
  786. EXPECT_EQ(url, response.url);
  787. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  788. EXPECT_EQ(std::string{}, response.header["hello"]);
  789. EXPECT_EQ(200, response.status_code);
  790. EXPECT_EQ(ErrorCode::OK, response.error.code);
  791. }
  792. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCBTest) {
  793. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  794. Response response = cpr::Get(url, Header{}, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{});
  795. EXPECT_EQ("Unauthorized", response.text);
  796. EXPECT_EQ(url, response.url);
  797. EXPECT_EQ("text/plain", response.header["content-type"]);
  798. EXPECT_EQ(std::string{}, response.header["hello"]);
  799. EXPECT_EQ(401, response.status_code);
  800. EXPECT_EQ(ErrorCode::OK, response.error.code);
  801. }
  802. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCCTest) {
  803. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  804. Response response = cpr::Get(url, Header{}, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"one", "two"}});
  805. std::string expected_text{"Header reflect GET"};
  806. EXPECT_EQ(expected_text, response.text);
  807. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  808. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  809. EXPECT_EQ(std::string{}, response.header["hello"]);
  810. EXPECT_EQ(200, response.status_code);
  811. EXPECT_EQ(ErrorCode::OK, response.error.code);
  812. }
  813. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCDTest) {
  814. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  815. Response response = cpr::Get(url, Header{}, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"one", "two"}});
  816. EXPECT_EQ("Unauthorized", response.text);
  817. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  818. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  819. EXPECT_EQ(std::string{}, response.header["hello"]);
  820. EXPECT_EQ(401, response.status_code);
  821. EXPECT_EQ(ErrorCode::OK, response.error.code);
  822. }
  823. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCETest) {
  824. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  825. Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC}, Parameters{});
  826. std::string expected_text{"Header reflect GET"};
  827. EXPECT_EQ(expected_text, response.text);
  828. EXPECT_EQ(url, response.url);
  829. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  830. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  831. EXPECT_EQ(200, response.status_code);
  832. EXPECT_EQ(ErrorCode::OK, response.error.code);
  833. }
  834. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCFTest) {
  835. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  836. Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{});
  837. EXPECT_EQ("Unauthorized", response.text);
  838. EXPECT_EQ(url, response.url);
  839. EXPECT_EQ("text/plain", response.header["content-type"]);
  840. EXPECT_EQ(std::string{}, response.header["hello"]);
  841. EXPECT_EQ(401, response.status_code);
  842. EXPECT_EQ(ErrorCode::OK, response.error.code);
  843. }
  844. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCGTest) {
  845. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  846. Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "password", AuthMode::BASIC}, Parameters{{"one", "two"}});
  847. std::string expected_text{"Header reflect GET"};
  848. EXPECT_EQ(expected_text, response.text);
  849. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  850. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  851. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  852. EXPECT_EQ(200, response.status_code);
  853. EXPECT_EQ(ErrorCode::OK, response.error.code);
  854. }
  855. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderCHTest) {
  856. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  857. Response response = cpr::Get(url, Header{{"hello", "world"}}, Authentication{"user", "bad_password", AuthMode::BASIC}, Parameters{{"one", "two"}});
  858. EXPECT_EQ("Unauthorized", response.text);
  859. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  860. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  861. EXPECT_EQ(std::string{}, response.header["hello"]);
  862. EXPECT_EQ(401, response.status_code);
  863. EXPECT_EQ(ErrorCode::OK, response.error.code);
  864. }
  865. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDATest) {
  866. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  867. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{}, Parameters{});
  868. std::string expected_text{"Header reflect GET"};
  869. EXPECT_EQ(expected_text, response.text);
  870. EXPECT_EQ(url, response.url);
  871. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  872. EXPECT_EQ(std::string{}, response.header["hello"]);
  873. EXPECT_EQ(200, response.status_code);
  874. EXPECT_EQ(ErrorCode::OK, response.error.code);
  875. }
  876. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDBTest) {
  877. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  878. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{}, Parameters{});
  879. EXPECT_EQ("Unauthorized", response.text);
  880. EXPECT_EQ(url, response.url);
  881. EXPECT_EQ("text/plain", response.header["content-type"]);
  882. EXPECT_EQ(std::string{}, response.header["hello"]);
  883. EXPECT_EQ(401, response.status_code);
  884. EXPECT_EQ(ErrorCode::OK, response.error.code);
  885. }
  886. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDCTest) {
  887. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  888. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{}, Parameters{{"one", "two"}});
  889. std::string expected_text{"Header reflect GET"};
  890. EXPECT_EQ(expected_text, response.text);
  891. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  892. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  893. EXPECT_EQ(std::string{}, response.header["hello"]);
  894. EXPECT_EQ(200, response.status_code);
  895. EXPECT_EQ(ErrorCode::OK, response.error.code);
  896. }
  897. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDDTest) {
  898. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  899. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{}, Parameters{{"one", "two"}});
  900. EXPECT_EQ("Unauthorized", response.text);
  901. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  902. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  903. EXPECT_EQ(std::string{}, response.header["hello"]);
  904. EXPECT_EQ(401, response.status_code);
  905. EXPECT_EQ(ErrorCode::OK, response.error.code);
  906. }
  907. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDETest) {
  908. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  909. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{{"hello", "world"}}, Parameters{});
  910. std::string expected_text{"Header reflect GET"};
  911. EXPECT_EQ(expected_text, response.text);
  912. EXPECT_EQ(url, response.url);
  913. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  914. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  915. EXPECT_EQ(200, response.status_code);
  916. EXPECT_EQ(ErrorCode::OK, response.error.code);
  917. }
  918. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDFTest) {
  919. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  920. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{{"hello", "world"}}, Parameters{});
  921. EXPECT_EQ("Unauthorized", response.text);
  922. EXPECT_EQ(url, response.url);
  923. EXPECT_EQ("text/plain", response.header["content-type"]);
  924. EXPECT_EQ(std::string{}, response.header["hello"]);
  925. EXPECT_EQ(401, response.status_code);
  926. EXPECT_EQ(ErrorCode::OK, response.error.code);
  927. }
  928. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDGTest) {
  929. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  930. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{{"hello", "world"}}, Parameters{{"one", "two"}});
  931. std::string expected_text{"Header reflect GET"};
  932. EXPECT_EQ(expected_text, response.text);
  933. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  934. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  935. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  936. EXPECT_EQ(200, response.status_code);
  937. EXPECT_EQ(ErrorCode::OK, response.error.code);
  938. }
  939. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderDHTest) {
  940. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  941. Response response = cpr::Get(url, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{{"hello", "world"}}, Parameters{{"one", "two"}});
  942. EXPECT_EQ("Unauthorized", response.text);
  943. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  944. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  945. EXPECT_EQ(std::string{}, response.header["hello"]);
  946. EXPECT_EQ(401, response.status_code);
  947. EXPECT_EQ(ErrorCode::OK, response.error.code);
  948. }
  949. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEATest) {
  950. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  951. Response response = cpr::Get(url, Header{}, Parameters{}, Authentication{"user", "password", AuthMode::BASIC});
  952. std::string expected_text{"Header reflect GET"};
  953. EXPECT_EQ(expected_text, response.text);
  954. EXPECT_EQ(url, response.url);
  955. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  956. EXPECT_EQ(std::string{}, response.header["hello"]);
  957. EXPECT_EQ(200, response.status_code);
  958. EXPECT_EQ(ErrorCode::OK, response.error.code);
  959. }
  960. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEBTest) {
  961. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  962. Response response = cpr::Get(url, Header{}, Parameters{}, Authentication{"user", "bad_password", AuthMode::BASIC});
  963. EXPECT_EQ("Unauthorized", response.text);
  964. EXPECT_EQ(url, response.url);
  965. EXPECT_EQ("text/plain", response.header["content-type"]);
  966. EXPECT_EQ(std::string{}, response.header["hello"]);
  967. EXPECT_EQ(401, response.status_code);
  968. EXPECT_EQ(ErrorCode::OK, response.error.code);
  969. }
  970. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderECTest) {
  971. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  972. Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}}, Authentication{"user", "password", AuthMode::BASIC});
  973. std::string expected_text{"Header reflect GET"};
  974. EXPECT_EQ(expected_text, response.text);
  975. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  976. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  977. EXPECT_EQ(std::string{}, response.header["hello"]);
  978. EXPECT_EQ(200, response.status_code);
  979. EXPECT_EQ(ErrorCode::OK, response.error.code);
  980. }
  981. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEDTest) {
  982. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  983. Response response = cpr::Get(url, Header{}, Parameters{{"one", "two"}}, Authentication{"user", "bad_password", AuthMode::BASIC});
  984. EXPECT_EQ("Unauthorized", response.text);
  985. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  986. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  987. EXPECT_EQ(std::string{}, response.header["hello"]);
  988. EXPECT_EQ(401, response.status_code);
  989. EXPECT_EQ(ErrorCode::OK, response.error.code);
  990. }
  991. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEETest) {
  992. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  993. Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{}, Authentication{"user", "password", AuthMode::BASIC});
  994. std::string expected_text{"Header reflect GET"};
  995. EXPECT_EQ(expected_text, response.text);
  996. EXPECT_EQ(url, response.url);
  997. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  998. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  999. EXPECT_EQ(200, response.status_code);
  1000. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1001. }
  1002. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEFTest) {
  1003. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1004. Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{}, Authentication{"user", "bad_password", AuthMode::BASIC});
  1005. EXPECT_EQ("Unauthorized", response.text);
  1006. EXPECT_EQ(url, response.url);
  1007. EXPECT_EQ("text/plain", response.header["content-type"]);
  1008. EXPECT_EQ(std::string{}, response.header["hello"]);
  1009. EXPECT_EQ(401, response.status_code);
  1010. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1011. }
  1012. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEGTest) {
  1013. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1014. Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}}, Authentication{"user", "password", AuthMode::BASIC});
  1015. std::string expected_text{"Header reflect GET"};
  1016. EXPECT_EQ(expected_text, response.text);
  1017. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1018. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1019. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  1020. EXPECT_EQ(200, response.status_code);
  1021. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1022. }
  1023. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderEHTest) {
  1024. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1025. Response response = cpr::Get(url, Header{{"hello", "world"}}, Parameters{{"one", "two"}}, Authentication{"user", "bad_password", AuthMode::BASIC});
  1026. EXPECT_EQ("Unauthorized", response.text);
  1027. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1028. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  1029. EXPECT_EQ(std::string{}, response.header["hello"]);
  1030. EXPECT_EQ(401, response.status_code);
  1031. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1032. }
  1033. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFATest) {
  1034. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1035. Response response = cpr::Get(url, Parameters{}, Authentication{"user", "password", AuthMode::BASIC}, Header{});
  1036. std::string expected_text{"Header reflect GET"};
  1037. EXPECT_EQ(expected_text, response.text);
  1038. EXPECT_EQ(url, response.url);
  1039. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1040. EXPECT_EQ(std::string{}, response.header["hello"]);
  1041. EXPECT_EQ(200, response.status_code);
  1042. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1043. }
  1044. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFBTest) {
  1045. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1046. Response response = cpr::Get(url, Parameters{}, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{});
  1047. EXPECT_EQ("Unauthorized", response.text);
  1048. EXPECT_EQ(url, response.url);
  1049. EXPECT_EQ("text/plain", response.header["content-type"]);
  1050. EXPECT_EQ(std::string{}, response.header["hello"]);
  1051. EXPECT_EQ(401, response.status_code);
  1052. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1053. }
  1054. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFCTest) {
  1055. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1056. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "password", AuthMode::BASIC}, Header{});
  1057. std::string expected_text{"Header reflect GET"};
  1058. EXPECT_EQ(expected_text, response.text);
  1059. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1060. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1061. EXPECT_EQ(std::string{}, response.header["hello"]);
  1062. EXPECT_EQ(200, response.status_code);
  1063. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1064. }
  1065. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFDTest) {
  1066. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1067. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{});
  1068. EXPECT_EQ("Unauthorized", response.text);
  1069. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1070. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  1071. EXPECT_EQ(std::string{}, response.header["hello"]);
  1072. EXPECT_EQ(401, response.status_code);
  1073. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1074. }
  1075. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFETest) {
  1076. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1077. Response response = cpr::Get(url, Parameters{}, Authentication{"user", "password", AuthMode::BASIC}, Header{{"hello", "world"}});
  1078. std::string expected_text{"Header reflect GET"};
  1079. EXPECT_EQ(expected_text, response.text);
  1080. EXPECT_EQ(url, response.url);
  1081. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1082. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  1083. EXPECT_EQ(200, response.status_code);
  1084. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1085. }
  1086. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFFTest) {
  1087. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1088. Response response = cpr::Get(url, Parameters{}, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{{"hello", "world"}});
  1089. EXPECT_EQ("Unauthorized", response.text);
  1090. EXPECT_EQ(url, response.url);
  1091. EXPECT_EQ("text/plain", response.header["content-type"]);
  1092. EXPECT_EQ(std::string{}, response.header["hello"]);
  1093. EXPECT_EQ(401, response.status_code);
  1094. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1095. }
  1096. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFGTest) {
  1097. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1098. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "password", AuthMode::BASIC}, Header{{"hello", "world"}});
  1099. std::string expected_text{"Header reflect GET"};
  1100. EXPECT_EQ(expected_text, response.text);
  1101. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1102. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1103. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  1104. EXPECT_EQ(200, response.status_code);
  1105. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1106. }
  1107. TEST(BasicAuthenticationParameterHeaderTests, BasicAuthenticationParameterHeaderFHTest) {
  1108. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  1109. Response response = cpr::Get(url, Parameters{{"one", "two"}}, Authentication{"user", "bad_password", AuthMode::BASIC}, Header{{"hello", "world"}});
  1110. EXPECT_EQ("Unauthorized", response.text);
  1111. EXPECT_EQ(Url{url + "?one=two"}, response.url);
  1112. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  1113. EXPECT_EQ(std::string{}, response.header["hello"]);
  1114. EXPECT_EQ(401, response.status_code);
  1115. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1116. }
  1117. TEST(GetRedirectTests, RedirectTest) {
  1118. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  1119. Response response = cpr::Get(url, Redirect(false));
  1120. std::string expected_text{"Moved Temporarily"};
  1121. EXPECT_EQ(expected_text, response.text);
  1122. EXPECT_EQ(url, response.url);
  1123. EXPECT_EQ(std::string{}, response.header["content-type"]);
  1124. EXPECT_EQ(302, response.status_code);
  1125. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1126. }
  1127. TEST(GetRedirectTests, ZeroMaxRedirectsSuccessTest) {
  1128. Url url{server->GetBaseUrl() + "/hello.html"};
  1129. Response response = cpr::Get(url, Redirect(0L));
  1130. std::string expected_text{"Hello world!"};
  1131. EXPECT_EQ(expected_text, response.text);
  1132. EXPECT_EQ(url, response.url);
  1133. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1134. EXPECT_EQ(200, response.status_code);
  1135. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1136. }
  1137. TEST(GetRedirectTests, ZeroMaxRedirectsFailureTest) {
  1138. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  1139. Response response = cpr::Get(url, Redirect(0L));
  1140. EXPECT_EQ(std::string{}, response.text);
  1141. EXPECT_EQ(url, response.url);
  1142. EXPECT_EQ(std::string{}, response.header["content-type"]);
  1143. EXPECT_EQ(301, response.status_code);
  1144. EXPECT_EQ(ErrorCode::TOO_MANY_REDIRECTS, response.error.code);
  1145. }
  1146. TEST(GetRedirectTests, BasicAuthenticationRedirectSuccessTest) {
  1147. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  1148. Response response = cpr::Get(url, Authentication{"user", "password", AuthMode::BASIC}, Header{{"RedirectLocation", "basic_auth.html"}}, Redirect(true, true));
  1149. std::string expected_text{"Header reflect GET"};
  1150. EXPECT_EQ(expected_text, response.text);
  1151. std::string resultUrl = "http://user:[email protected]:" + std::to_string(server->GetPort()) + "/basic_auth.html";
  1152. EXPECT_EQ(response.url, resultUrl);
  1153. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1154. EXPECT_EQ(200, response.status_code);
  1155. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1156. }
  1157. TEST(BasicTests, RequestBodyTest) {
  1158. Url url{server->GetBaseUrl() + "/body_get.html"};
  1159. Body body{"message=abc123"};
  1160. Response response = cpr::Get(url, body);
  1161. std::string expected_text{"abc123"};
  1162. EXPECT_EQ(expected_text, response.text);
  1163. EXPECT_EQ(url, response.url);
  1164. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1165. EXPECT_EQ(200, response.status_code);
  1166. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1167. }
  1168. TEST(BasicTests, RequestBodyStringViewTest) {
  1169. Url url{server->GetBaseUrl() + "/body_get.html"};
  1170. Body body{static_cast<std::string_view>("message=abc123")};
  1171. Response response = cpr::Get(url, body);
  1172. std::string expected_text{"abc123"};
  1173. EXPECT_EQ(expected_text, response.text);
  1174. EXPECT_EQ(url, response.url);
  1175. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1176. EXPECT_EQ(200, response.status_code);
  1177. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1178. }
  1179. TEST(LimitRateTests, HelloWorldTest) {
  1180. Url url{server->GetBaseUrl() + "/hello.html"};
  1181. Response response = cpr::Get(url, LimitRate(1024, 1024));
  1182. std::string expected_text{"Hello world!"};
  1183. EXPECT_EQ(expected_text, response.text);
  1184. EXPECT_EQ(url, response.url);
  1185. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  1186. EXPECT_EQ(200, response.status_code);
  1187. EXPECT_EQ(ErrorCode::OK, response.error.code);
  1188. }
  1189. int main(int argc, char** argv) {
  1190. ::testing::InitGoogleTest(&argc, argv);
  1191. ::testing::AddGlobalTestEnvironment(server);
  1192. return RUN_ALL_TESTS();
  1193. }