Fix client.cc code, since res.error() without operator overloading… (#921)

* Fix client.cc code, since res.error() without operator overloading causing error in Xcode

* Add unit test to check new error to string with operator overloading

* Add inline as requested in code review comment
This commit is contained in:
Alessio Pollero 2021-05-01 19:29:23 +02:00 committed by GitHub
parent c58b00580e
commit 2a70c45697
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View file

@ -804,6 +804,12 @@ enum class Error {
Compression, Compression,
}; };
inline std::ostream& operator << (std::ostream& os, const Error& obj)
{
os << static_cast<std::underlying_type<Error>::type>(obj);
return os;
}
class Result { class Result {
public: public:
Result(std::unique_ptr<Response> &&res, Error err, Result(std::unique_ptr<Response> &&res, Error err,

View file

@ -7,6 +7,7 @@
#include <future> #include <future>
#include <stdexcept> #include <stdexcept>
#include <thread> #include <thread>
#include <sstream>
#define SERVER_CERT_FILE "./cert.pem" #define SERVER_CERT_FILE "./cert.pem"
#define SERVER_CERT2_FILE "./cert2.pem" #define SERVER_CERT2_FILE "./cert2.pem"
@ -547,6 +548,23 @@ TEST(ConnectionErrorTest, InvalidHost2) {
EXPECT_EQ(Error::Connection, res.error()); EXPECT_EQ(Error::Connection, res.error());
} }
TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) {
auto host = "httpbin.org/";
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
SSLClient cli(host);
#else
Client cli(host);
#endif
cli.set_connection_timeout(std::chrono::seconds(2));
auto res = cli.Get("/");
ASSERT_TRUE(!res);
stringstream s;
s << "error code: " << res.error();
EXPECT_EQ("error code: 2", s.str());
}
TEST(ConnectionErrorTest, InvalidPort) { TEST(ConnectionErrorTest, InvalidPort) {
auto host = "localhost"; auto host = "localhost";
auto port = 44380; auto port = 44380;