blob: c5bfbdca99d66a93de91c0b8e3648060bbba3277 [file] [log] [blame]
Ed Tanous52e31622024-01-23 16:31:11 -08001#include "boost/beast/core/buffers_to_string.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05002#include "boost/beast/core/flat_buffer.hpp"
3#include "boost/beast/http/serializer.hpp"
Ed Tanous7a6f0032024-01-28 09:49:03 -08004#include "file_test_utilities.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05005#include "http/http_response.hpp"
6
7#include <filesystem>
8#include <fstream>
9#include <thread>
10
11#include "gtest/gtest.h"
12namespace
13{
14void addHeaders(crow::Response& res)
15{
16 res.addHeader("myheader", "myvalue");
17 res.keepAlive(true);
18 res.result(boost::beast::http::status::ok);
19}
20void verifyHeaders(crow::Response& res)
21{
22 EXPECT_EQ(res.getHeaderValue("myheader"), "myvalue");
23 EXPECT_EQ(res.keepAlive(), true);
24 EXPECT_EQ(res.result(), boost::beast::http::status::ok);
25}
26
Ed Tanous52e31622024-01-23 16:31:11 -080027std::string getData(boost::beast::http::response<bmcweb::FileBody>& m)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060028{
Ed Tanous52e31622024-01-23 16:31:11 -080029 std::string ret;
30
31 boost::beast::http::response_serializer<bmcweb::FileBody> sr{m};
32 sr.split(true);
33
34 auto reader = [&sr, &ret](const boost::system::error_code& ec2,
35 const auto& buffer) {
36 EXPECT_FALSE(ec2);
37 std::string ret2 = boost::beast::buffers_to_string(buffer);
38 sr.consume(ret2.size());
39 ret += ret2;
40 };
41 boost::system::error_code ec;
42
43 // Read headers
Abhilash Rajub5f288d2023-11-08 22:32:44 -060044 while (!sr.is_header_done())
45 {
Ed Tanous52e31622024-01-23 16:31:11 -080046 sr.next(ec, reader);
47 EXPECT_FALSE(ec);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060048 }
Ed Tanous52e31622024-01-23 16:31:11 -080049 ret.clear();
Abhilash Rajub5f288d2023-11-08 22:32:44 -060050
Ed Tanous52e31622024-01-23 16:31:11 -080051 // Read body
Abhilash Rajub5f288d2023-11-08 22:32:44 -060052 while (!sr.is_done())
53 {
Ed Tanous52e31622024-01-23 16:31:11 -080054 sr.next(ec, reader);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060055 EXPECT_FALSE(ec);
56 }
57
58 return ret;
59}
Ed Tanous52e31622024-01-23 16:31:11 -080060
Abhilash Raju8e3f7032023-07-17 08:53:11 -050061TEST(HttpResponse, Headers)
62{
63 crow::Response res;
64 addHeaders(res);
65 verifyHeaders(res);
66}
67TEST(HttpResponse, StringBody)
68{
69 crow::Response res;
70 addHeaders(res);
71 std::string_view bodyvalue = "this is my new body";
72 res.write({bodyvalue.data(), bodyvalue.length()});
73 EXPECT_EQ(*res.body(), bodyvalue);
74 verifyHeaders(res);
75}
76TEST(HttpResponse, FileBody)
77{
78 crow::Response res;
79 addHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060080 std::string path = makeFile("sample text");
Abhilash Raju8e3f7032023-07-17 08:53:11 -050081 res.openFile(path);
82
83 verifyHeaders(res);
84 std::filesystem::remove(path);
85}
Abhilash Rajub5f288d2023-11-08 22:32:44 -060086TEST(HttpResponse, FileBodyWithFd)
87{
88 crow::Response res;
89 addHeaders(res);
90 std::string path = makeFile("sample text");
91 FILE* fd = fopen(path.c_str(), "r+");
92 res.openFd(fileno(fd));
93 verifyHeaders(res);
94 fclose(fd);
95 std::filesystem::remove(path);
96}
97
98TEST(HttpResponse, Base64FileBodyWithFd)
99{
100 crow::Response res;
101 addHeaders(res);
102 std::string path = makeFile("sample text");
103 FILE* fd = fopen(path.c_str(), "r+");
104 res.openFd(fileno(fd), bmcweb::EncodingType::Base64);
105 verifyHeaders(res);
106 fclose(fd);
107 std::filesystem::remove(path);
108}
109
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500110TEST(HttpResponse, BodyTransitions)
111{
112 crow::Response res;
113 addHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600114 std::string path = makeFile("sample text");
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500115 res.openFile(path);
116
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500117 verifyHeaders(res);
118 res.write("body text");
119
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500120 verifyHeaders(res);
121 std::filesystem::remove(path);
122}
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600123
124void testFileData(crow::Response& res, const std::string& data)
125{
Ed Tanous52e31622024-01-23 16:31:11 -0800126 auto& fb = res.response;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600127 EXPECT_EQ(getData(fb), data);
128}
129
Ed Tanous7a6f0032024-01-28 09:49:03 -0800130std::string generateBigdata()
131{
132 std::string result;
133 while (result.size() < 10000)
134 {
135 result += "sample text";
136 }
137 return result;
138}
139
140TEST(HttpResponse, StringBodyWriterLarge)
141{
142 crow::Response res;
143 std::string data = generateBigdata();
144 res.write(std::string(data));
145 testFileData(res, data);
146}
147
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600148TEST(HttpResponse, Base64FileBodyWriter)
149{
150 crow::Response res;
151 std::string data = "sample text";
152 std::string path = makeFile(data);
153 FILE* f = fopen(path.c_str(), "r+");
154 res.openFd(fileno(f), bmcweb::EncodingType::Base64);
155 testFileData(res, crow::utility::base64encode(data));
156 fclose(f);
157 std::filesystem::remove(path);
158}
159
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600160TEST(HttpResponse, Base64FileBodyWriterLarge)
161{
162 crow::Response res;
163 std::string data = generateBigdata();
164 std::string path = makeFile(data);
165 {
166 boost::beast::file_posix file;
167 boost::system::error_code ec;
168 file.open(path.c_str(), boost::beast::file_mode::read, ec);
169 EXPECT_EQ(ec.value(), 0);
170 res.openFd(file.native_handle(), bmcweb::EncodingType::Base64);
171 testFileData(res, crow::utility::base64encode(data));
172 }
173
174 std::filesystem::remove(path);
175}
176
177TEST(HttpResponse, FileBodyWriterLarge)
178{
179 crow::Response res;
180 std::string data = generateBigdata();
181 std::string path = makeFile(data);
182 {
183 boost::beast::file_posix file;
184 boost::system::error_code ec;
185 file.open(path.c_str(), boost::beast::file_mode::read, ec);
186 EXPECT_EQ(ec.value(), 0);
187 res.openFd(file.native_handle());
188 testFileData(res, data);
189 }
190 std::filesystem::remove(path);
191}
192
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500193} // namespace