delete_tests.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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(DeleteTests, DeleteTest) {
  8. Url url{server->GetBaseUrl() + "/delete.html"};
  9. Response response = cpr::Delete(url);
  10. std::string expected_text{"Delete success"};
  11. EXPECT_EQ(expected_text, response.text);
  12. EXPECT_EQ(url, response.url);
  13. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  14. EXPECT_EQ(200, response.status_code);
  15. EXPECT_EQ(ErrorCode::OK, response.error.code);
  16. }
  17. TEST(DeleteTests, DeleteUnallowedTest) {
  18. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  19. Response response = cpr::Delete(url);
  20. std::string expected_text{"Method Not Allowed"};
  21. EXPECT_EQ(expected_text, response.text);
  22. EXPECT_EQ(url, response.url);
  23. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  24. EXPECT_EQ(405, response.status_code);
  25. EXPECT_EQ(ErrorCode::OK, response.error.code);
  26. }
  27. TEST(DeleteTests, DeleteJsonBodyTest) {
  28. Url url{server->GetBaseUrl() + "/delete.html"};
  29. Response response = cpr::Delete(url, cpr::Body{"'foo': 'bar'"}, cpr::Header{{"Content-Type", "application/json"}});
  30. std::string expected_text{"'foo': 'bar'"};
  31. EXPECT_EQ(expected_text, response.text);
  32. EXPECT_EQ(url, response.url);
  33. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  34. EXPECT_EQ(200, response.status_code);
  35. EXPECT_EQ(ErrorCode::OK, response.error.code);
  36. }
  37. TEST(DeleteTests, SessionDeleteTest) {
  38. Url url{server->GetBaseUrl() + "/delete.html"};
  39. Session session;
  40. session.SetUrl(url);
  41. Response response = session.Delete();
  42. std::string expected_text{"Delete success"};
  43. EXPECT_EQ(expected_text, response.text);
  44. EXPECT_EQ(url, response.url);
  45. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  46. EXPECT_EQ(200, response.status_code);
  47. EXPECT_EQ(ErrorCode::OK, response.error.code);
  48. }
  49. TEST(DeleteTests, SessionDeleteUnallowedTest) {
  50. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  51. Session session;
  52. session.SetUrl(url);
  53. Response response = session.Delete();
  54. std::string expected_text{"Method Not Allowed"};
  55. EXPECT_EQ(expected_text, response.text);
  56. EXPECT_EQ(url, response.url);
  57. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  58. EXPECT_EQ(405, response.status_code);
  59. EXPECT_EQ(ErrorCode::OK, response.error.code);
  60. }
  61. TEST(DeleteTests, SessionDeleteJsonBodyTest) {
  62. Url url{server->GetBaseUrl() + "/delete.html"};
  63. Session session;
  64. session.SetUrl(url);
  65. session.SetHeader(cpr::Header{{"Content-Type", "application/json"}});
  66. session.SetBody(cpr::Body{"{'foo': 'bar'}"});
  67. Response response = session.Delete();
  68. std::string expected_text{"{'foo': 'bar'}"};
  69. EXPECT_EQ(expected_text, response.text);
  70. EXPECT_EQ(url, response.url);
  71. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  72. EXPECT_EQ(200, response.status_code);
  73. EXPECT_EQ(ErrorCode::OK, response.error.code);
  74. }
  75. TEST(DeleteTests, SessionDeleteAfterGetTest) {
  76. Session session;
  77. {
  78. Url url{server->GetBaseUrl() + "/get.html"};
  79. session.SetUrl(url);
  80. Response response = session.Get();
  81. }
  82. Url url{server->GetBaseUrl() + "/delete.html"};
  83. session.SetUrl(url);
  84. Response response = session.Delete();
  85. std::string expected_text{"Delete success"};
  86. EXPECT_EQ(expected_text, response.text);
  87. EXPECT_EQ(url, response.url);
  88. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  89. EXPECT_EQ(200, response.status_code);
  90. EXPECT_EQ(ErrorCode::OK, response.error.code);
  91. }
  92. TEST(DeleteTests, SessionDeleteUnallowedAfterGetTest) {
  93. Session session;
  94. {
  95. Url url{server->GetBaseUrl() + "/get.html"};
  96. session.SetUrl(url);
  97. Response response = session.Get();
  98. }
  99. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  100. session.SetUrl(url);
  101. Response response = session.Delete();
  102. std::string expected_text{"Method Not Allowed"};
  103. EXPECT_EQ(expected_text, response.text);
  104. EXPECT_EQ(url, response.url);
  105. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  106. EXPECT_EQ(405, response.status_code);
  107. EXPECT_EQ(ErrorCode::OK, response.error.code);
  108. }
  109. TEST(DeleteTests, SessionDeleteAfterHeadTest) {
  110. Session session;
  111. {
  112. Url url{server->GetBaseUrl() + "/get.html"};
  113. session.SetUrl(url);
  114. Response response = session.Head();
  115. }
  116. Url url{server->GetBaseUrl() + "/delete.html"};
  117. session.SetUrl(url);
  118. Response response = session.Delete();
  119. std::string expected_text{"Delete success"};
  120. EXPECT_EQ(expected_text, response.text);
  121. EXPECT_EQ(url, response.url);
  122. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  123. EXPECT_EQ(200, response.status_code);
  124. EXPECT_EQ(ErrorCode::OK, response.error.code);
  125. }
  126. TEST(DeleteTests, SessionDeleteUnallowedAfterHeadTest) {
  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() + "/delete_unallowed.html"};
  134. session.SetUrl(url);
  135. Response response = session.Delete();
  136. std::string expected_text{"Method Not Allowed"};
  137. EXPECT_EQ(expected_text, response.text);
  138. EXPECT_EQ(url, response.url);
  139. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  140. EXPECT_EQ(405, response.status_code);
  141. EXPECT_EQ(ErrorCode::OK, response.error.code);
  142. }
  143. TEST(DeleteTests, SessionDeleteAfterPostTest) {
  144. Session session;
  145. {
  146. Url url{server->GetBaseUrl() + "/url_post.html"};
  147. Payload payload{{"x", "5"}};
  148. session.SetUrl(url);
  149. Response response = session.Post();
  150. }
  151. Url url{server->GetBaseUrl() + "/patch_unallowed.html"};
  152. session.SetUrl(url);
  153. Response response = session.Delete();
  154. std::string expected_text{"Delete success"};
  155. EXPECT_EQ(expected_text, response.text);
  156. EXPECT_EQ(url, response.url);
  157. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  158. EXPECT_EQ(200, response.status_code);
  159. EXPECT_EQ(ErrorCode::OK, response.error.code);
  160. }
  161. TEST(DeleteTests, SessionDeleteUnallowedAfterPostTest) {
  162. Session session;
  163. {
  164. Url url{server->GetBaseUrl() + "/url_post.html"};
  165. Payload payload{{"x", "5"}};
  166. session.SetUrl(url);
  167. Response response = session.Post();
  168. }
  169. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  170. session.SetUrl(url);
  171. Response response = session.Delete();
  172. std::string expected_text{"Method Not Allowed"};
  173. EXPECT_EQ(expected_text, response.text);
  174. EXPECT_EQ(url, response.url);
  175. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  176. EXPECT_EQ(405, response.status_code);
  177. EXPECT_EQ(ErrorCode::OK, response.error.code);
  178. }
  179. TEST(DeleteTests, AsyncDeleteTest) {
  180. Url url{server->GetBaseUrl() + "/delete.html"};
  181. cpr::AsyncResponse future_response = cpr::DeleteAsync(url);
  182. cpr::Response response = future_response.get();
  183. std::string expected_text{"Delete success"};
  184. EXPECT_EQ(expected_text, response.text);
  185. EXPECT_EQ(url, 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(DeleteTests, AsyncDeleteUnallowedTest) {
  191. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  192. cpr::AsyncResponse future_response = cpr::DeleteAsync(url);
  193. cpr::Response response = future_response.get();
  194. std::string expected_text{"Method Not Allowed"};
  195. EXPECT_EQ(expected_text, response.text);
  196. EXPECT_EQ(url, response.url);
  197. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  198. EXPECT_EQ(405, response.status_code);
  199. EXPECT_EQ(ErrorCode::OK, response.error.code);
  200. }
  201. TEST(DeleteTests, AsyncMultipleDeleteTest) {
  202. Url url{server->GetBaseUrl() + "/delete.html"};
  203. std::vector<AsyncResponse> responses;
  204. for (size_t i = 0; i < 10; ++i) {
  205. responses.emplace_back(cpr::DeleteAsync(url));
  206. }
  207. for (cpr::AsyncResponse& future_response : responses) {
  208. cpr::Response response = future_response.get();
  209. std::string expected_text{"Delete success"};
  210. EXPECT_EQ(expected_text, response.text);
  211. EXPECT_EQ(url, response.url);
  212. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  213. EXPECT_EQ(200, response.status_code);
  214. EXPECT_EQ(ErrorCode::OK, response.error.code);
  215. }
  216. }
  217. TEST(DeleteTests, AsyncMultipleDeleteUnallowedTest) {
  218. Url url{server->GetBaseUrl() + "/delete_unallowed.html"};
  219. std::vector<AsyncResponse> responses;
  220. for (size_t i = 0; i < 10; ++i) {
  221. responses.emplace_back(cpr::DeleteAsync(url));
  222. }
  223. for (cpr::AsyncResponse& future_response : responses) {
  224. cpr::Response response = future_response.get();
  225. std::string expected_text{"Method Not Allowed"};
  226. EXPECT_EQ(expected_text, response.text);
  227. EXPECT_EQ(url, response.url);
  228. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  229. EXPECT_EQ(405, response.status_code);
  230. EXPECT_EQ(ErrorCode::OK, response.error.code);
  231. }
  232. }
  233. int main(int argc, char** argv) {
  234. ::testing::InitGoogleTest(&argc, argv);
  235. ::testing::AddGlobalTestEnvironment(server);
  236. return RUN_ALL_TESTS();
  237. }