1
0

post_tests.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include <gtest/gtest.h>
  2. #include <array>
  3. #include <cstdio>
  4. #include <fstream>
  5. #include <string>
  6. #include <cpr/cpr.h>
  7. #include <cpr/multipart.h>
  8. #include "httpServer.hpp"
  9. using namespace cpr;
  10. static HttpServer* server = new HttpServer();
  11. TEST(UrlEncodedPostTests, UrlPostSingleTest) {
  12. Url url{server->GetBaseUrl() + "/url_post.html"};
  13. Response response = cpr::Post(url, Payload{{"x", "5"}});
  14. std::string expected_text{
  15. "{\n"
  16. " \"x\": 5\n"
  17. "}"};
  18. EXPECT_EQ(expected_text, response.text);
  19. EXPECT_EQ(url, response.url);
  20. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  21. EXPECT_EQ(201, response.status_code);
  22. EXPECT_EQ(ErrorCode::OK, response.error.code);
  23. }
  24. TEST(UrlEncodedPostTests, UrlPostAddPayloadPair) {
  25. Url url{server->GetBaseUrl() + "/url_post.html"};
  26. Payload payload{{"x", "1"}};
  27. payload.Add({"y", "2"});
  28. Response response = cpr::Post(url, payload);
  29. std::string expected_text{
  30. "{\n"
  31. " \"x\": 1,\n"
  32. " \"y\": 2,\n"
  33. " \"sum\": 3\n"
  34. "}"};
  35. EXPECT_EQ(expected_text, response.text);
  36. EXPECT_EQ(url, response.url);
  37. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  38. EXPECT_EQ(201, response.status_code);
  39. }
  40. TEST(UrlEncodedPostTests, UrlPostPayloadIteratorTest) {
  41. Url url{server->GetBaseUrl() + "/url_post.html"};
  42. std::vector<Pair> payloadData;
  43. payloadData.emplace_back("x", "1");
  44. payloadData.emplace_back("y", "2");
  45. Response response = cpr::Post(url, Payload(payloadData.begin(), payloadData.end()));
  46. std::string expected_text{
  47. "{\n"
  48. " \"x\": 1,\n"
  49. " \"y\": 2,\n"
  50. " \"sum\": 3\n"
  51. "}"};
  52. EXPECT_EQ(expected_text, response.text);
  53. EXPECT_EQ(url, response.url);
  54. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  55. EXPECT_EQ(201, response.status_code);
  56. }
  57. TEST(UrlEncodedPostTests, UrlPostEncodeTest) {
  58. Url url{server->GetBaseUrl() + "/url_post.html"};
  59. Response response = cpr::Post(url, Payload{{"x", "hello world!!~"}});
  60. std::string expected_text{
  61. "{\n"
  62. " \"x\": hello world!!~\n"
  63. "}"};
  64. EXPECT_EQ(expected_text, response.text);
  65. EXPECT_EQ(url, response.url);
  66. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  67. EXPECT_EQ(201, response.status_code);
  68. EXPECT_EQ(ErrorCode::OK, response.error.code);
  69. }
  70. TEST(UrlEncodedPostTests, UrlPostEncodeNoCopyTest) {
  71. Url url{server->GetBaseUrl() + "/url_post.html"};
  72. Payload payload{{"x", "hello world!!~"}};
  73. // payload lives through the lifetime of Post, so it doesn't need to be copied
  74. Response response = cpr::Post(url, payload);
  75. std::string expected_text{
  76. "{\n"
  77. " \"x\": hello world!!~\n"
  78. "}"};
  79. EXPECT_EQ(expected_text, response.text);
  80. EXPECT_EQ(url, response.url);
  81. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  82. EXPECT_EQ(201, response.status_code);
  83. EXPECT_EQ(ErrorCode::OK, response.error.code);
  84. }
  85. TEST(UrlEncodedPostTests, UrlPostManyTest) {
  86. Url url{server->GetBaseUrl() + "/url_post.html"};
  87. Response response = cpr::Post(url, Payload{{"x", "5"}, {"y", "13"}});
  88. std::string expected_text{
  89. "{\n"
  90. " \"x\": 5,\n"
  91. " \"y\": 13,\n"
  92. " \"sum\": 18\n"
  93. "}"};
  94. EXPECT_EQ(expected_text, response.text);
  95. EXPECT_EQ(url, response.url);
  96. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  97. EXPECT_EQ(201, response.status_code);
  98. EXPECT_EQ(ErrorCode::OK, response.error.code);
  99. }
  100. TEST(UrlEncodedPostTests, UrlPostBadHostTest) {
  101. Url url{"http://bad_host/"};
  102. Response response = cpr::Post(url, Payload{{"hello", "world"}});
  103. EXPECT_EQ(std::string{}, response.text);
  104. EXPECT_EQ(url, response.url);
  105. EXPECT_EQ(std::string{}, response.header["content-type"]);
  106. EXPECT_EQ(0, response.status_code);
  107. EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
  108. }
  109. TEST(UrlEncodedPostTests, FormPostSingleTest) {
  110. Url url{server->GetBaseUrl() + "/form_post.html"};
  111. Response response = cpr::Post(url, Multipart{{"x", 5}});
  112. std::string expected_text{
  113. "{\n"
  114. " \"x\": \"5\"\n"
  115. "}"};
  116. EXPECT_EQ(expected_text, response.text);
  117. EXPECT_EQ(url, response.url);
  118. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  119. EXPECT_EQ(201, response.status_code);
  120. EXPECT_EQ(ErrorCode::OK, response.error.code);
  121. }
  122. TEST(UrlEncodedPostTests, FormPostFileTest) {
  123. std::string filename{"test_file"};
  124. std::string content{"hello world"};
  125. std::ofstream test_file;
  126. test_file.open(filename);
  127. test_file << content;
  128. test_file.close();
  129. Url url{server->GetBaseUrl() + "/form_post.html"};
  130. Response response = cpr::Post(url, Multipart{{"x", File{filename}}});
  131. std::string expected_text{
  132. "{\n"
  133. " \"x\": \"test_file=" +
  134. content +
  135. "\"\n"
  136. "}"};
  137. std::remove(filename.c_str());
  138. EXPECT_EQ(expected_text, response.text);
  139. EXPECT_EQ(url, response.url);
  140. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  141. EXPECT_EQ(201, response.status_code);
  142. EXPECT_EQ(ErrorCode::OK, response.error.code);
  143. }
  144. TEST(UrlEncodedPostTests, FormPostMultipleFilesTestLvalue) {
  145. Url url{server->GetBaseUrl() + "/form_post.html"};
  146. std::string filename1{"file1"};
  147. std::string content1{"apple"};
  148. std::ofstream file1;
  149. file1.open(filename1);
  150. file1 << content1;
  151. file1.close();
  152. std::string filename2{"file2"};
  153. std::string content2{"banana"};
  154. std::ofstream file2;
  155. file2.open(filename2);
  156. file2 << content2;
  157. file2.close();
  158. File singleFile{"file1"};
  159. File singleFileWithOverridedFilename{"file1", "applefile"};
  160. Files multipleFiles{"file1", "file2"};
  161. Files multipleFilesWithOverridedFilename{
  162. File{"file1", "applefile"},
  163. File{"file2", "bananafile"},
  164. };
  165. {
  166. Response response = cpr::Post(url, Multipart{{"files", singleFile}});
  167. std::string expected_text{
  168. "{\n"
  169. " \"files\": \"file1=" +
  170. content1 + "\"\n}"};
  171. EXPECT_EQ(expected_text, response.text);
  172. EXPECT_EQ(url, response.url);
  173. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  174. EXPECT_EQ(201, response.status_code);
  175. EXPECT_EQ(ErrorCode::OK, response.error.code);
  176. }
  177. {
  178. Response response = cpr::Post(url, Multipart{{"files", singleFileWithOverridedFilename}});
  179. std::string expected_text{
  180. "{\n"
  181. " \"files\": \"applefile=" +
  182. content1 + "\"\n}"};
  183. EXPECT_EQ(expected_text, response.text);
  184. EXPECT_EQ(url, response.url);
  185. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  186. EXPECT_EQ(201, response.status_code);
  187. EXPECT_EQ(ErrorCode::OK, response.error.code);
  188. }
  189. {
  190. Response response = cpr::Post(url, Multipart{{"files", multipleFiles}});
  191. std::string expected_text{
  192. "{\n"
  193. " \"files\": \"file1=" +
  194. content1 +
  195. "\",\n"
  196. " \"files\": \"file2=" +
  197. content2 + "\"\n}"};
  198. EXPECT_EQ(expected_text, response.text);
  199. EXPECT_EQ(url, response.url);
  200. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  201. EXPECT_EQ(201, response.status_code);
  202. EXPECT_EQ(ErrorCode::OK, response.error.code);
  203. }
  204. {
  205. Response response = cpr::Post(url, Multipart{{"files", multipleFilesWithOverridedFilename}});
  206. std::string expected_text{
  207. "{\n"
  208. " \"files\": \"applefile=" +
  209. content1 +
  210. "\",\n"
  211. " \"files\": \"bananafile=" +
  212. content2 + "\"\n}"};
  213. EXPECT_EQ(expected_text, response.text);
  214. EXPECT_EQ(url, response.url);
  215. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  216. EXPECT_EQ(201, response.status_code);
  217. EXPECT_EQ(ErrorCode::OK, response.error.code);
  218. }
  219. std::remove(filename1.c_str());
  220. std::remove(filename2.c_str());
  221. }
  222. TEST(UrlEncodedPostTests, FormPostMultipleFilesTestRvalue) {
  223. Url url{server->GetBaseUrl() + "/form_post.html"};
  224. std::string filename1{"file1"};
  225. std::string content1{"apple"};
  226. std::ofstream file1;
  227. file1.open(filename1);
  228. file1 << content1;
  229. file1.close();
  230. std::string filename2{"file2"};
  231. std::string content2{"banana"};
  232. std::ofstream file2;
  233. file2.open(filename2);
  234. file2 << content2;
  235. file2.close();
  236. {
  237. Response response = cpr::Post(url, Multipart{{"files", File{"file1"}}});
  238. std::string expected_text{
  239. "{\n"
  240. " \"files\": \"file1=" +
  241. content1 + "\"\n}"};
  242. EXPECT_EQ(expected_text, response.text);
  243. EXPECT_EQ(url, response.url);
  244. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  245. EXPECT_EQ(201, response.status_code);
  246. EXPECT_EQ(ErrorCode::OK, response.error.code);
  247. }
  248. {
  249. Response response = cpr::Post(url, Multipart{{"files", File{"file1", "applefile"}}});
  250. std::string expected_text{
  251. "{\n"
  252. " \"files\": \"applefile=" +
  253. content1 + "\"\n}"};
  254. EXPECT_EQ(expected_text, response.text);
  255. EXPECT_EQ(url, response.url);
  256. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  257. EXPECT_EQ(201, response.status_code);
  258. EXPECT_EQ(ErrorCode::OK, response.error.code);
  259. }
  260. {
  261. Response response = cpr::Post(url, Multipart{{"files", Files{"file1", "file2"}}});
  262. std::string expected_text{
  263. "{\n"
  264. " \"files\": \"file1=" +
  265. content1 +
  266. "\",\n"
  267. " \"files\": \"file2=" +
  268. content2 + "\"\n}"};
  269. EXPECT_EQ(expected_text, response.text);
  270. EXPECT_EQ(url, response.url);
  271. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  272. EXPECT_EQ(201, response.status_code);
  273. EXPECT_EQ(ErrorCode::OK, response.error.code);
  274. }
  275. {
  276. Response response = cpr::Post(url, Multipart{{"files", Files{
  277. File{"file1", "applefile"},
  278. File{"file2", "bananafile"},
  279. }}});
  280. std::string expected_text{
  281. "{\n"
  282. " \"files\": \"applefile=" +
  283. content1 +
  284. "\",\n"
  285. " \"files\": \"bananafile=" +
  286. content2 + "\"\n}"};
  287. EXPECT_EQ(expected_text, response.text);
  288. EXPECT_EQ(url, response.url);
  289. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  290. EXPECT_EQ(201, response.status_code);
  291. EXPECT_EQ(ErrorCode::OK, response.error.code);
  292. }
  293. std::remove(filename1.c_str());
  294. std::remove(filename2.c_str());
  295. }
  296. TEST(UrlEncodedPostTests, FormPostFileTestWithOverridedFilename) {
  297. std::string filename{"test_file"};
  298. std::string overided_filename{"overided_filename"};
  299. std::string content{"hello world"};
  300. std::ofstream test_file;
  301. test_file.open(filename);
  302. test_file << content;
  303. test_file.close();
  304. Url url{server->GetBaseUrl() + "/form_post.html"};
  305. Response response = cpr::Post(url, Multipart{{"x", File{filename, overided_filename}}});
  306. std::string expected_text{
  307. "{\n"
  308. " \"x\": \"" +
  309. overided_filename + "=" + content +
  310. "\"\n"
  311. "}"};
  312. std::remove(filename.c_str());
  313. EXPECT_EQ(expected_text, response.text);
  314. EXPECT_EQ(url, response.url);
  315. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  316. EXPECT_EQ(201, response.status_code);
  317. EXPECT_EQ(ErrorCode::OK, response.error.code);
  318. }
  319. TEST(UrlEncodedPostTests, FormPostFileNoCopyTest) {
  320. std::string filename{"./test_file"};
  321. std::string content{"hello world"};
  322. std::ofstream test_file;
  323. test_file.open(filename);
  324. test_file << content;
  325. test_file.close();
  326. Url url{server->GetBaseUrl() + "/form_post.html"};
  327. Multipart multipart{{"x", File{filename}}};
  328. Response response = cpr::Post(url, multipart);
  329. std::string expected_text{
  330. "{\n"
  331. " \"x\": \"test_file=" +
  332. content +
  333. "\"\n"
  334. "}"};
  335. std::remove(filename.c_str());
  336. EXPECT_EQ(expected_text, response.text);
  337. EXPECT_EQ(url, response.url);
  338. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  339. EXPECT_EQ(201, response.status_code);
  340. EXPECT_EQ(ErrorCode::OK, response.error.code);
  341. }
  342. TEST(UrlEncodedPostTests, FormPostFileNoCopyTestWithOverridedFilename) {
  343. std::string filename{"test_file"};
  344. std::string overrided_filename{"overided_filename"};
  345. std::string content{"hello world"};
  346. std::ofstream test_file;
  347. test_file.open(filename);
  348. test_file << content;
  349. test_file.close();
  350. Url url{server->GetBaseUrl() + "/form_post.html"};
  351. Multipart multipart{{"x", File{filename, overrided_filename}}};
  352. Response response = cpr::Post(url, multipart);
  353. std::string expected_text{
  354. "{\n"
  355. " \"x\": \"" +
  356. overrided_filename + "=" + content +
  357. "\"\n"
  358. "}"};
  359. std::remove(filename.c_str());
  360. EXPECT_EQ(expected_text, response.text);
  361. EXPECT_EQ(url, response.url);
  362. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  363. EXPECT_EQ(201, response.status_code);
  364. EXPECT_EQ(ErrorCode::OK, response.error.code);
  365. }
  366. TEST(UrlEncodedPostTests, TimeoutPostTest) {
  367. Url url{server->GetBaseUrl() + "/json_post.html"};
  368. std::string body{R"({"RegisterObject": {"DeviceID": "65010000005030000001"}})"};
  369. cpr::Response response = cpr::Post(url, cpr::Header{{"Content-Type", "application/json"}}, cpr::Body{body}, cpr::ConnectTimeout{3000}, cpr::Timeout{3000});
  370. std::string expected_text{R"({"RegisterObject": {"DeviceID": "65010000005030000001"}})"};
  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(201, response.status_code);
  375. EXPECT_EQ(ErrorCode::OK, response.error.code);
  376. }
  377. TEST(UrlEncodedPostTests, FormPostFileBufferTest) {
  378. std::string content{"hello world"};
  379. Url url{server->GetBaseUrl() + "/form_post.html"};
  380. Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
  381. std::string expected_text{
  382. "{\n"
  383. " \"x\": \"test_file=" +
  384. content +
  385. "\"\n"
  386. "}"};
  387. EXPECT_EQ(expected_text, response.text);
  388. EXPECT_EQ(url, response.url);
  389. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  390. EXPECT_EQ(201, response.status_code);
  391. EXPECT_EQ(ErrorCode::OK, response.error.code);
  392. }
  393. TEST(UrlEncodedPostTests, FormPostFileBufferNoCopyTest) {
  394. std::string content{"hello world"};
  395. Url url{server->GetBaseUrl() + "/form_post.html"};
  396. Multipart multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}};
  397. Response response = cpr::Post(url, multipart);
  398. std::string expected_text{
  399. "{\n"
  400. " \"x\": \"test_file=" +
  401. content +
  402. "\"\n"
  403. "}"};
  404. EXPECT_EQ(expected_text, response.text);
  405. EXPECT_EQ(url, response.url);
  406. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  407. EXPECT_EQ(201, response.status_code);
  408. EXPECT_EQ(ErrorCode::OK, response.error.code);
  409. }
  410. TEST(UrlEncodedPostTests, FormPostFileBufferPointerTest) {
  411. const char* content = "hello world";
  412. Url url{server->GetBaseUrl() + "/form_post.html"};
  413. Response response = cpr::Post(url, Multipart{{"x", Buffer{content, 11 + content, "test_file"}}});
  414. std::string expected_text{
  415. "{\n"
  416. " \"x\": \"test_file=" +
  417. std::string(content) +
  418. "\"\n"
  419. "}"};
  420. EXPECT_EQ(expected_text, response.text);
  421. EXPECT_EQ(url, response.url);
  422. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  423. EXPECT_EQ(201, response.status_code);
  424. EXPECT_EQ(ErrorCode::OK, response.error.code);
  425. }
  426. TEST(UrlEncodedPostTests, FormPostFileBufferArrayTest) {
  427. const char content[] = "hello world";
  428. Url url{server->GetBaseUrl() + "/form_post.html"};
  429. // We subtract 1 from std::end() because we don't want to include the terminating null
  430. Response response = cpr::Post(url, Multipart{{"x", Buffer{std::begin(content), std::end(content) - 1, "test_file"}}});
  431. std::string expected_text{
  432. "{\n"
  433. " \"x\": \"test_file=" +
  434. std::string(content) +
  435. "\"\n"
  436. "}"};
  437. EXPECT_EQ(expected_text, response.text);
  438. EXPECT_EQ(url, response.url);
  439. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  440. EXPECT_EQ(201, response.status_code);
  441. EXPECT_EQ(ErrorCode::OK, response.error.code);
  442. }
  443. TEST(UrlEncodedPostTests, FormPostFileBufferVectorTest) {
  444. std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
  445. Url url{server->GetBaseUrl() + "/form_post.html"};
  446. Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
  447. std::string expected_text{
  448. "{\n"
  449. " \"x\": \"test_file=hello world\"\n"
  450. "}"};
  451. EXPECT_EQ(expected_text, response.text);
  452. EXPECT_EQ(url, response.url);
  453. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  454. EXPECT_EQ(201, response.status_code);
  455. EXPECT_EQ(ErrorCode::OK, response.error.code);
  456. }
  457. TEST(UrlEncodedPostTests, FormPostFileBufferStdArrayTest) {
  458. std::array<unsigned char, 11> content{{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}};
  459. Url url{server->GetBaseUrl() + "/form_post.html"};
  460. Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
  461. std::string expected_text{
  462. "{\n"
  463. " \"x\": \"test_file=hello world\"\n"
  464. "}"};
  465. EXPECT_EQ(expected_text, response.text);
  466. EXPECT_EQ(url, response.url);
  467. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  468. EXPECT_EQ(201, response.status_code);
  469. EXPECT_EQ(ErrorCode::OK, response.error.code);
  470. }
  471. TEST(UrlEncodedPostTests, FormPostBufferRvalueTest) {
  472. std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
  473. Url url{server->GetBaseUrl() + "/form_post.html"};
  474. Response response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
  475. std::string expected_text{
  476. "{\n"
  477. " \"x\": \"test_file=hello world\"\n"
  478. "}"};
  479. EXPECT_EQ(expected_text, response.text);
  480. EXPECT_EQ(url, response.url);
  481. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  482. EXPECT_EQ(201, response.status_code);
  483. EXPECT_EQ(ErrorCode::OK, response.error.code);
  484. }
  485. TEST(UrlEncodedPostTests, ReflectPostBufferLvalueTest) {
  486. std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
  487. Url url{server->GetBaseUrl() + "/form_post.html"};
  488. Buffer buff{content.begin(), content.end(), "test_file"};
  489. Response response = cpr::Post(url, Multipart{{"x", buff}});
  490. std::string expected_text{
  491. "{\n"
  492. " \"x\": \"test_file=hello world\"\n"
  493. "}"};
  494. EXPECT_EQ(expected_text, response.text);
  495. EXPECT_EQ(url, response.url);
  496. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  497. EXPECT_EQ(201, response.status_code);
  498. EXPECT_EQ(ErrorCode::OK, response.error.code);
  499. }
  500. TEST(UrlEncodedPostTests, FormPostManyTest) {
  501. Url url{server->GetBaseUrl() + "/form_post.html"};
  502. Response response = cpr::Post(url, Multipart{{"x", 5}, {"y", 13}});
  503. std::string expected_text{
  504. "{\n"
  505. " \"x\": \"5\",\n"
  506. " \"y\": \"13\"\n"
  507. "}"};
  508. EXPECT_EQ(expected_text, response.text);
  509. EXPECT_EQ(url, response.url);
  510. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  511. EXPECT_EQ(201, response.status_code);
  512. EXPECT_EQ(ErrorCode::OK, response.error.code);
  513. }
  514. TEST(UrlEncodedPostTests, FormPostManyNoCopyTest) {
  515. Url url{server->GetBaseUrl() + "/form_post.html"};
  516. Multipart multipart{{"x", 5}, {"y", 13}};
  517. Response response = cpr::Post(url, multipart);
  518. std::string expected_text{
  519. "{\n"
  520. " \"x\": \"5\",\n"
  521. " \"y\": \"13\"\n"
  522. "}"};
  523. EXPECT_EQ(expected_text, response.text);
  524. EXPECT_EQ(url, response.url);
  525. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  526. EXPECT_EQ(201, response.status_code);
  527. EXPECT_EQ(ErrorCode::OK, response.error.code);
  528. }
  529. TEST(UrlEncodedPostTests, FormPostContentTypeTest) {
  530. Url url{server->GetBaseUrl() + "/form_post.html"};
  531. Response response = cpr::Post(url, Multipart{{"x", 5, "application/number"}});
  532. std::string expected_text{
  533. "{\n"
  534. " \"x\": \"5\"\n"
  535. "}"};
  536. EXPECT_EQ(expected_text, response.text);
  537. EXPECT_EQ(url, response.url);
  538. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  539. EXPECT_EQ(201, response.status_code);
  540. EXPECT_EQ(ErrorCode::OK, response.error.code);
  541. }
  542. TEST(UrlEncodedPostTests, FormPostContentTypeLValueTest) {
  543. Url url{server->GetBaseUrl() + "/form_post.html"};
  544. Multipart multipart{{"x", 5, "application/number"}};
  545. Response response = cpr::Post(url, multipart);
  546. std::string expected_text{
  547. "{\n"
  548. " \"x\": \"5\"\n"
  549. "}"};
  550. EXPECT_EQ(expected_text, response.text);
  551. EXPECT_EQ(url, response.url);
  552. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  553. EXPECT_EQ(201, response.status_code);
  554. EXPECT_EQ(ErrorCode::OK, response.error.code);
  555. }
  556. TEST(UrlEncodedPostTests, UrlPostAsyncSingleTest) {
  557. Url url{server->GetBaseUrl() + "/url_post.html"};
  558. Payload payload{{"x", "5"}};
  559. std::vector<AsyncResponse> responses;
  560. for (size_t i = 0; i < 10; ++i) {
  561. responses.emplace_back(cpr::PostAsync(url, payload));
  562. }
  563. for (cpr::AsyncResponse& future_response : responses) {
  564. cpr::Response response = future_response.get();
  565. std::string expected_text{
  566. "{\n"
  567. " \"x\": 5\n"
  568. "}"};
  569. EXPECT_EQ(expected_text, response.text);
  570. EXPECT_EQ(url, response.url);
  571. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  572. EXPECT_EQ(201, response.status_code);
  573. EXPECT_EQ(ErrorCode::OK, response.error.code);
  574. }
  575. }
  576. TEST(UrlEncodedPostTests, UrlReflectTest) {
  577. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  578. Response response = cpr::Post(url, Payload{{"x", "5"}});
  579. std::string expected_text{"Header reflect POST"};
  580. EXPECT_EQ(expected_text, response.text);
  581. EXPECT_EQ(url, response.url);
  582. EXPECT_EQ(200, response.status_code);
  583. EXPECT_EQ(ErrorCode::OK, response.error.code);
  584. }
  585. TEST(UrlEncodedPostTests, PostWithNoBodyTest) {
  586. Url url{server->GetBaseUrl() + "/form_post.html"};
  587. Response response = cpr::Post(url);
  588. std::string expected_text{"{\n}"};
  589. EXPECT_EQ(expected_text, response.text);
  590. EXPECT_EQ(url, response.url);
  591. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  592. EXPECT_EQ(201, response.status_code);
  593. EXPECT_EQ(ErrorCode::OK, response.error.code);
  594. }
  595. static std::string getTimestamp() {
  596. char buf[1000];
  597. time_t now = time(0);
  598. struct tm* tm = gmtime(&now);
  599. strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S GMT", tm);
  600. return buf;
  601. }
  602. TEST(UrlEncodedPostTests, PostReflectTest) {
  603. std::string uri = server->GetBaseUrl() + "/post_reflect.html";
  604. std::string body = R"({"property1": "value1"})";
  605. std::string contentType = "application/json";
  606. std::string signature = "x-ms-date: something";
  607. std::string logType = "LoggingTest";
  608. std::string date = getTimestamp();
  609. Response response = cpr::Post(cpr::Url(uri), cpr::Header{{"content-type", contentType}, {"Authorization", signature}, {"log-type", logType}, {"x-ms-date", date}, {"content-length", std::to_string(body.length())}}, cpr::Body(body));
  610. EXPECT_EQ(ErrorCode::OK, response.error.code);
  611. EXPECT_EQ(200, response.status_code);
  612. EXPECT_EQ(body, response.text);
  613. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  614. EXPECT_EQ(signature, response.header["Authorization"]);
  615. EXPECT_EQ(logType, response.header["log-type"]);
  616. EXPECT_EQ(date, response.header["x-ms-date"]);
  617. EXPECT_EQ(std::to_string(body.length()), response.header["content-length"]);
  618. }
  619. TEST(UrlEncodedPostTests, PostReflectPayloadTest) {
  620. std::string uri = server->GetBaseUrl() + "/header_reflect.html";
  621. cpr::Payload payload = cpr::Payload{{"email", ""}, {"password", ""}, {"devicetoken", ""}};
  622. cpr::Response response = cpr::Post(cpr::Url(uri), cpr::Timeout{10000}, payload);
  623. EXPECT_EQ(ErrorCode::OK, response.error.code);
  624. EXPECT_EQ(200, response.status_code);
  625. }
  626. TEST(UrlEncodedPostTests, InjectMultipleHeadersTest) {
  627. std::string uri = server->GetBaseUrl() + "/post_reflect.html";
  628. std::string key_1 = "key_1";
  629. std::string val_1 = "value_1";
  630. std::string key_2 = "key_2";
  631. std::string val_2 = "value_2";
  632. cpr::Response response = cpr::Post(cpr::Url{uri}, cpr::Header{{key_1, val_1}}, cpr::Header{{key_2, val_2}});
  633. EXPECT_EQ(ErrorCode::OK, response.error.code);
  634. EXPECT_EQ(200, response.status_code);
  635. EXPECT_EQ(val_1, response.header[key_1]);
  636. EXPECT_EQ(val_2, response.header[key_2]);
  637. }
  638. TEST(UrlEncodedPostTests, PostBodyWithFile) {
  639. std::string filename{"test_file"};
  640. std::string expected_text(R"({"property1": "value1"})");
  641. std::ofstream test_file;
  642. test_file.open(filename);
  643. test_file << expected_text;
  644. test_file.close();
  645. Url url{server->GetBaseUrl() + "/post_reflect.html"};
  646. cpr::Response response = Post(url, cpr::Header({{"Content-Type", "application/octet-stream"}}), cpr::Body(File("test_file")));
  647. EXPECT_EQ(expected_text, response.text);
  648. EXPECT_EQ(url, response.url);
  649. EXPECT_EQ(ErrorCode::OK, response.error.code);
  650. EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
  651. EXPECT_EQ(200, response.status_code);
  652. }
  653. TEST(UrlEncodedPostTests, PostBodyWithBuffer) {
  654. Url url{server->GetBaseUrl() + "/post_reflect.html"};
  655. std::string expected_text(R"({"property1": "value1"})");
  656. cpr::Response response = Post(url, cpr::Header({{"Content-Type", "application/octet-stream"}}), cpr::Body(Buffer{expected_text.begin(), expected_text.end(), "test_file"}));
  657. EXPECT_EQ(expected_text, response.text);
  658. EXPECT_EQ(url, response.url);
  659. EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
  660. EXPECT_EQ(200, response.status_code);
  661. EXPECT_EQ(ErrorCode::OK, response.error.code);
  662. }
  663. TEST(PostRedirectTests, TempRedirectTest) {
  664. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  665. Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
  666. std::string expected_text{
  667. "{\n"
  668. " \"x\": 5\n"
  669. "}"};
  670. EXPECT_EQ(expected_text, response.text);
  671. EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
  672. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  673. EXPECT_EQ(201, response.status_code);
  674. EXPECT_EQ(ErrorCode::OK, response.error.code);
  675. }
  676. TEST(PostRedirectTests, TempRedirectNoneTest) {
  677. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  678. Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));
  679. EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
  680. EXPECT_EQ(405, response.status_code);
  681. EXPECT_EQ(ErrorCode::OK, response.error.code);
  682. }
  683. TEST(PostRedirectTests, PermRedirectTest) {
  684. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  685. Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}});
  686. std::string expected_text{
  687. "{\n"
  688. " \"x\": 5\n"
  689. "}"};
  690. EXPECT_EQ(expected_text, response.text);
  691. EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
  692. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  693. EXPECT_EQ(201, response.status_code);
  694. EXPECT_EQ(ErrorCode::OK, response.error.code);
  695. }
  696. TEST(PostRedirectTests, PermRedirectNoneTest) {
  697. Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
  698. Response response = cpr::Post(url, Payload{{"x", "5"}}, Header{{"RedirectLocation", "url_post.html"}}, Redirect(PostRedirectFlags::NONE));
  699. EXPECT_EQ(response.url, server->GetBaseUrl() + "/url_post.html");
  700. EXPECT_EQ(405, response.status_code);
  701. EXPECT_EQ(ErrorCode::OK, response.error.code);
  702. }
  703. int main(int argc, char** argv) {
  704. ::testing::InitGoogleTest(&argc, argv);
  705. ::testing::AddGlobalTestEnvironment(server);
  706. return RUN_ALL_TESTS();
  707. }