Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 1 | #include "file_test_utilities.hpp" |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 2 | #include "http_body.hpp" |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 3 | |
| 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 | |
| 13 | using ::testing::ElementsAre; |
| 14 | |
| 15 | namespace bmcweb |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 20 | TEST(HttpHttpBodyValueType, MoveString) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 21 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 22 | HttpBody::value_type value("teststring"); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 23 | // Move constructor |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 24 | HttpBody::value_type value2(std::move(value)); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 25 | EXPECT_EQ(value2.encodingType, EncodingType::Raw); |
| 26 | EXPECT_EQ(value2.str(), "teststring"); |
| 27 | EXPECT_EQ(value2.payloadSize(), 10); |
| 28 | } |
| 29 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 30 | TEST(HttpHttpBodyValueType, MoveOperatorString) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 31 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 32 | HttpBody::value_type value; |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 33 | value.str() = "teststring"; |
| 34 | // Move constructor |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 35 | HttpBody::value_type value2 = std::move(value); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 36 | EXPECT_EQ(value2.encodingType, EncodingType::Raw); |
| 37 | EXPECT_EQ(value2.str(), "teststring"); |
| 38 | EXPECT_EQ(value2.payloadSize(), 10); |
| 39 | } |
| 40 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 41 | TEST(HttpHttpBodyValueType, copysignl) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 42 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 43 | HttpBody::value_type value; |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 44 | value.str() = "teststring"; |
| 45 | // Move constructor |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 46 | HttpBody::value_type value2(value); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 47 | EXPECT_EQ(value2.encodingType, EncodingType::Raw); |
| 48 | EXPECT_EQ(value2.str(), "teststring"); |
| 49 | EXPECT_EQ(value2.payloadSize(), 10); |
| 50 | } |
| 51 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 52 | TEST(HttpHttpBodyValueType, CopyOperatorString) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 53 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 54 | HttpBody::value_type value; |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 55 | value.str() = "teststring"; |
| 56 | // Move constructor |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 57 | HttpBody::value_type value2 = value; |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 58 | EXPECT_EQ(value2.encodingType, EncodingType::Raw); |
| 59 | EXPECT_EQ(value2.str(), "teststring"); |
| 60 | EXPECT_EQ(value2.payloadSize(), 10); |
| 61 | } |
| 62 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 63 | TEST(HttpHttpBodyValueType, MoveFile) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 64 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 65 | HttpBody::value_type value(EncodingType::Base64); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 66 | 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 Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 71 | HttpBody::value_type value2(std::move(value)); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 72 | 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 Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 86 | TEST(HttpHttpBodyValueType, MoveOperatorFile) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 87 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 88 | HttpBody::value_type value(EncodingType::Base64); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 89 | 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 Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 94 | HttpBody::value_type value2 = std::move(value); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 95 | 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 Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 108 | TEST(HttpBodyValueType, SetFd) |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 109 | { |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 110 | HttpBody::value_type value(EncodingType::Base64); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 111 | 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 |