From 52e31629978bbcefc0e14c96272ef77c2dad6a9c Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Tue, 23 Jan 2024 16:31:11 -0800 Subject: 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 --- test/http/http_response_test.cpp | 86 +++++++++++----------------------------- 1 file changed, 23 insertions(+), 63 deletions(-) (limited to 'test') diff --git a/test/http/http_response_test.cpp b/test/http/http_response_test.cpp index 1ee3853f78..f61d6dea9d 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 @@ std::string makeFile(std::string_view sampleData) return stringPath; } -void readHeader(boost::beast::http::serializer& sr) -{ - 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); - } -} - -std::string collectFromBuffers( - const auto& buffer, - boost::beast::http::serializer& sr) +std::string getData(boost::beast::http::response& m) { std::string ret; - for (auto iter = boost::asio::buffer_sequence_begin(buffer); - iter != boost::asio::buffer_sequence_end(buffer); ++iter) + boost::beast::http::response_serializer 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()) { - const auto& innerBuf = *iter; - auto view = std::string_view(static_cast(innerBuf.data()), - innerBuf.size()); - ret += view; - sr.consume(innerBuf.size()); + sr.next(ec, reader); + EXPECT_FALSE(ec); } - return ret; -} + ret.clear(); -std::string - readBody(boost::beast::http::serializer& 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 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( - res.response), - true); -} + TEST(HttpResponse, Headers) { crow::Response res; @@ -156,26 +126,16 @@ TEST(HttpResponse, BodyTransitions) std::string path = makeFile("sample text"); res.openFile(path); - EXPECT_EQ(boost::variant2::holds_alternative( - res.response), - true); - verifyHeaders(res); res.write("body text"); - EXPECT_EQ( - boost::variant2::holds_alternative( - res.response), - true); - verifyHeaders(res); std::filesystem::remove(path); } void testFileData(crow::Response& res, const std::string& data) { - auto& fb = - boost::variant2::get(res.response); + auto& fb = res.response; EXPECT_EQ(getData(fb), data); } -- cgit v1.2.3