error_tests.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <gtest/gtest.h>
  2. #include <chrono>
  3. #include <string>
  4. #include <cpr/cpr.h>
  5. #include <curl/curl.h>
  6. #include "httpServer.hpp"
  7. #include "httpsServer.hpp"
  8. using namespace cpr;
  9. static HttpServer* server = new HttpServer();
  10. TEST(ErrorTests, UnsupportedProtocolFailure) {
  11. Url url{"urk://wat.is.this"};
  12. Response response = cpr::Get(url);
  13. EXPECT_EQ(0, response.status_code);
  14. EXPECT_EQ(ErrorCode::UNSUPPORTED_PROTOCOL, response.error.code);
  15. }
  16. TEST(ErrorTests, InvalidURLFailure) {
  17. Url url{"???"};
  18. Response response = cpr::Get(url);
  19. EXPECT_EQ(0, response.status_code);
  20. EXPECT_EQ(ErrorCode::INVALID_URL_FORMAT, response.error.code);
  21. }
  22. TEST(ErrorTests, TimeoutFailure) {
  23. Url url{server->GetBaseUrl() + "/timeout.html"};
  24. Response response = cpr::Get(url, cpr::Timeout{1});
  25. EXPECT_EQ(0, response.status_code);
  26. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  27. }
  28. TEST(ErrorTests, ChronoTimeoutFailure) {
  29. Url url{server->GetBaseUrl() + "/timeout.html"};
  30. Response response = cpr::Get(url, cpr::Timeout{std::chrono::milliseconds{1}});
  31. EXPECT_EQ(0, response.status_code);
  32. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  33. }
  34. TEST(ErrorTests, ConnectTimeoutFailure) {
  35. Url url{"http://localhost:67"};
  36. Response response = cpr::Get(url, cpr::ConnectTimeout{1});
  37. EXPECT_EQ(0, response.status_code);
  38. // Sometimes a CONNECTION_FAILURE happens before the OPERATION_TIMEDOUT:
  39. EXPECT_TRUE(response.error.code == ErrorCode::OPERATION_TIMEDOUT || response.error.code == ErrorCode::CONNECTION_FAILURE);
  40. }
  41. TEST(ErrorTests, ChronoConnectTimeoutFailure) {
  42. Url url{"http://localhost:67"};
  43. Response response = cpr::Get(url, cpr::ConnectTimeout{std::chrono::milliseconds{1}});
  44. EXPECT_EQ(0, response.status_code);
  45. // Sometimes a CONNECTION_FAILURE happens before the OPERATION_TIMEDOUT:
  46. EXPECT_TRUE(response.error.code == ErrorCode::OPERATION_TIMEDOUT || response.error.code == ErrorCode::CONNECTION_FAILURE);
  47. }
  48. TEST(ErrorTests, LowSpeedTimeFailure) {
  49. Url url{server->GetBaseUrl() + "/low_speed.html"};
  50. Response response = cpr::Get(url, cpr::LowSpeed{1000, 1});
  51. // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
  52. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  53. }
  54. TEST(ErrorTests, LowSpeedBytesFailure) {
  55. Url url{server->GetBaseUrl() + "/low_speed_bytes.html"};
  56. Response response = cpr::Get(url, cpr::LowSpeed{1000, 1});
  57. // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
  58. EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
  59. }
  60. TEST(ErrorTests, ProxyFailure) {
  61. Url url{server->GetBaseUrl() + "/hello.html"};
  62. Response response = cpr::Get(url, cpr::Proxies{{"http", "http://bad_host/"}});
  63. EXPECT_EQ(url, response.url);
  64. EXPECT_EQ(0, response.status_code);
  65. EXPECT_EQ(ErrorCode::PROXY_RESOLUTION_FAILURE, response.error.code);
  66. }
  67. TEST(ErrorTests, BoolFalseTest) {
  68. Error error;
  69. EXPECT_FALSE(error);
  70. }
  71. TEST(ErrorTests, BoolTrueTest) {
  72. Error error;
  73. error.code = ErrorCode::UNSUPPORTED_PROTOCOL;
  74. EXPECT_TRUE(error);
  75. }
  76. int main(int argc, char** argv) {
  77. ::testing::InitGoogleTest(&argc, argv);
  78. ::testing::AddGlobalTestEnvironment(server);
  79. return RUN_ALL_TESTS();
  80. }