blob: d0836a2bab1b191fa0d380f40eaf7096d638de6b [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/http/serializer.hpp"
Ed Tanous7a6f0032024-01-28 09:49:03 -08003#include "file_test_utilities.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08004#include "http/http_body.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05005#include "http/http_response.hpp"
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include "utility.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05007
Ed Tanousf0b59af2024-03-20 13:38:04 -07008#include <boost/beast/core/file_base.hpp>
9#include <boost/beast/core/file_posix.hpp>
10#include <boost/beast/http/message.hpp>
11#include <boost/beast/http/status.hpp>
12
13#include <cstdio>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050014#include <filesystem>
Ed Tanousf0b59af2024-03-20 13:38:04 -070015#include <string>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050016
17#include "gtest/gtest.h"
18namespace
19{
20void addHeaders(crow::Response& res)
21{
22 res.addHeader("myheader", "myvalue");
23 res.keepAlive(true);
24 res.result(boost::beast::http::status::ok);
25}
26void 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 Tanousb2896142024-01-31 15:25:47 -080033std::string getData(boost::beast::http::response<bmcweb::HttpBody>& m)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060034{
Ed Tanous52e31622024-01-23 16:31:11 -080035 std::string ret;
36
Ed Tanousb2896142024-01-31 15:25:47 -080037 boost::beast::http::response_serializer<bmcweb::HttpBody> sr{m};
Ed Tanous52e31622024-01-23 16:31:11 -080038 sr.split(true);
39
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 Rajub5f288d2023-11-08 22:32:44 -060050 while (!sr.is_header_done())
51 {
Ed Tanous52e31622024-01-23 16:31:11 -080052 sr.next(ec, reader);
53 EXPECT_FALSE(ec);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060054 }
Ed Tanous52e31622024-01-23 16:31:11 -080055 ret.clear();
Abhilash Rajub5f288d2023-11-08 22:32:44 -060056
Ed Tanous52e31622024-01-23 16:31:11 -080057 // Read body
Abhilash Rajub5f288d2023-11-08 22:32:44 -060058 while (!sr.is_done())
59 {
Ed Tanous52e31622024-01-23 16:31:11 -080060 sr.next(ec, reader);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060061 EXPECT_FALSE(ec);
62 }
63
64 return ret;
65}
Ed Tanous52e31622024-01-23 16:31:11 -080066
Abhilash Raju8e3f7032023-07-17 08:53:11 -050067TEST(HttpResponse, Headers)
68{
69 crow::Response res;
70 addHeaders(res);
71 verifyHeaders(res);
72}
73TEST(HttpResponse, StringBody)
74{
75 crow::Response res;
76 addHeaders(res);
77 std::string_view bodyvalue = "this is my new body";
78 res.write({bodyvalue.data(), bodyvalue.length()});
79 EXPECT_EQ(*res.body(), bodyvalue);
80 verifyHeaders(res);
81}
Ed Tanousb2896142024-01-31 15:25:47 -080082TEST(HttpResponse, HttpBody)
Abhilash Raju8e3f7032023-07-17 08:53:11 -050083{
84 crow::Response res;
85 addHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060086 std::string path = makeFile("sample text");
Abhilash Raju8e3f7032023-07-17 08:53:11 -050087 res.openFile(path);
88
89 verifyHeaders(res);
90 std::filesystem::remove(path);
91}
Ed Tanousb2896142024-01-31 15:25:47 -080092TEST(HttpResponse, HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060093{
94 crow::Response res;
95 addHeaders(res);
96 std::string path = makeFile("sample text");
97 FILE* fd = fopen(path.c_str(), "r+");
98 res.openFd(fileno(fd));
99 verifyHeaders(res);
100 fclose(fd);
101 std::filesystem::remove(path);
102}
103
Ed Tanousb2896142024-01-31 15:25:47 -0800104TEST(HttpResponse, Base64HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600105{
106 crow::Response res;
107 addHeaders(res);
108 std::string path = makeFile("sample text");
109 FILE* fd = fopen(path.c_str(), "r+");
110 res.openFd(fileno(fd), bmcweb::EncodingType::Base64);
111 verifyHeaders(res);
112 fclose(fd);
113 std::filesystem::remove(path);
114}
115
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500116TEST(HttpResponse, BodyTransitions)
117{
118 crow::Response res;
119 addHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600120 std::string path = makeFile("sample text");
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500121 res.openFile(path);
122
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500123 verifyHeaders(res);
124 res.write("body text");
125
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500126 verifyHeaders(res);
127 std::filesystem::remove(path);
128}
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600129
130void testFileData(crow::Response& res, const std::string& data)
131{
Ed Tanous52e31622024-01-23 16:31:11 -0800132 auto& fb = res.response;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600133 EXPECT_EQ(getData(fb), data);
134}
135
Ed Tanous7a6f0032024-01-28 09:49:03 -0800136std::string generateBigdata()
137{
138 std::string result;
139 while (result.size() < 10000)
140 {
141 result += "sample text";
142 }
143 return result;
144}
145
146TEST(HttpResponse, StringBodyWriterLarge)
147{
148 crow::Response res;
149 std::string data = generateBigdata();
150 res.write(std::string(data));
151 testFileData(res, data);
152}
153
Ed Tanousb2896142024-01-31 15:25:47 -0800154TEST(HttpResponse, Base64HttpBodyWriter)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600155{
156 crow::Response res;
157 std::string data = "sample text";
158 std::string path = makeFile(data);
159 FILE* f = fopen(path.c_str(), "r+");
160 res.openFd(fileno(f), bmcweb::EncodingType::Base64);
161 testFileData(res, crow::utility::base64encode(data));
162 fclose(f);
163 std::filesystem::remove(path);
164}
165
Ed Tanousb2896142024-01-31 15:25:47 -0800166TEST(HttpResponse, Base64HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600167{
168 crow::Response res;
169 std::string data = generateBigdata();
170 std::string path = makeFile(data);
171 {
172 boost::beast::file_posix file;
173 boost::system::error_code ec;
174 file.open(path.c_str(), boost::beast::file_mode::read, ec);
175 EXPECT_EQ(ec.value(), 0);
176 res.openFd(file.native_handle(), bmcweb::EncodingType::Base64);
177 testFileData(res, crow::utility::base64encode(data));
178 }
179
180 std::filesystem::remove(path);
181}
182
Ed Tanousb2896142024-01-31 15:25:47 -0800183TEST(HttpResponse, HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600184{
185 crow::Response res;
186 std::string data = generateBigdata();
187 std::string path = makeFile(data);
188 {
189 boost::beast::file_posix file;
190 boost::system::error_code ec;
191 file.open(path.c_str(), boost::beast::file_mode::read, ec);
192 EXPECT_EQ(ec.value(), 0);
193 res.openFd(file.native_handle());
194 testFileData(res, data);
195 }
196 std::filesystem::remove(path);
197}
198
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500199} // namespace