From 85b5cdd78d9b0dd14e35d5eaf88c39055e191928 Mon Sep 17 00:00:00 2001 From: Florian Albrechtskirchinger Date: Thu, 6 Mar 2025 17:58:55 +0100 Subject: [PATCH] Move detail::read_file() to test/test.cc (#2092) The unit test code is the only user of the function. read_file() now throws an exception if the file isn't found. --- httplib.h | 12 ------------ test/test.cc | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/httplib.h b/httplib.h index 60da33f..3b3beab 100644 --- a/httplib.h +++ b/httplib.h @@ -240,7 +240,6 @@ using socket_t = int; #include #include #include -#include #include #include #include @@ -2387,8 +2386,6 @@ std::string encode_query_param(const std::string &value); std::string decode_url(const std::string &s, bool convert_plus_to_space); -void read_file(const std::string &path, std::string &out); - std::string trim_copy(const std::string &s); void divide( @@ -2916,15 +2913,6 @@ inline std::string decode_url(const std::string &s, return result; } -inline void read_file(const std::string &path, std::string &out) { - std::ifstream fs(path, std::ios_base::binary); - fs.seekg(0, std::ios_base::end); - auto size = fs.tellg(); - fs.seekg(0); - out.resize(static_cast(size)); - fs.read(&out[0], static_cast(size)); -} - inline std::string file_extension(const std::string &path) { std::smatch m; static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); diff --git a/test/test.cc b/test/test.cc index 71c311b..fce0545 100644 --- a/test/test.cc +++ b/test/test.cc @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -59,6 +60,16 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, #endif } +static void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + if (!fs) throw std::runtime_error("File not found: " + path); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + #ifndef _WIN32 class UnixSocketTest : public ::testing::Test { protected: @@ -729,7 +740,7 @@ TEST(ChunkedEncodingTest, FromHTTPWatch_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, res->body); @@ -782,7 +793,7 @@ TEST(ChunkedEncodingTest, WithContentReceiver_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); @@ -814,7 +825,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) { ASSERT_TRUE(res); std::string out; - detail::read_file("./image.jpg", out); + read_file("./image.jpg", out); EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(out, body); @@ -6176,7 +6187,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) { TEST(SSLClientTest, ServerCertificateVerification5_Online) { std::string cert; - detail::read_file(CA_CERT_FILE, cert); + read_file(CA_CERT_FILE, cert); SSLClient cli("google.com"); cli.load_ca_cert_store(cert.data(), cert.size());