prepare_tests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <vector>
  4. #include <cpr/cpr.h>
  5. #include "httpServer.hpp"
  6. using namespace cpr;
  7. static HttpServer* server = new HttpServer();
  8. TEST(PrepareTests, GetTest) {
  9. Url url{server->GetBaseUrl() + "/hello.html"};
  10. Session session;
  11. session.SetUrl(url);
  12. session.PrepareGet();
  13. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  14. Response response = session.Complete(curl_result);
  15. std::string expected_text{"Hello world!"};
  16. EXPECT_EQ(expected_text, response.text);
  17. EXPECT_EQ(url, response.url);
  18. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  19. EXPECT_EQ(200, response.status_code);
  20. EXPECT_EQ(ErrorCode::OK, response.error.code);
  21. }
  22. TEST(PrepareTests, OptionsTests) {
  23. Url url{server->GetBaseUrl() + "/"};
  24. Session session;
  25. session.SetUrl(url);
  26. session.PrepareOptions();
  27. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  28. Response response = session.Complete(curl_result);
  29. std::string expected_text{""};
  30. EXPECT_EQ(expected_text, response.text);
  31. EXPECT_EQ(url, response.url);
  32. EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, response.header["Access-Control-Allow-Methods"]);
  33. EXPECT_EQ(200, response.status_code);
  34. EXPECT_EQ(ErrorCode::OK, response.error.code);
  35. }
  36. TEST(PrepareTests, PatchTest) {
  37. Url url{server->GetBaseUrl() + "/patch.html"};
  38. Payload payload{{"x", "5"}};
  39. Session session;
  40. session.SetUrl(url);
  41. session.SetPayload(payload);
  42. session.PreparePatch();
  43. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  44. Response response = session.Complete(curl_result);
  45. std::string expected_text{
  46. "{\n"
  47. " \"x\": 5\n"
  48. "}"};
  49. EXPECT_EQ(expected_text, response.text);
  50. EXPECT_EQ(url, response.url);
  51. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  52. EXPECT_EQ(200, response.status_code);
  53. EXPECT_EQ(ErrorCode::OK, response.error.code);
  54. }
  55. TEST(PrepareTests, MultipleDeleteHeadPutGetPostTest) {
  56. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  57. Url urlPost{server->GetBaseUrl() + "/post_reflect.html"};
  58. Url urlPut{server->GetBaseUrl() + "/put.html"};
  59. Session session;
  60. for (size_t i = 0; i < 3; ++i) {
  61. {
  62. session.SetUrl(url);
  63. session.PrepareDelete();
  64. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  65. Response response = session.Complete(curl_result);
  66. std::string expected_text{"Header reflect DELETE"};
  67. EXPECT_EQ(expected_text, response.text);
  68. EXPECT_EQ(url, response.url);
  69. EXPECT_EQ(200, response.status_code);
  70. EXPECT_EQ(ErrorCode::OK, response.error.code);
  71. }
  72. {
  73. session.SetUrl(urlPost);
  74. std::string expectedBody = "a1b2c3Post";
  75. session.SetBody(expectedBody);
  76. session.PreparePost();
  77. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  78. Response response = session.Complete(curl_result);
  79. EXPECT_EQ(expectedBody, response.text);
  80. EXPECT_EQ(urlPost, response.url);
  81. EXPECT_EQ(200, response.status_code);
  82. EXPECT_EQ(ErrorCode::OK, response.error.code);
  83. }
  84. {
  85. session.SetUrl(url);
  86. session.PrepareGet();
  87. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  88. Response response = session.Complete(curl_result);
  89. std::string expected_text{"Header reflect GET"};
  90. EXPECT_EQ(expected_text, response.text);
  91. EXPECT_EQ(url, response.url);
  92. EXPECT_EQ(200, response.status_code);
  93. EXPECT_EQ(ErrorCode::OK, response.error.code);
  94. }
  95. {
  96. session.SetUrl(urlPut);
  97. session.SetPayload({{"x", "5"}});
  98. session.PreparePut();
  99. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  100. Response response = session.Complete(curl_result);
  101. std::string expected_text{
  102. "{\n"
  103. " \"x\": 5\n"
  104. "}"};
  105. EXPECT_EQ(expected_text, response.text);
  106. EXPECT_EQ(urlPut, response.url);
  107. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  108. EXPECT_EQ(200, response.status_code);
  109. EXPECT_EQ(ErrorCode::OK, response.error.code);
  110. }
  111. {
  112. session.SetUrl(url);
  113. session.PrepareHead();
  114. CURLcode curl_result = curl_easy_perform(session.GetCurlHolder()->handle);
  115. Response response = session.Complete(curl_result);
  116. std::string expected_text{"Header reflect HEAD"};
  117. EXPECT_EQ(url, response.url);
  118. EXPECT_EQ(200, response.status_code);
  119. EXPECT_EQ(ErrorCode::OK, response.error.code);
  120. }
  121. }
  122. }
  123. int main(int argc, char** argv) {
  124. ::testing::InitGoogleTest(&argc, argv);
  125. ::testing::AddGlobalTestEnvironment(server);
  126. return RUN_ALL_TESTS();
  127. }