Create TemporaryFileHandle class
TemporaryFileHandle class is used to create temp files in the
filesystem, and hold a handle to them until the class goes out of scope,
at which time they can be removed. It replaces makeFile(), which was
not RAII safe if an exception gets thrown, and could potentially leave
files in the filesystem if the tests fail.
Tested: Unit tests pass
Change-Id: I03eb0d342a6cd7b78115a8c42be9175f30c4ccd0
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/test/http/http_body_test.cpp b/test/http/http_body_test.cpp
index 0983c1d..d8c9dd1 100644
--- a/test/http/http_body_test.cpp
+++ b/test/http/http_body_test.cpp
@@ -67,9 +67,10 @@
TEST(HttpHttpBodyValueType, MoveFile)
{
HttpBody::value_type value(EncodingType::Base64);
- std::string filepath = makeFile("teststring");
+ TemporaryFileHandle temporaryFile("teststring");
boost::system::error_code ec;
- value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
+ value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
+ ec);
ASSERT_FALSE(ec);
// Move constructor
HttpBody::value_type value2(std::move(value));
@@ -90,9 +91,10 @@
TEST(HttpHttpBodyValueType, MoveOperatorFile)
{
HttpBody::value_type value(EncodingType::Base64);
- std::string filepath = makeFile("teststring");
+ TemporaryFileHandle temporaryFile("teststring");
boost::system::error_code ec;
- value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
+ value.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
+ ec);
ASSERT_FALSE(ec);
// Move constructor
HttpBody::value_type value2 = std::move(value);
@@ -109,13 +111,12 @@
EXPECT_EQ(value2.payloadSize(), 16);
}
-TEST(HttpBodyValueType, SetFd)
+TEST(HttpFileBodyValueType, SetFd)
{
HttpBody::value_type value(EncodingType::Base64);
- std::string filepath = makeFile("teststring");
-
+ TemporaryFileHandle temporaryFile("teststring");
boost::system::error_code ec;
- value.setFd(fileno(fopen(filepath.c_str(), "r")), ec);
+ value.setFd(fileno(fopen(temporaryFile.stringPath.c_str(), "r")), ec);
ASSERT_FALSE(ec);
std::array<char, 4096> buffer{};