error_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <chrono>
4 #include <string>
5 
6 #include <cpr/cpr.h>
7 #include <curl/curl.h>
8 
9 #include "server.h"
10 
11 using namespace cpr;
12 
13 static Server* server = new Server();
14 auto base = server->GetBaseUrl();
15 auto baseSSL = server->GetBaseUrlSSL();
16 
17 TEST(ErrorTests, BasicSSLFailure) {
18  auto url = Url{baseSSL + "/hello.html"};
19  auto response = cpr::Get(url);
20  EXPECT_EQ(url, response.url);
21  EXPECT_EQ(0, response.status_code);
23  auto expected = ErrorCode::UNSUPPORTED_PROTOCOL;
24  if(curl_version->features & CURL_VERSION_SSL) {
26  }
27  EXPECT_EQ(expected, response.error.code);
28 
29 }
30 
31 TEST(ErrorTests, UnsupportedProtocolFailure) {
32  auto url = Url{"urk://wat.is.this"};
33  auto response = cpr::Get(url);
34  EXPECT_EQ(0, response.status_code);
35  EXPECT_EQ(ErrorCode::UNSUPPORTED_PROTOCOL, response.error.code);
36 }
37 
38 TEST(ErrorTests, InvalidURLFailure) {
39  auto url = Url{"???"};
40  auto response = cpr::Get(url);
41  EXPECT_EQ(0, response.status_code);
42  EXPECT_EQ(ErrorCode::INVALID_URL_FORMAT, response.error.code);
43 }
44 
45 TEST(ErrorTests, TimeoutFailure) {
46  auto url = Url{base + "/timeout.html"};
47  auto response = cpr::Get(url, cpr::Timeout{1});
48  EXPECT_EQ(0, response.status_code);
49  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
50 }
51 
52 TEST(ErrorTests, ChronoTimeoutFailure) {
53  auto url = Url{base + "/timeout.html"};
54  auto response = cpr::Get(url, cpr::Timeout{std::chrono::milliseconds{1}});
55  EXPECT_EQ(0, response.status_code);
56  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
57 }
58 
59 TEST(ErrorTests, ConnectTimeoutFailure) {
60  auto url = Url{"http://localhost:67"};
61  auto response = cpr::Get(url, cpr::ConnectTimeout{1});
62  EXPECT_EQ(0, response.status_code);
63  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
64 }
65 
66 TEST(ErrorTests, ChronoConnectTimeoutFailure) {
67  auto url = Url{"http://localhost:67"};
68  auto response = cpr::Get(url, cpr::ConnectTimeout{std::chrono::milliseconds{1}});
69  EXPECT_EQ(0, response.status_code);
70  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
71 }
72 
73 TEST(ErrorTests, LowSpeedTimeFailure) {
74  auto url = Url{base + "/low_speed.html"};
75  auto response = cpr::Get(url, cpr::LowSpeed{1000, 1});
76  EXPECT_EQ(0, response.status_code);
77  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
78 }
79 
80 TEST(ErrorTests, LowSpeedBytesFailure) {
81  auto url = Url{base + "/low_speed_bytes.html"};
82  auto response = cpr::Get(url, cpr::LowSpeed{1000, 1});
83  EXPECT_EQ(0, response.status_code);
84  EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
85 }
86 
87 TEST(ErrorTests, ProxyFailure) {
88  auto url = Url{base + "/hello.html"};
89  auto response = cpr::Get(url, cpr::Proxies{{"http", "http://bad_host/"}});
90  EXPECT_EQ(url, response.url);
91  EXPECT_EQ(0, response.status_code);
92  EXPECT_EQ(ErrorCode::PROXY_RESOLUTION_FAILURE, response.error.code);
93 }
94 
95 TEST(ErrorTests, BoolFalseTest) {
96  Error error;
97  EXPECT_FALSE(error);
98 }
99 
100 TEST(ErrorTests, BoolTrueTest) {
101  Error error;
103  EXPECT_TRUE(error);
104 }
105 
106 int main(int argc, char** argv) {
107  ::testing::InitGoogleTest(&argc, argv);
109  return RUN_ALL_TESTS();
110 }
Url GetBaseUrlSSL()
Definition: server.cpp:629
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1342
CURL_EXTERN curl_version_info_data * curl_version_info(CURLversion)
Definition: version.c:342
Definition: server.h:12
#define CURL_VERSION_SSL
Definition: curl.h:2636
std::string Url
Definition: cprtypes.h:14
ErrorCode code
Definition: error.h:44
TEST(ErrorTests, BasicSSLFailure)
Definition: error_tests.cpp:17
auto baseSSL
Definition: error_tests.cpp:15
auto base
Definition: error_tests.cpp:14
Url GetBaseUrl()
Definition: server.cpp:625
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
Response Get(Ts &&...ts)
Definition: api.h:38
Definition: auth.cpp:3
#define CURLVERSION_NOW
Definition: curl.h:2603
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
#define EXPECT_TRUE(condition)
Definition: gtest.h:1952
int main(int argc, char **argv)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1955
static Server * server
Definition: error_tests.cpp:13
CURL_EXTERN char * curl_version(void)
Definition: version.c:77


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:09