alternating_tests.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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(AlternatingTests, PutGetTest) {
  8. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  9. Session session;
  10. session.SetUrl(url);
  11. {
  12. Payload payload{{"x", "5"}};
  13. Response response = cpr::Put(url, payload);
  14. std::string expected_text{"Header reflect PUT"};
  15. EXPECT_EQ(expected_text, response.text);
  16. EXPECT_EQ(url, response.url);
  17. EXPECT_EQ(200, response.status_code);
  18. EXPECT_EQ(ErrorCode::OK, response.error.code);
  19. }
  20. {
  21. Response response = cpr::Get(url);
  22. std::string expected_text{"Header reflect GET"};
  23. EXPECT_EQ(expected_text, response.text);
  24. EXPECT_EQ(url, response.url);
  25. EXPECT_EQ(200, response.status_code);
  26. EXPECT_EQ(ErrorCode::OK, response.error.code);
  27. }
  28. }
  29. TEST(AlternatingTests, PutGetPutGetTest) {
  30. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  31. Session session;
  32. session.SetUrl(url);
  33. {
  34. Payload payload{{"x", "5"}};
  35. Response response = cpr::Put(url, payload);
  36. std::string expected_text{"Header reflect PUT"};
  37. EXPECT_EQ(expected_text, response.text);
  38. EXPECT_EQ(url, response.url);
  39. EXPECT_EQ(200, response.status_code);
  40. EXPECT_EQ(ErrorCode::OK, response.error.code);
  41. }
  42. {
  43. Response response = cpr::Get(url);
  44. std::string expected_text{"Header reflect GET"};
  45. EXPECT_EQ(expected_text, response.text);
  46. EXPECT_EQ(url, response.url);
  47. EXPECT_EQ(200, response.status_code);
  48. EXPECT_EQ(ErrorCode::OK, response.error.code);
  49. }
  50. {
  51. Payload payload{{"x", "5"}};
  52. Response response = cpr::Put(url, payload);
  53. std::string expected_text{"Header reflect PUT"};
  54. EXPECT_EQ(expected_text, response.text);
  55. EXPECT_EQ(url, response.url);
  56. EXPECT_EQ(200, response.status_code);
  57. EXPECT_EQ(ErrorCode::OK, response.error.code);
  58. }
  59. {
  60. Response response = cpr::Get(url);
  61. std::string expected_text{"Header reflect GET"};
  62. EXPECT_EQ(expected_text, response.text);
  63. EXPECT_EQ(url, response.url);
  64. EXPECT_EQ(200, response.status_code);
  65. EXPECT_EQ(ErrorCode::OK, response.error.code);
  66. }
  67. }
  68. TEST(AlternatingTests, HeadGetTest) {
  69. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  70. Session session;
  71. session.SetUrl(url);
  72. {
  73. // Head shouldn't return a body
  74. Response response = cpr::Head(url);
  75. std::string expected_text{""};
  76. EXPECT_EQ(expected_text, response.text);
  77. EXPECT_EQ(url, response.url);
  78. EXPECT_EQ(200, response.status_code);
  79. EXPECT_EQ(ErrorCode::OK, response.error.code);
  80. }
  81. {
  82. Response response = cpr::Get(url);
  83. std::string expected_text{"Header reflect GET"};
  84. EXPECT_EQ(expected_text, response.text);
  85. EXPECT_EQ(url, response.url);
  86. EXPECT_EQ(200, response.status_code);
  87. EXPECT_EQ(ErrorCode::OK, response.error.code);
  88. }
  89. }
  90. TEST(AlternatingTests, PutHeadTest) {
  91. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  92. Session session;
  93. session.SetUrl(url);
  94. {
  95. Payload payload{{"x", "5"}};
  96. Response response = cpr::Put(url, payload);
  97. std::string expected_text{"Header reflect PUT"};
  98. EXPECT_EQ(expected_text, response.text);
  99. EXPECT_EQ(url, response.url);
  100. EXPECT_EQ(200, response.status_code);
  101. EXPECT_EQ(ErrorCode::OK, response.error.code);
  102. }
  103. {
  104. // Head shouldn't return a body
  105. Response response = cpr::Head(url);
  106. std::string expected_text{""};
  107. EXPECT_EQ(expected_text, response.text);
  108. EXPECT_EQ(url, response.url);
  109. EXPECT_EQ(200, response.status_code);
  110. EXPECT_EQ(ErrorCode::OK, response.error.code);
  111. }
  112. }
  113. TEST(AlternatingTests, PutPostTest) {
  114. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  115. Session session;
  116. session.SetUrl(url);
  117. {
  118. Payload payload{{"x", "5"}};
  119. Response response = cpr::Put(url, payload);
  120. std::string expected_text{"Header reflect PUT"};
  121. EXPECT_EQ(expected_text, response.text);
  122. EXPECT_EQ(url, response.url);
  123. EXPECT_EQ(200, response.status_code);
  124. EXPECT_EQ(ErrorCode::OK, response.error.code);
  125. }
  126. {
  127. Payload payload{{"x", "5"}};
  128. Response response = cpr::Post(url, payload);
  129. std::string expected_text{"Header reflect POST"};
  130. EXPECT_EQ(expected_text, response.text);
  131. EXPECT_EQ(url, response.url);
  132. EXPECT_EQ(200, response.status_code);
  133. EXPECT_EQ(ErrorCode::OK, response.error.code);
  134. }
  135. }
  136. int main(int argc, char** argv) {
  137. ::testing::InitGoogleTest(&argc, argv);
  138. ::testing::AddGlobalTestEnvironment(server);
  139. return RUN_ALL_TESTS();
  140. }