patch_tests.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <cpr/cpr.h>
  4. #include "httpServer.hpp"
  5. using namespace cpr;
  6. static HttpServer* server = new HttpServer();
  7. TEST(PatchTests, PatchTest) {
  8. Url url{server->GetBaseUrl() + "/patch.html"};
  9. Payload payload{{"x", "5"}};
  10. Response response = cpr::Patch(url, payload);
  11. std::string expected_text{
  12. "{\n"
  13. " \"x\": 5\n"
  14. "}"};
  15. EXPECT_EQ(expected_text, response.text);
  16. EXPECT_EQ(url, response.url);
  17. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  18. EXPECT_EQ(200, response.status_code);
  19. EXPECT_EQ(ErrorCode::OK, response.error.code);
  20. }
  21. TEST(PatchTests, PatchUnallowedTest) {
  22. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  23. Payload payload{{"x", "5"}};
  24. Response response = cpr::Patch(url, payload);
  25. std::string expected_text{"Method Not Allowed"};
  26. EXPECT_EQ(expected_text, response.text);
  27. EXPECT_EQ(url, response.url);
  28. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  29. EXPECT_EQ(405, response.status_code);
  30. EXPECT_EQ(ErrorCode::OK, response.error.code);
  31. }
  32. TEST(PatchTests, SessionPatchTest) {
  33. Url url{server->GetBaseUrl() + "/patch.html"};
  34. Payload payload{{"x", "5"}};
  35. Session session;
  36. session.SetUrl(url);
  37. session.SetPayload(payload);
  38. Response response = session.Patch();
  39. std::string expected_text{
  40. "{\n"
  41. " \"x\": 5\n"
  42. "}"};
  43. EXPECT_EQ(expected_text, response.text);
  44. EXPECT_EQ(url, response.url);
  45. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  46. EXPECT_EQ(200, response.status_code);
  47. EXPECT_EQ(ErrorCode::OK, response.error.code);
  48. }
  49. TEST(PatchTests, SessionPatchUnallowedTest) {
  50. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  51. Payload payload{{"x", "5"}};
  52. Session session;
  53. session.SetUrl(url);
  54. session.SetPayload(payload);
  55. Response response = session.Patch();
  56. std::string expected_text{"Method Not Allowed"};
  57. EXPECT_EQ(expected_text, response.text);
  58. EXPECT_EQ(url, response.url);
  59. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  60. EXPECT_EQ(405, response.status_code);
  61. EXPECT_EQ(ErrorCode::OK, response.error.code);
  62. }
  63. TEST(PatchTests, SessionPatchAfterGetTest) {
  64. Session session;
  65. {
  66. Url url{server->GetBaseUrl() + "/get.html"};
  67. session.SetUrl(url);
  68. Response response = session.Get();
  69. }
  70. Url url{server->GetBaseUrl() + "/patch.html"};
  71. Payload payload{{"x", "5"}};
  72. session.SetUrl(url);
  73. session.SetPayload(payload);
  74. Response response = session.Patch();
  75. std::string expected_text{
  76. "{\n"
  77. " \"x\": 5\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(200, response.status_code);
  83. EXPECT_EQ(ErrorCode::OK, response.error.code);
  84. }
  85. TEST(PatchTests, SessionPatchUnallowedAfterGetTest) {
  86. Session session;
  87. {
  88. Url url{server->GetBaseUrl() + "/get.html"};
  89. session.SetUrl(url);
  90. Response response = session.Get();
  91. }
  92. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  93. Payload payload{{"x", "5"}};
  94. session.SetUrl(url);
  95. session.SetPayload(payload);
  96. Response response = session.Patch();
  97. std::string expected_text{"Method Not Allowed"};
  98. EXPECT_EQ(expected_text, response.text);
  99. EXPECT_EQ(url, response.url);
  100. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  101. EXPECT_EQ(405, response.status_code);
  102. EXPECT_EQ(ErrorCode::OK, response.error.code);
  103. }
  104. TEST(PatchTests, SessionPatchAfterHeadTest) {
  105. Session session;
  106. {
  107. Url url{server->GetBaseUrl() + "/get.html"};
  108. session.SetUrl(url);
  109. Response response = session.Head();
  110. }
  111. Url url{server->GetBaseUrl() + "/patch.html"};
  112. Payload payload{{"x", "5"}};
  113. session.SetUrl(url);
  114. session.SetPayload(payload);
  115. Response response = session.Patch();
  116. std::string expected_text{
  117. "{\n"
  118. " \"x\": 5\n"
  119. "}"};
  120. EXPECT_EQ(expected_text, response.text);
  121. EXPECT_EQ(url, response.url);
  122. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  123. EXPECT_EQ(200, response.status_code);
  124. EXPECT_EQ(ErrorCode::OK, response.error.code);
  125. }
  126. TEST(PatchTests, SessionPatchUnallowedAfterHeadTest) {
  127. Session session;
  128. {
  129. Url url{server->GetBaseUrl() + "/get.html"};
  130. session.SetUrl(url);
  131. Response response = session.Head();
  132. }
  133. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  134. Payload payload{{"x", "5"}};
  135. session.SetUrl(url);
  136. session.SetPayload(payload);
  137. Response response = session.Patch();
  138. std::string expected_text{"Method Not Allowed"};
  139. EXPECT_EQ(expected_text, response.text);
  140. EXPECT_EQ(url, response.url);
  141. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  142. EXPECT_EQ(405, response.status_code);
  143. EXPECT_EQ(ErrorCode::OK, response.error.code);
  144. }
  145. TEST(PatchTests, SessionPatchAfterPostTest) {
  146. Session session;
  147. {
  148. Url url{server->GetBaseUrl() + "/url_post.html"};
  149. Payload payload{{"x", "5"}};
  150. session.SetUrl(url);
  151. Response response = session.Post();
  152. }
  153. Url url{server->GetBaseUrl() + "/patch.html"};
  154. Payload payload{{"x", "5"}};
  155. session.SetUrl(url);
  156. session.SetPayload(payload);
  157. Response response = session.Patch();
  158. std::string expected_text{
  159. "{\n"
  160. " \"x\": 5\n"
  161. "}"};
  162. EXPECT_EQ(expected_text, response.text);
  163. EXPECT_EQ(url, response.url);
  164. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  165. EXPECT_EQ(200, response.status_code);
  166. EXPECT_EQ(ErrorCode::OK, response.error.code);
  167. }
  168. TEST(PatchTests, SessionPatchUnallowedAfterPostTest) {
  169. Session session;
  170. {
  171. Url url{server->GetBaseUrl() + "/url_post.html"};
  172. Payload payload{{"x", "5"}};
  173. session.SetUrl(url);
  174. Response response = session.Post();
  175. }
  176. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  177. Payload payload{{"x", "5"}};
  178. session.SetUrl(url);
  179. session.SetPayload(payload);
  180. Response response = session.Patch();
  181. std::string expected_text{"Method Not Allowed"};
  182. EXPECT_EQ(expected_text, response.text);
  183. EXPECT_EQ(url, response.url);
  184. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  185. EXPECT_EQ(405, response.status_code);
  186. EXPECT_EQ(ErrorCode::OK, response.error.code);
  187. }
  188. TEST(PatchTests, AsyncPatchTest) {
  189. Url url{server->GetBaseUrl() + "/patch.html"};
  190. Payload payload{{"x", "5"}};
  191. cpr::AsyncResponse future_response = cpr::PatchAsync(url, payload);
  192. cpr::Response response = future_response.get();
  193. std::string expected_text{
  194. "{\n"
  195. " \"x\": 5\n"
  196. "}"};
  197. EXPECT_EQ(expected_text, response.text);
  198. EXPECT_EQ(url, response.url);
  199. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  200. EXPECT_EQ(200, response.status_code);
  201. EXPECT_EQ(ErrorCode::OK, response.error.code);
  202. }
  203. TEST(PatchTests, AsyncPatchUnallowedTest) {
  204. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  205. Payload payload{{"x", "5"}};
  206. cpr::AsyncResponse future_response = cpr::PatchAsync(url, payload);
  207. cpr::Response response = future_response.get();
  208. std::string expected_text{"Method Not Allowed"};
  209. EXPECT_EQ(expected_text, response.text);
  210. EXPECT_EQ(url, response.url);
  211. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  212. EXPECT_EQ(405, response.status_code);
  213. EXPECT_EQ(ErrorCode::OK, response.error.code);
  214. }
  215. TEST(PatchTests, AsyncMultiplePatchTest) {
  216. Url url{server->GetBaseUrl() + "/patch.html"};
  217. Payload payload{{"x", "5"}};
  218. std::vector<AsyncResponse> responses;
  219. for (size_t i = 0; i < 10; ++i) {
  220. responses.emplace_back(cpr::PatchAsync(url, payload));
  221. }
  222. for (cpr::AsyncResponse& future_response : responses) {
  223. cpr::Response response = future_response.get();
  224. std::string expected_text{
  225. "{\n"
  226. " \"x\": 5\n"
  227. "}"};
  228. EXPECT_EQ(expected_text, response.text);
  229. EXPECT_EQ(url, response.url);
  230. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  231. EXPECT_EQ(200, response.status_code);
  232. EXPECT_EQ(ErrorCode::OK, response.error.code);
  233. }
  234. }
  235. TEST(PatchTests, AsyncMultiplePatchUnallowedTest) {
  236. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  237. Payload payload{{"x", "5"}};
  238. std::vector<AsyncResponse> responses;
  239. for (size_t i = 0; i < 10; ++i) {
  240. responses.emplace_back(cpr::PatchAsync(url, payload));
  241. }
  242. for (cpr::AsyncResponse& future_response : responses) {
  243. cpr::Response response = future_response.get();
  244. std::string expected_text{"Method Not Allowed"};
  245. EXPECT_EQ(expected_text, response.text);
  246. EXPECT_EQ(url, response.url);
  247. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  248. EXPECT_EQ(405, response.status_code);
  249. EXPECT_EQ(ErrorCode::OK, response.error.code);
  250. }
  251. }
  252. int main(int argc, char** argv) {
  253. ::testing::InitGoogleTest(&argc, argv);
  254. ::testing::AddGlobalTestEnvironment(server);
  255. return RUN_ALL_TESTS();
  256. }