Skip to content

Commit 2a70c45

Browse files
authored
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
1 parent c58b005 commit 2a70c45

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

httplib.h

+6
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,12 @@ enum class Error {
804804
Compression,
805805
};
806806

807+
inline std::ostream& operator << (std::ostream& os, const Error& obj)
808+
{
809+
os << static_cast<std::underlying_type<Error>::type>(obj);
810+
return os;
811+
}
812+
807813
class Result {
808814
public:
809815
Result(std::unique_ptr<Response> &&res, Error err,

test/test.cc

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <future>
88
#include <stdexcept>
99
#include <thread>
10+
#include <sstream>
1011

1112
#define SERVER_CERT_FILE "./cert.pem"
1213
#define SERVER_CERT2_FILE "./cert2.pem"
@@ -547,6 +548,23 @@ TEST(ConnectionErrorTest, InvalidHost2) {
547548
EXPECT_EQ(Error::Connection, res.error());
548549
}
549550

551+
TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) {
552+
auto host = "httpbin.org/";
553+
554+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
555+
SSLClient cli(host);
556+
#else
557+
Client cli(host);
558+
#endif
559+
cli.set_connection_timeout(std::chrono::seconds(2));
560+
561+
auto res = cli.Get("/");
562+
ASSERT_TRUE(!res);
563+
stringstream s;
564+
s << "error code: " << res.error();
565+
EXPECT_EQ("error code: 2", s.str());
566+
}
567+
550568
TEST(ConnectionErrorTest, InvalidPort) {
551569
auto host = "localhost";
552570
auto port = 44380;

0 commit comments

Comments
 (0)