Simplify body
Now that we have a custom boost http body class, we can use it in more
cases. There's some significant overhead and code when switching to a
file body, namely removing all the headers. Making the body class
support strings would allow us to completely avoid that inefficiency.
At the same time, it would mean that we can now use that class for all
cases, including HttpClient, and http::Request. This leads to some code
reduction overall, and means we're reliant on fewer beast structures.
As an added benefit, we no longer have to take a dependency on
boost::variant2.
Tested: Redfish service validator passes, with the exception of
badNamespaceInclude, which is showing warnings prior to this commit.
Change-Id: I061883a73230d6085d951c15891465c2c8445969
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/test/http/http_response_test.cpp b/test/http/http_response_test.cpp
index 1ee3853..f61d6de 100644
--- a/test/http/http_response_test.cpp
+++ b/test/http/http_response_test.cpp
@@ -1,3 +1,4 @@
+#include "boost/beast/core/buffers_to_string.hpp"
#include "boost/beast/core/flat_buffer.hpp"
#include "boost/beast/http/serializer.hpp"
#include "http/http_response.hpp"
@@ -35,71 +36,40 @@
return stringPath;
}
-void readHeader(boost::beast::http::serializer<false, bmcweb::FileBody>& sr)
+std::string getData(boost::beast::http::response<bmcweb::FileBody>& m)
{
+ std::string ret;
+
+ boost::beast::http::response_serializer<bmcweb::FileBody> sr{m};
+ sr.split(true);
+
+ auto reader = [&sr, &ret](const boost::system::error_code& ec2,
+ const auto& buffer) {
+ EXPECT_FALSE(ec2);
+ std::string ret2 = boost::beast::buffers_to_string(buffer);
+ sr.consume(ret2.size());
+ ret += ret2;
+ };
+ boost::system::error_code ec;
+
+ // Read headers
while (!sr.is_header_done())
{
- boost::system::error_code ec;
- sr.next(ec, [&sr](const boost::system::error_code& ec2,
- const auto& buffer) {
- ASSERT_FALSE(ec2);
- sr.consume(boost::beast::buffer_bytes(buffer));
- });
- ASSERT_FALSE(ec);
+ sr.next(ec, reader);
+ EXPECT_FALSE(ec);
}
-}
+ ret.clear();
-std::string collectFromBuffers(
- const auto& buffer,
- boost::beast::http::serializer<false, bmcweb::FileBody>& sr)
-{
- std::string ret;
-
- for (auto iter = boost::asio::buffer_sequence_begin(buffer);
- iter != boost::asio::buffer_sequence_end(buffer); ++iter)
- {
- const auto& innerBuf = *iter;
- auto view = std::string_view(static_cast<const char*>(innerBuf.data()),
- innerBuf.size());
- ret += view;
- sr.consume(innerBuf.size());
- }
- return ret;
-}
-
-std::string
- readBody(boost::beast::http::serializer<false, bmcweb::FileBody>& sr)
-{
- std::string ret;
+ // Read body
while (!sr.is_done())
{
- boost::system::error_code ec;
- sr.next(ec, [&sr, &ret](const boost::system::error_code& ec2,
- const auto& buffer) {
- ASSERT_FALSE(ec2);
- ret += collectFromBuffers(buffer, sr);
- });
+ sr.next(ec, reader);
EXPECT_FALSE(ec);
}
return ret;
}
-std::string getData(crow::Response::file_response& m)
-{
- boost::beast::http::serializer<false, bmcweb::FileBody> sr{m};
- std::stringstream ret;
- sr.split(true);
- readHeader(sr);
- return readBody(sr);
-}
-TEST(HttpResponse, Defaults)
-{
- crow::Response res;
- EXPECT_EQ(
- boost::variant2::holds_alternative<crow::Response::string_response>(
- res.response),
- true);
-}
+
TEST(HttpResponse, Headers)
{
crow::Response res;
@@ -156,26 +126,16 @@
std::string path = makeFile("sample text");
res.openFile(path);
- EXPECT_EQ(boost::variant2::holds_alternative<crow::Response::file_response>(
- res.response),
- true);
-
verifyHeaders(res);
res.write("body text");
- EXPECT_EQ(
- boost::variant2::holds_alternative<crow::Response::string_response>(
- res.response),
- true);
-
verifyHeaders(res);
std::filesystem::remove(path);
}
void testFileData(crow::Response& res, const std::string& data)
{
- auto& fb =
- boost::variant2::get<crow::Response::file_response>(res.response);
+ auto& fb = res.response;
EXPECT_EQ(getData(fb), data);
}