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/http_body.hpp" |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 3 | #include "http/http_response.hpp" |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 4 | #include "utility.hpp" |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 5 | |
Ed Tanous | 8d45b9c | 2024-04-08 17:50:10 -0700 | [diff] [blame^] | 6 | #include <boost/beast/core/buffers_to_string.hpp> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 7 | #include <boost/beast/core/file_base.hpp> |
| 8 | #include <boost/beast/core/file_posix.hpp> |
| 9 | #include <boost/beast/http/message.hpp> |
Ed Tanous | 8d45b9c | 2024-04-08 17:50:10 -0700 | [diff] [blame^] | 10 | #include <boost/beast/http/serializer.hpp> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 11 | #include <boost/beast/http/status.hpp> |
| 12 | |
| 13 | #include <cstdio> |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 14 | #include <filesystem> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 15 | #include <string> |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | namespace |
| 19 | { |
| 20 | void addHeaders(crow::Response& res) |
| 21 | { |
| 22 | res.addHeader("myheader", "myvalue"); |
| 23 | res.keepAlive(true); |
| 24 | res.result(boost::beast::http::status::ok); |
| 25 | } |
| 26 | void verifyHeaders(crow::Response& res) |
| 27 | { |
| 28 | EXPECT_EQ(res.getHeaderValue("myheader"), "myvalue"); |
| 29 | EXPECT_EQ(res.keepAlive(), true); |
| 30 | EXPECT_EQ(res.result(), boost::beast::http::status::ok); |
| 31 | } |
| 32 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 33 | std::string getData(boost::beast::http::response<bmcweb::HttpBody>& m) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 34 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 35 | std::string ret; |
| 36 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 37 | boost::beast::http::response_serializer<bmcweb::HttpBody> sr{m}; |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 38 | sr.split(true); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 39 | // Reads buffers into ret |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 40 | auto reader = [&sr, &ret](const boost::system::error_code& ec2, |
| 41 | const auto& buffer) { |
| 42 | EXPECT_FALSE(ec2); |
| 43 | std::string ret2 = boost::beast::buffers_to_string(buffer); |
| 44 | sr.consume(ret2.size()); |
| 45 | ret += ret2; |
| 46 | }; |
| 47 | boost::system::error_code ec; |
| 48 | |
| 49 | // Read headers |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 50 | while (!sr.is_header_done()) |
| 51 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 52 | sr.next(ec, reader); |
| 53 | EXPECT_FALSE(ec); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 54 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 55 | ret.clear(); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 56 | |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 57 | // Read body |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 58 | while (!sr.is_done()) |
| 59 | { |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 60 | sr.next(ec, reader); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 61 | EXPECT_FALSE(ec); |
| 62 | } |
| 63 | |
| 64 | return ret; |
| 65 | } |
Ed Tanous | 52e3162 | 2024-01-23 16:31:11 -0800 | [diff] [blame] | 66 | |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 67 | TEST(HttpResponse, Headers) |
| 68 | { |
| 69 | crow::Response res; |
| 70 | addHeaders(res); |
| 71 | verifyHeaders(res); |
| 72 | } |
| 73 | TEST(HttpResponse, StringBody) |
| 74 | { |
| 75 | crow::Response res; |
| 76 | addHeaders(res); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 77 | std::string_view bodyValue = "this is my new body"; |
| 78 | res.write({bodyValue.data(), bodyValue.length()}); |
| 79 | EXPECT_EQ(*res.body(), bodyValue); |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 80 | verifyHeaders(res); |
| 81 | } |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 82 | TEST(HttpResponse, HttpBody) |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 83 | { |
| 84 | crow::Response res; |
| 85 | addHeaders(res); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 86 | TemporaryFileHandle temporaryFile("sample text"); |
| 87 | res.openFile(temporaryFile.stringPath); |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 88 | |
| 89 | verifyHeaders(res); |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 90 | } |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 91 | TEST(HttpResponse, HttpBodyWithFd) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 92 | { |
| 93 | crow::Response res; |
| 94 | addHeaders(res); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 95 | TemporaryFileHandle temporaryFile("sample text"); |
| 96 | FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r+"); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 97 | res.openFd(fileno(fd)); |
| 98 | verifyHeaders(res); |
| 99 | fclose(fd); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 102 | TEST(HttpResponse, Base64HttpBodyWithFd) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 103 | { |
| 104 | crow::Response res; |
| 105 | addHeaders(res); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 106 | TemporaryFileHandle temporaryFile("sample text"); |
| 107 | FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r"); |
| 108 | ASSERT_NE(fd, nullptr); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 109 | res.openFd(fileno(fd), bmcweb::EncodingType::Base64); |
| 110 | verifyHeaders(res); |
| 111 | fclose(fd); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 112 | } |
| 113 | |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 114 | TEST(HttpResponse, BodyTransitions) |
| 115 | { |
| 116 | crow::Response res; |
| 117 | addHeaders(res); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 118 | TemporaryFileHandle temporaryFile("sample text"); |
| 119 | res.openFile(temporaryFile.stringPath); |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 120 | |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 121 | verifyHeaders(res); |
| 122 | res.write("body text"); |
| 123 | |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 124 | verifyHeaders(res); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 127 | std::string generateBigdata() |
| 128 | { |
| 129 | std::string result; |
| 130 | while (result.size() < 10000) |
| 131 | { |
| 132 | result += "sample text"; |
| 133 | } |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | TEST(HttpResponse, StringBodyWriterLarge) |
| 138 | { |
| 139 | crow::Response res; |
| 140 | std::string data = generateBigdata(); |
| 141 | res.write(std::string(data)); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 142 | EXPECT_EQ(getData(res.response), data); |
Ed Tanous | 7a6f003 | 2024-01-28 09:49:03 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 145 | TEST(HttpResponse, Base64HttpBodyWriter) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 146 | { |
| 147 | crow::Response res; |
| 148 | std::string data = "sample text"; |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 149 | TemporaryFileHandle temporaryFile(data); |
| 150 | FILE* f = fopen(temporaryFile.stringPath.c_str(), "r+"); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 151 | res.openFd(fileno(f), bmcweb::EncodingType::Base64); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 152 | EXPECT_EQ(getData(res.response), "c2FtcGxlIHRleHQ="); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 155 | TEST(HttpResponse, Base64HttpBodyWriterLarge) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 156 | { |
| 157 | crow::Response res; |
| 158 | std::string data = generateBigdata(); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 159 | TemporaryFileHandle temporaryFile(data); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 160 | |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 161 | boost::beast::file_posix file; |
| 162 | boost::system::error_code ec; |
| 163 | file.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, |
| 164 | ec); |
| 165 | EXPECT_EQ(ec.value(), 0); |
| 166 | res.openFd(file.native_handle(), bmcweb::EncodingType::Base64); |
| 167 | EXPECT_EQ(getData(res.response), crow::utility::base64encode(data)); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 168 | } |
| 169 | |
Ed Tanous | b289614 | 2024-01-31 15:25:47 -0800 | [diff] [blame] | 170 | TEST(HttpResponse, HttpBodyWriterLarge) |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 171 | { |
| 172 | crow::Response res; |
| 173 | std::string data = generateBigdata(); |
Ed Tanous | 5575efb | 2024-01-30 09:25:03 -0800 | [diff] [blame] | 174 | TemporaryFileHandle temporaryFile(data); |
| 175 | |
| 176 | boost::beast::file_posix file; |
| 177 | boost::system::error_code ec; |
| 178 | file.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read, |
| 179 | ec); |
| 180 | EXPECT_EQ(ec.value(), 0); |
| 181 | res.openFd(file.native_handle()); |
| 182 | EXPECT_EQ(getData(res.response), data); |
Abhilash Raju | b5f288d | 2023-11-08 22:32:44 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Abhilash Raju | 8e3f703 | 2023-07-17 08:53:11 -0500 | [diff] [blame] | 185 | } // namespace |