summaryrefslogtreecommitdiff
path: root/test/http/http_body_test.cpp
diff options
context:
space:
mode:
authorEd Tanous <ed@tanous.net>2024-02-01 02:25:47 +0300
committerEd Tanous <ed@tanous.net>2024-03-19 03:27:05 +0300
commitb2896149c39967dd9d1ee79357bdc53537cfabd7 (patch)
treed63dd043c5219c7109e403189d12801a0a722e24 /test/http/http_body_test.cpp
parentc51afd54a55d5c8d6cb6e9583e209788f7996fe3 (diff)
downloadbmcweb-b2896149c39967dd9d1ee79357bdc53537cfabd7.tar.xz
Rename FileBody to HttpBody
Now that our custom body type does things more than files, it makes sense to rename it. This commit renames the header itself, then all instances of the class. Tested: Basic GET requests succeed. Change-Id: If4361ac8992fc7c268f48a336707f96e68d3576c Signed-off-by: Ed Tanous <ed@tanous.net>
Diffstat (limited to 'test/http/http_body_test.cpp')
-rw-r--r--test/http/http_body_test.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/test/http/http_body_test.cpp b/test/http/http_body_test.cpp
new file mode 100644
index 0000000000..6367cf9409
--- /dev/null
+++ b/test/http/http_body_test.cpp
@@ -0,0 +1,128 @@
+#include "file_test_utilities.hpp"
+#include "http_body.hpp"
+
+#include <boost/system/error_code.hpp>
+
+#include <array>
+#include <span>
+#include <string>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using ::testing::ElementsAre;
+
+namespace bmcweb
+{
+namespace
+{
+
+TEST(HttpHttpBodyValueType, MoveString)
+{
+ HttpBody::value_type value("teststring");
+ // Move constructor
+ HttpBody::value_type value2(std::move(value));
+ EXPECT_EQ(value2.encodingType, EncodingType::Raw);
+ EXPECT_EQ(value2.str(), "teststring");
+ EXPECT_EQ(value2.payloadSize(), 10);
+}
+
+TEST(HttpHttpBodyValueType, MoveOperatorString)
+{
+ HttpBody::value_type value;
+ value.str() = "teststring";
+ // Move constructor
+ HttpBody::value_type value2 = std::move(value);
+ EXPECT_EQ(value2.encodingType, EncodingType::Raw);
+ EXPECT_EQ(value2.str(), "teststring");
+ EXPECT_EQ(value2.payloadSize(), 10);
+}
+
+TEST(HttpHttpBodyValueType, copysignl)
+{
+ HttpBody::value_type value;
+ value.str() = "teststring";
+ // Move constructor
+ HttpBody::value_type value2(value);
+ EXPECT_EQ(value2.encodingType, EncodingType::Raw);
+ EXPECT_EQ(value2.str(), "teststring");
+ EXPECT_EQ(value2.payloadSize(), 10);
+}
+
+TEST(HttpHttpBodyValueType, CopyOperatorString)
+{
+ HttpBody::value_type value;
+ value.str() = "teststring";
+ // Move constructor
+ HttpBody::value_type value2 = value;
+ EXPECT_EQ(value2.encodingType, EncodingType::Raw);
+ EXPECT_EQ(value2.str(), "teststring");
+ EXPECT_EQ(value2.payloadSize(), 10);
+}
+
+TEST(HttpHttpBodyValueType, MoveFile)
+{
+ HttpBody::value_type value(EncodingType::Base64);
+ std::string filepath = makeFile("teststring");
+ boost::system::error_code ec;
+ value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
+ ASSERT_FALSE(ec);
+ // Move constructor
+ HttpBody::value_type value2(std::move(value));
+ std::array<char, 11> buffer{};
+ size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
+ ASSERT_FALSE(ec);
+ EXPECT_EQ(value2.encodingType, EncodingType::Base64);
+
+ EXPECT_THAT(std::span(buffer.data(), out),
+ ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
+
+ EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
+ 'g', '\0'));
+
+ EXPECT_EQ(value2.payloadSize(), 16);
+}
+
+TEST(HttpHttpBodyValueType, MoveOperatorFile)
+{
+ HttpBody::value_type value(EncodingType::Base64);
+ std::string filepath = makeFile("teststring");
+ boost::system::error_code ec;
+ value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
+ ASSERT_FALSE(ec);
+ // Move constructor
+ HttpBody::value_type value2 = std::move(value);
+ std::array<char, 11> buffer{};
+ size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
+ ASSERT_FALSE(ec);
+ EXPECT_EQ(value2.encodingType, EncodingType::Base64);
+
+ EXPECT_THAT(std::span(buffer.data(), out),
+ ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
+ EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
+ 'g', '\0'));
+
+ EXPECT_EQ(value2.payloadSize(), 16);
+}
+
+TEST(HttpBodyValueType, SetFd)
+{
+ HttpBody::value_type value(EncodingType::Base64);
+ std::string filepath = makeFile("teststring");
+
+ boost::system::error_code ec;
+ value.setFd(fileno(fopen(filepath.c_str(), "r")), ec);
+ ASSERT_FALSE(ec);
+
+ std::array<char, 4096> buffer{};
+
+ size_t out = value.file().read(buffer.data(), buffer.size(), ec);
+ ASSERT_FALSE(ec);
+
+ EXPECT_THAT(std::span(buffer.data(), out),
+ ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
+ EXPECT_EQ(value.payloadSize(), 16);
+}
+
+} // namespace
+} // namespace bmcweb