blob: 6367cf9409d79959182b53959135f1e86bb98b25 [file] [log] [blame]
Ed Tanous7a6f0032024-01-28 09:49:03 -08001#include "file_test_utilities.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08002#include "http_body.hpp"
Ed Tanous7a6f0032024-01-28 09:49:03 -08003
4#include <boost/system/error_code.hpp>
5
6#include <array>
7#include <span>
8#include <string>
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13using ::testing::ElementsAre;
14
15namespace bmcweb
16{
17namespace
18{
19
Ed Tanousb2896142024-01-31 15:25:47 -080020TEST(HttpHttpBodyValueType, MoveString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080021{
Ed Tanousb2896142024-01-31 15:25:47 -080022 HttpBody::value_type value("teststring");
Ed Tanous7a6f0032024-01-28 09:49:03 -080023 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080024 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080025 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
26 EXPECT_EQ(value2.str(), "teststring");
27 EXPECT_EQ(value2.payloadSize(), 10);
28}
29
Ed Tanousb2896142024-01-31 15:25:47 -080030TEST(HttpHttpBodyValueType, MoveOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080031{
Ed Tanousb2896142024-01-31 15:25:47 -080032 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080033 value.str() = "teststring";
34 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080035 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080036 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
37 EXPECT_EQ(value2.str(), "teststring");
38 EXPECT_EQ(value2.payloadSize(), 10);
39}
40
Ed Tanousb2896142024-01-31 15:25:47 -080041TEST(HttpHttpBodyValueType, copysignl)
Ed Tanous7a6f0032024-01-28 09:49:03 -080042{
Ed Tanousb2896142024-01-31 15:25:47 -080043 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080044 value.str() = "teststring";
45 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080046 HttpBody::value_type value2(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080047 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
48 EXPECT_EQ(value2.str(), "teststring");
49 EXPECT_EQ(value2.payloadSize(), 10);
50}
51
Ed Tanousb2896142024-01-31 15:25:47 -080052TEST(HttpHttpBodyValueType, CopyOperatorString)
Ed Tanous7a6f0032024-01-28 09:49:03 -080053{
Ed Tanousb2896142024-01-31 15:25:47 -080054 HttpBody::value_type value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080055 value.str() = "teststring";
56 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080057 HttpBody::value_type value2 = value;
Ed Tanous7a6f0032024-01-28 09:49:03 -080058 EXPECT_EQ(value2.encodingType, EncodingType::Raw);
59 EXPECT_EQ(value2.str(), "teststring");
60 EXPECT_EQ(value2.payloadSize(), 10);
61}
62
Ed Tanousb2896142024-01-31 15:25:47 -080063TEST(HttpHttpBodyValueType, MoveFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080064{
Ed Tanousb2896142024-01-31 15:25:47 -080065 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous7a6f0032024-01-28 09:49:03 -080066 std::string filepath = makeFile("teststring");
67 boost::system::error_code ec;
68 value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
69 ASSERT_FALSE(ec);
70 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080071 HttpBody::value_type value2(std::move(value));
Ed Tanous7a6f0032024-01-28 09:49:03 -080072 std::array<char, 11> buffer{};
73 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
74 ASSERT_FALSE(ec);
75 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
76
77 EXPECT_THAT(std::span(buffer.data(), out),
78 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
79
80 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
81 'g', '\0'));
82
83 EXPECT_EQ(value2.payloadSize(), 16);
84}
85
Ed Tanousb2896142024-01-31 15:25:47 -080086TEST(HttpHttpBodyValueType, MoveOperatorFile)
Ed Tanous7a6f0032024-01-28 09:49:03 -080087{
Ed Tanousb2896142024-01-31 15:25:47 -080088 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous7a6f0032024-01-28 09:49:03 -080089 std::string filepath = makeFile("teststring");
90 boost::system::error_code ec;
91 value.open(filepath.c_str(), boost::beast::file_mode::read, ec);
92 ASSERT_FALSE(ec);
93 // Move constructor
Ed Tanousb2896142024-01-31 15:25:47 -080094 HttpBody::value_type value2 = std::move(value);
Ed Tanous7a6f0032024-01-28 09:49:03 -080095 std::array<char, 11> buffer{};
96 size_t out = value2.file().read(buffer.data(), buffer.size(), ec);
97 ASSERT_FALSE(ec);
98 EXPECT_EQ(value2.encodingType, EncodingType::Base64);
99
100 EXPECT_THAT(std::span(buffer.data(), out),
101 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
102 EXPECT_THAT(buffer, ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n',
103 'g', '\0'));
104
105 EXPECT_EQ(value2.payloadSize(), 16);
106}
107
Ed Tanousb2896142024-01-31 15:25:47 -0800108TEST(HttpBodyValueType, SetFd)
Ed Tanous7a6f0032024-01-28 09:49:03 -0800109{
Ed Tanousb2896142024-01-31 15:25:47 -0800110 HttpBody::value_type value(EncodingType::Base64);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800111 std::string filepath = makeFile("teststring");
112
113 boost::system::error_code ec;
114 value.setFd(fileno(fopen(filepath.c_str(), "r")), ec);
115 ASSERT_FALSE(ec);
116
117 std::array<char, 4096> buffer{};
118
119 size_t out = value.file().read(buffer.data(), buffer.size(), ec);
120 ASSERT_FALSE(ec);
121
122 EXPECT_THAT(std::span(buffer.data(), out),
123 ElementsAre('t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g'));
124 EXPECT_EQ(value.payloadSize(), 16);
125}
126
127} // namespace
128} // namespace bmcweb