raw_body_tests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <gtest/gtest.h>
  2. #include <cstdio>
  3. #include <fstream>
  4. #include <string>
  5. #include <cpr/cpr.h>
  6. #include <cpr/multipart.h>
  7. #include "httpServer.hpp"
  8. using namespace cpr;
  9. static HttpServer* server = new HttpServer();
  10. TEST(BodyPostTests, DefaultUrlEncodedPostTest) {
  11. Url url{server->GetBaseUrl() + "/url_post.html"};
  12. Response response = cpr::Post(url, Body{"x=5"});
  13. std::string expected_text = "{\n \"x\": 5\n}";
  14. EXPECT_EQ(expected_text, response.text);
  15. EXPECT_EQ(url, response.url);
  16. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  17. EXPECT_EQ(201, response.status_code);
  18. EXPECT_EQ(ErrorCode::OK, response.error.code);
  19. }
  20. TEST(BodyPostTests, TextUrlEncodedPostTest) {
  21. Url url{server->GetBaseUrl() + "/url_post.html"};
  22. Response response = cpr::Post(url, Body{"x=hello world!!~"});
  23. std::string expected_text{
  24. "{\n"
  25. " \"x\": hello world!!~\n"
  26. "}"};
  27. EXPECT_EQ(expected_text, response.text);
  28. EXPECT_EQ(url, response.url);
  29. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  30. EXPECT_EQ(201, response.status_code);
  31. EXPECT_EQ(ErrorCode::OK, response.error.code);
  32. }
  33. TEST(BodyPostTests, TextUrlEncodedNoCopyPostTest) {
  34. Url url{server->GetBaseUrl() + "/url_post.html"};
  35. Body body{"x=hello world!!~"};
  36. // body lives through the lifetime of Post, so it doesn't need to be copied
  37. Response response = cpr::Post(url, body);
  38. std::string expected_text{
  39. "{\n"
  40. " \"x\": hello world!!~\n"
  41. "}"};
  42. EXPECT_EQ(expected_text, response.text);
  43. EXPECT_EQ(url, response.url);
  44. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  45. EXPECT_EQ(201, response.status_code);
  46. EXPECT_EQ(ErrorCode::OK, response.error.code);
  47. }
  48. TEST(BodyPostTests, UrlEncodedManyPostTest) {
  49. Url url{server->GetBaseUrl() + "/url_post.html"};
  50. Response response = cpr::Post(url, Body{"x=5&y=13"});
  51. std::string expected_text{
  52. "{\n"
  53. " \"x\": 5,\n"
  54. " \"y\": 13,\n"
  55. " \"sum\": 18\n"
  56. "}"};
  57. EXPECT_EQ(expected_text, response.text);
  58. EXPECT_EQ(url, response.url);
  59. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  60. EXPECT_EQ(201, response.status_code);
  61. EXPECT_EQ(ErrorCode::OK, response.error.code);
  62. }
  63. TEST(BodyPostTests, CustomHeaderNumberPostTest) {
  64. Url url{server->GetBaseUrl() + "/json_post.html"};
  65. Response response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "application/json"}});
  66. std::string expected_text{"{\"x\":5}"};
  67. EXPECT_EQ(expected_text, response.text);
  68. EXPECT_EQ(url, response.url);
  69. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  70. EXPECT_EQ(201, response.status_code);
  71. EXPECT_EQ(ErrorCode::OK, response.error.code);
  72. }
  73. TEST(BodyPostTests, CustomHeaderTextPostTest) {
  74. Url url{server->GetBaseUrl() + "/json_post.html"};
  75. Response response = cpr::Post(url, Body{"{\"x\":\"hello world!!~\"}"}, Header{{"Content-Type", "application/json"}});
  76. std::string expected_text{"{\"x\":\"hello world!!~\"}"};
  77. EXPECT_EQ(expected_text, response.text);
  78. EXPECT_EQ(url, response.url);
  79. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  80. EXPECT_EQ(201, response.status_code);
  81. EXPECT_EQ(ErrorCode::OK, response.error.code);
  82. }
  83. TEST(BodyPostTests, CustomWrongHeaderPostTest) {
  84. Url url{server->GetBaseUrl() + "/json_post.html"};
  85. Response response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "text/plain"}});
  86. std::string expected_text{"Unsupported Media Type"};
  87. EXPECT_EQ(expected_text, response.text);
  88. EXPECT_EQ(url, response.url);
  89. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  90. EXPECT_EQ(415, response.status_code);
  91. EXPECT_EQ(ErrorCode::OK, response.error.code);
  92. }
  93. TEST(BodyPostTests, UrlPostBadHostTest) {
  94. Url url{"http://bad_host/"};
  95. Response response = cpr::Post(url, Body{"hello=world"});
  96. EXPECT_EQ(std::string{}, response.text);
  97. EXPECT_EQ(url, response.url);
  98. EXPECT_EQ(std::string{}, response.header["content-type"]);
  99. EXPECT_EQ(0, response.status_code);
  100. EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
  101. }
  102. TEST(BodyPostTests, StringMoveBodyTest) {
  103. Url url{server->GetBaseUrl() + "/url_post.html"};
  104. Response response = cpr::Post(url, Body{std::string{"x=5"}});
  105. std::string expected_text{
  106. "{\n"
  107. " \"x\": 5\n"
  108. "}"};
  109. EXPECT_EQ(expected_text, response.text);
  110. EXPECT_EQ(url, response.url);
  111. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  112. EXPECT_EQ(201, response.status_code);
  113. EXPECT_EQ(ErrorCode::OK, response.error.code);
  114. }
  115. int main(int argc, char** argv) {
  116. ::testing::InitGoogleTest(&argc, argv);
  117. ::testing::AddGlobalTestEnvironment(server);
  118. return RUN_ALL_TESTS();
  119. }