From 2a70c45697822621c4c50b5e26834d42ed41df01 Mon Sep 17 00:00:00 2001 From: Alessio Pollero Date: Sat, 1 May 2021 19:29:23 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20client.cc=20code,=20since=20res.error()?= =?UTF-8?q?=20without=20operator=20overloading=E2=80=A6=20(#921)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- httplib.h | 6 ++++++ test/test.cc | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/httplib.h b/httplib.h index fff0bc4..0c2fc32 100644 --- a/httplib.h +++ b/httplib.h @@ -804,6 +804,12 @@ enum class Error { Compression, }; +inline std::ostream& operator << (std::ostream& os, const Error& obj) +{ + os << static_cast::type>(obj); + return os; +} + class Result { public: Result(std::unique_ptr &&res, Error err, diff --git a/test/test.cc b/test/test.cc index df06444..ba3c14d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7,6 +7,7 @@ #include #include #include +#include #define SERVER_CERT_FILE "./cert.pem" #define SERVER_CERT2_FILE "./cert2.pem" @@ -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;