summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEd Tanous <ed@tanous.net>2024-01-24 03:31:11 +0300
committerEd Tanous <ed@tanous.net>2024-02-16 20:34:04 +0300
commit52e31629978bbcefc0e14c96272ef77c2dad6a9c (patch)
treee6618ffeeb0e0ab694a9484d4115344b8f767ff1 /test
parente4588158c0ccc2b3b7af459b262e8eaefaf8f985 (diff)
downloadbmcweb-52e31629978bbcefc0e14c96272ef77c2dad6a9c.tar.xz
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>
Diffstat (limited to 'test')
-rw-r--r--test/http/http_response_test.cpp86
1 files changed, 23 insertions, 63 deletions
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<false, bmcweb::FileBody>& 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<false, bmcweb::FileBody>& sr)
+std::string getData(boost::beast::http::response<bmcweb::FileBody>& 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<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())
{
- const auto& innerBuf = *iter;
- auto view = std::string_view(static_cast<const char*>(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<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 @@ TEST(HttpResponse, BodyTransitions)
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);
}