1
0

head_tests.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include <chrono>
  2. #include <gtest/gtest.h>
  3. #include <string>
  4. #include <cpr/cpr.h>
  5. #include "httpServer.hpp"
  6. using namespace cpr;
  7. static HttpServer* server = new HttpServer();
  8. TEST(HeadTests, BasicHeadTest) {
  9. Url url{server->GetBaseUrl() + "/hello.html"};
  10. Response response = cpr::Head(url);
  11. EXPECT_EQ(std::string{}, 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(HeadTests, ComplexHeadTest) {
  18. Url url{server->GetBaseUrl() + "/basic.json"};
  19. Response response = cpr::Head(url);
  20. EXPECT_EQ(std::string{}, response.text);
  21. EXPECT_EQ(url, response.url);
  22. EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
  23. EXPECT_EQ(200, response.status_code);
  24. EXPECT_EQ(ErrorCode::OK, response.error.code);
  25. }
  26. TEST(HeadTests, ResourceNotFoundHeadTest) {
  27. Url url{server->GetBaseUrl() + "/error.html"};
  28. Response response = cpr::Head(url);
  29. EXPECT_EQ(std::string{}, response.text);
  30. EXPECT_EQ(url, response.url);
  31. EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
  32. EXPECT_EQ(404, response.status_code);
  33. EXPECT_EQ(ErrorCode::OK, response.error.code);
  34. }
  35. TEST(HeadTests, BadHostHeadTest) {
  36. Url url{"http://bad_host/"};
  37. Response response = cpr::Head(url);
  38. EXPECT_EQ(std::string{}, response.text);
  39. EXPECT_EQ(url, response.url);
  40. EXPECT_EQ(0, response.status_code);
  41. EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
  42. }
  43. TEST(HeadTests, CookieHeadTest) {
  44. Url url{server->GetBaseUrl() + "/basic_cookies.html"};
  45. Response response = cpr::Head(url);
  46. cpr::Cookies expectedCookies{
  47. {"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  48. {"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
  49. };
  50. cpr::Cookies res_cookies{response.cookies};
  51. EXPECT_EQ(std::string{}, response.text);
  52. EXPECT_EQ(url, response.url);
  53. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  54. EXPECT_EQ(200, response.status_code);
  55. EXPECT_EQ(ErrorCode::OK, response.error.code);
  56. for (auto cookie = res_cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != res_cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
  57. EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
  58. EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
  59. EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
  60. EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
  61. EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
  62. EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
  63. EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
  64. }
  65. }
  66. TEST(HeadTests, ParameterHeadTest) {
  67. Url url{server->GetBaseUrl() + "/hello.html"};
  68. Parameters parameters{{"key", "value"}};
  69. Response response = cpr::Head(url, parameters);
  70. EXPECT_EQ(std::string{}, response.text);
  71. EXPECT_EQ(Url{url + "?key=value"}, response.url);
  72. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  73. EXPECT_EQ(200, response.status_code);
  74. EXPECT_EQ(ErrorCode::OK, response.error.code);
  75. }
  76. TEST(HeadTests, AuthenticationSuccessHeadTest) {
  77. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  78. Response response = cpr::Head(url, Authentication{"user", "password", AuthMode::BASIC});
  79. EXPECT_EQ(std::string{}, response.text);
  80. EXPECT_EQ(url, response.url);
  81. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  82. EXPECT_EQ(200, response.status_code);
  83. EXPECT_EQ(ErrorCode::OK, response.error.code);
  84. }
  85. TEST(HeadTests, AuthenticationNullFailureHeadTest) {
  86. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  87. Response response = cpr::Head(url);
  88. EXPECT_EQ(std::string{}, response.text);
  89. EXPECT_EQ(url, response.url);
  90. EXPECT_EQ("text/plain", response.header["content-type"]);
  91. EXPECT_EQ(401, response.status_code);
  92. EXPECT_EQ(ErrorCode::OK, response.error.code);
  93. }
  94. TEST(HeadTests, AuthenticationFailureHeadTest) {
  95. Url url{server->GetBaseUrl() + "/basic_auth.html"};
  96. Response response = cpr::Head(url, Authentication{"user", "bad_password", AuthMode::BASIC});
  97. EXPECT_EQ(std::string{}, response.text);
  98. EXPECT_EQ(url, response.url);
  99. EXPECT_EQ("text/plain", response.header["content-type"]);
  100. EXPECT_EQ(401, response.status_code);
  101. EXPECT_EQ(ErrorCode::OK, response.error.code);
  102. }
  103. TEST(HeadTests, BearerSuccessHeadTest) {
  104. Url url{server->GetBaseUrl() + "/bearer_token.html"};
  105. #if CPR_LIBCURL_VERSION_NUM >= 0x073D00
  106. Response response = cpr::Get(url, Bearer{"the_token"});
  107. #else
  108. Response response = cpr::Get(url, Header{{"Authorization", "Bearer the_token"}});
  109. #endif
  110. EXPECT_EQ(std::string{"Header reflect GET"}, response.text);
  111. EXPECT_EQ(url, response.url);
  112. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  113. EXPECT_EQ(200, response.status_code);
  114. EXPECT_EQ(ErrorCode::OK, response.error.code);
  115. }
  116. TEST(HeadTests, DigestSuccessHeadTest) {
  117. Url url{server->GetBaseUrl() + "/digest_auth.html"};
  118. Response response = cpr::Head(url, Authentication{"user", "password", AuthMode::DIGEST});
  119. EXPECT_EQ(std::string{}, response.text);
  120. EXPECT_EQ(url, response.url);
  121. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  122. EXPECT_EQ(200, response.status_code);
  123. EXPECT_EQ(ErrorCode::OK, response.error.code);
  124. }
  125. TEST(HeadTests, HeaderReflectNoneHeadTest) {
  126. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  127. Response response = cpr::Head(url);
  128. EXPECT_EQ(std::string{}, response.text);
  129. EXPECT_EQ(url, response.url);
  130. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  131. EXPECT_EQ(std::string{}, response.header["hello"]);
  132. EXPECT_EQ(200, response.status_code);
  133. EXPECT_EQ(ErrorCode::OK, response.error.code);
  134. }
  135. TEST(HeadTests, HeaderReflectEmptyHeadTest) {
  136. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  137. Response response = cpr::Head(url, Header{});
  138. EXPECT_EQ(std::string{}, response.text);
  139. EXPECT_EQ(url, response.url);
  140. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  141. EXPECT_EQ(std::string{}, response.header["hello"]);
  142. EXPECT_EQ(200, response.status_code);
  143. EXPECT_EQ(ErrorCode::OK, response.error.code);
  144. }
  145. TEST(HeadTests, HeaderReflectHeadTest) {
  146. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  147. Response response = cpr::Head(url, Header{{"hello", "world"}});
  148. EXPECT_EQ(std::string{}, response.text);
  149. EXPECT_EQ(url, response.url);
  150. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  151. EXPECT_EQ(std::string{"world"}, response.header["hello"]);
  152. EXPECT_EQ(200, response.status_code);
  153. EXPECT_EQ(ErrorCode::OK, response.error.code);
  154. }
  155. TEST(HeadTests, SetEmptyHeaderHeadTest) {
  156. Url url{server->GetBaseUrl() + "/header_reflect.html"};
  157. Response response = cpr::Head(url, Header{{"hello", ""}});
  158. EXPECT_EQ(std::string{}, response.text);
  159. EXPECT_EQ(url, response.url);
  160. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  161. EXPECT_EQ(std::string{}, response.header["hello"]);
  162. EXPECT_EQ(200, response.status_code);
  163. EXPECT_EQ(ErrorCode::OK, response.error.code);
  164. }
  165. TEST(HeadTests, RedirectHeadTest) {
  166. Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
  167. Response response = cpr::Head(url, Redirect(false));
  168. EXPECT_EQ(std::string{}, response.text);
  169. EXPECT_EQ(url, response.url);
  170. EXPECT_EQ(std::string{}, response.header["content-type"]);
  171. EXPECT_EQ(302, response.status_code);
  172. EXPECT_EQ(ErrorCode::OK, response.error.code);
  173. }
  174. TEST(HeadTests, ZeroMaxRedirectsHeadTest) {
  175. Url url{server->GetBaseUrl() + "/hello.html"};
  176. Response response = cpr::Head(url, Redirect(0L));
  177. EXPECT_EQ(std::string{}, response.text);
  178. EXPECT_EQ(url, response.url);
  179. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  180. EXPECT_EQ(200, response.status_code);
  181. EXPECT_EQ(ErrorCode::OK, response.error.code);
  182. }
  183. TEST(HeadTests, BasicHeadAsyncTest) {
  184. Url url{server->GetBaseUrl() + "/hello.html"};
  185. std::vector<AsyncResponse> responses;
  186. for (size_t i = 0; i < 10; ++i) {
  187. responses.emplace_back(cpr::HeadAsync(url));
  188. }
  189. for (cpr::AsyncResponse& future_response : responses) {
  190. cpr::Response response = future_response.get();
  191. EXPECT_EQ(std::string{}, response.text);
  192. EXPECT_EQ(url, response.url);
  193. EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
  194. EXPECT_EQ(200, response.status_code);
  195. EXPECT_EQ(ErrorCode::OK, response.error.code);
  196. }
  197. }
  198. int main(int argc, char** argv) {
  199. ::testing::InitGoogleTest(&argc, argv);
  200. ::testing::AddGlobalTestEnvironment(server);
  201. return RUN_ALL_TESTS();
  202. }