Skip to content

Commit 85b5cdd

Browse files
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.
1 parent f2928d7 commit 85b5cdd

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

httplib.h

-12
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ using socket_t = int;
240240
#include <errno.h>
241241
#include <exception>
242242
#include <fcntl.h>
243-
#include <fstream>
244243
#include <functional>
245244
#include <iomanip>
246245
#include <iostream>
@@ -2387,8 +2386,6 @@ std::string encode_query_param(const std::string &value);
23872386

23882387
std::string decode_url(const std::string &s, bool convert_plus_to_space);
23892388

2390-
void read_file(const std::string &path, std::string &out);
2391-
23922389
std::string trim_copy(const std::string &s);
23932390

23942391
void divide(
@@ -2916,15 +2913,6 @@ inline std::string decode_url(const std::string &s,
29162913
return result;
29172914
}
29182915

2919-
inline void read_file(const std::string &path, std::string &out) {
2920-
std::ifstream fs(path, std::ios_base::binary);
2921-
fs.seekg(0, std::ios_base::end);
2922-
auto size = fs.tellg();
2923-
fs.seekg(0);
2924-
out.resize(static_cast<size_t>(size));
2925-
fs.read(&out[0], static_cast<std::streamsize>(size));
2926-
}
2927-
29282916
inline std::string file_extension(const std::string &path) {
29292917
std::smatch m;
29302918
static auto re = std::regex("\\.([a-zA-Z0-9]+)$");

test/test.cc

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <atomic>
1111
#include <chrono>
12+
#include <fstream>
1213
#include <future>
1314
#include <limits>
1415
#include <memory>
@@ -59,6 +60,16 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files,
5960
#endif
6061
}
6162

63+
static void read_file(const std::string &path, std::string &out) {
64+
std::ifstream fs(path, std::ios_base::binary);
65+
if (!fs) throw std::runtime_error("File not found: " + path);
66+
fs.seekg(0, std::ios_base::end);
67+
auto size = fs.tellg();
68+
fs.seekg(0);
69+
out.resize(static_cast<size_t>(size));
70+
fs.read(&out[0], static_cast<std::streamsize>(size));
71+
}
72+
6273
#ifndef _WIN32
6374
class UnixSocketTest : public ::testing::Test {
6475
protected:
@@ -729,7 +740,7 @@ TEST(ChunkedEncodingTest, FromHTTPWatch_Online) {
729740
ASSERT_TRUE(res);
730741

731742
std::string out;
732-
detail::read_file("./image.jpg", out);
743+
read_file("./image.jpg", out);
733744

734745
EXPECT_EQ(StatusCode::OK_200, res->status);
735746
EXPECT_EQ(out, res->body);
@@ -782,7 +793,7 @@ TEST(ChunkedEncodingTest, WithContentReceiver_Online) {
782793
ASSERT_TRUE(res);
783794

784795
std::string out;
785-
detail::read_file("./image.jpg", out);
796+
read_file("./image.jpg", out);
786797

787798
EXPECT_EQ(StatusCode::OK_200, res->status);
788799
EXPECT_EQ(out, body);
@@ -814,7 +825,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
814825
ASSERT_TRUE(res);
815826

816827
std::string out;
817-
detail::read_file("./image.jpg", out);
828+
read_file("./image.jpg", out);
818829

819830
EXPECT_EQ(StatusCode::OK_200, res->status);
820831
EXPECT_EQ(out, body);
@@ -6176,7 +6187,7 @@ TEST(SSLClientTest, ServerCertificateVerification4) {
61766187

61776188
TEST(SSLClientTest, ServerCertificateVerification5_Online) {
61786189
std::string cert;
6179-
detail::read_file(CA_CERT_FILE, cert);
6190+
read_file(CA_CERT_FILE, cert);
61806191

61816192
SSLClient cli("google.com");
61826193
cli.load_ca_cert_store(cert.data(), cert.size());

0 commit comments

Comments
 (0)