1
0
mirror of synced 2025-04-19 00:24:02 +03:00

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.
This commit is contained in:
Florian Albrechtskirchinger 2025-03-06 17:58:55 +01:00 committed by GitHub
parent f2928d7152
commit 85b5cdd78d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 16 deletions

View File

@ -240,7 +240,6 @@ using socket_t = int;
#include <errno.h> #include <errno.h>
#include <exception> #include <exception>
#include <fcntl.h> #include <fcntl.h>
#include <fstream>
#include <functional> #include <functional>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -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); 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); std::string trim_copy(const std::string &s);
void divide( void divide(
@ -2916,15 +2913,6 @@ inline std::string decode_url(const std::string &s,
return result; 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_t>(size));
fs.read(&out[0], static_cast<std::streamsize>(size));
}
inline std::string file_extension(const std::string &path) { inline std::string file_extension(const std::string &path) {
std::smatch m; std::smatch m;
static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); static auto re = std::regex("\\.([a-zA-Z0-9]+)$");

View File

@ -9,6 +9,7 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <fstream>
#include <future> #include <future>
#include <limits> #include <limits>
#include <memory> #include <memory>
@ -59,6 +60,16 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files,
#endif #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_t>(size));
fs.read(&out[0], static_cast<std::streamsize>(size));
}
#ifndef _WIN32 #ifndef _WIN32
class UnixSocketTest : public ::testing::Test { class UnixSocketTest : public ::testing::Test {
protected: protected:
@ -729,7 +740,7 @@ TEST(ChunkedEncodingTest, FromHTTPWatch_Online) {
ASSERT_TRUE(res); ASSERT_TRUE(res);
std::string out; std::string out;
detail::read_file("./image.jpg", out); read_file("./image.jpg", out);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ(out, res->body); EXPECT_EQ(out, res->body);
@ -782,7 +793,7 @@ TEST(ChunkedEncodingTest, WithContentReceiver_Online) {
ASSERT_TRUE(res); ASSERT_TRUE(res);
std::string out; std::string out;
detail::read_file("./image.jpg", out); read_file("./image.jpg", out);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ(out, body); EXPECT_EQ(out, body);
@ -814,7 +825,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
ASSERT_TRUE(res); ASSERT_TRUE(res);
std::string out; std::string out;
detail::read_file("./image.jpg", out); read_file("./image.jpg", out);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ(out, body); EXPECT_EQ(out, body);
@ -6176,7 +6187,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) {
TEST(SSLClientTest, ServerCertificateVerification5_Online) { TEST(SSLClientTest, ServerCertificateVerification5_Online) {
std::string cert; std::string cert;
detail::read_file(CA_CERT_FILE, cert); read_file(CA_CERT_FILE, cert);
SSLClient cli("google.com"); SSLClient cli("google.com");
cli.load_ca_cert_store(cert.data(), cert.size()); cli.load_ca_cert_store(cert.data(), cert.size());