Skip to content

Commit

Permalink
Fix client.cc code, since res.error() without operator overloading… (y…
Browse files Browse the repository at this point in the history
…hirose#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
  • Loading branch information
aleroot authored May 1, 2021
1 parent c58b005 commit 2a70c45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,12 @@ enum class Error {
Compression,
};

inline std::ostream& operator << (std::ostream& os, const Error& obj)
{
os << static_cast<std::underlying_type<Error>::type>(obj);
return os;
}

class Result {
public:
Result(std::unique_ptr<Response> &&res, Error err,
Expand Down
18 changes: 18 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <future>
#include <stdexcept>
#include <thread>
#include <sstream>

#define SERVER_CERT_FILE "./cert.pem"
#define SERVER_CERT2_FILE "./cert2.pem"
Expand Down Expand Up @@ -547,6 +548,23 @@ TEST(ConnectionErrorTest, InvalidHost2) {
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) {
auto host = "localhost";
auto port = 44380;
Expand Down

0 comments on commit 2a70c45

Please sign in to comment.