blob: 3bc0c9c03c4ca22c13f0241eb15caa66f96f122e [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/http_body.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05003#include "http/http_response.hpp"
Ed Tanousf0b59af2024-03-20 13:38:04 -07004#include "utility.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05005
Ed Tanous8d45b9c2024-04-08 17:50:10 -07006#include <boost/beast/core/buffers_to_string.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -07007#include <boost/beast/core/file_base.hpp>
8#include <boost/beast/core/file_posix.hpp>
9#include <boost/beast/http/message.hpp>
Ed Tanous8d45b9c2024-04-08 17:50:10 -070010#include <boost/beast/http/serializer.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -070011#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);
Ed Tanous5575efb2024-01-30 09:25:03 -080039 // Reads buffers into ret
Ed Tanous52e31622024-01-23 16:31:11 -080040 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);
Ed Tanous5575efb2024-01-30 09:25:03 -080077 std::string_view bodyValue = "this is my new body";
78 res.write({bodyValue.data(), bodyValue.length()});
79 EXPECT_EQ(*res.body(), bodyValue);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050080 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);
Ed Tanous5575efb2024-01-30 09:25:03 -080086 TemporaryFileHandle temporaryFile("sample text");
87 res.openFile(temporaryFile.stringPath);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050088
89 verifyHeaders(res);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050090}
Ed Tanousb2896142024-01-31 15:25:47 -080091TEST(HttpResponse, HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060092{
93 crow::Response res;
94 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -080095 TemporaryFileHandle temporaryFile("sample text");
96 FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r+");
Abhilash Rajub5f288d2023-11-08 22:32:44 -060097 res.openFd(fileno(fd));
98 verifyHeaders(res);
99 fclose(fd);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600100}
101
Ed Tanousb2896142024-01-31 15:25:47 -0800102TEST(HttpResponse, Base64HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600103{
104 crow::Response res;
105 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -0800106 TemporaryFileHandle temporaryFile("sample text");
107 FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r");
108 ASSERT_NE(fd, nullptr);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600109 res.openFd(fileno(fd), bmcweb::EncodingType::Base64);
110 verifyHeaders(res);
111 fclose(fd);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600112}
113
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500114TEST(HttpResponse, BodyTransitions)
115{
116 crow::Response res;
117 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -0800118 TemporaryFileHandle temporaryFile("sample text");
119 res.openFile(temporaryFile.stringPath);
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500120
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500121 verifyHeaders(res);
122 res.write("body text");
123
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500124 verifyHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600125}
126
Ed Tanous7a6f0032024-01-28 09:49:03 -0800127std::string generateBigdata()
128{
129 std::string result;
130 while (result.size() < 10000)
131 {
132 result += "sample text";
133 }
134 return result;
135}
136
137TEST(HttpResponse, StringBodyWriterLarge)
138{
139 crow::Response res;
140 std::string data = generateBigdata();
141 res.write(std::string(data));
Ed Tanous5575efb2024-01-30 09:25:03 -0800142 EXPECT_EQ(getData(res.response), data);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800143}
144
Ed Tanousb2896142024-01-31 15:25:47 -0800145TEST(HttpResponse, Base64HttpBodyWriter)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600146{
147 crow::Response res;
148 std::string data = "sample text";
Ed Tanous5575efb2024-01-30 09:25:03 -0800149 TemporaryFileHandle temporaryFile(data);
150 FILE* f = fopen(temporaryFile.stringPath.c_str(), "r+");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600151 res.openFd(fileno(f), bmcweb::EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -0800152 EXPECT_EQ(getData(res.response), "c2FtcGxlIHRleHQ=");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600153}
154
Ed Tanousb2896142024-01-31 15:25:47 -0800155TEST(HttpResponse, Base64HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600156{
157 crow::Response res;
158 std::string data = generateBigdata();
Ed Tanous5575efb2024-01-30 09:25:03 -0800159 TemporaryFileHandle temporaryFile(data);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600160
Ed Tanous5575efb2024-01-30 09:25:03 -0800161 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 Rajub5f288d2023-11-08 22:32:44 -0600168}
169
Ed Tanousb2896142024-01-31 15:25:47 -0800170TEST(HttpResponse, HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600171{
172 crow::Response res;
173 std::string data = generateBigdata();
Ed Tanous5575efb2024-01-30 09:25:03 -0800174 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 Rajub5f288d2023-11-08 22:32:44 -0600183}
184
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500185} // namespace