mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-04 19:25:51 +00:00
Add logs of what's going on - Simple RIAA activity tracer
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
#include <doctest/doctest.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <thread>
|
||||
|
||||
@@ -36,6 +37,40 @@ using namespace ripple;
|
||||
|
||||
namespace {
|
||||
|
||||
struct logger
|
||||
{
|
||||
std::string name;
|
||||
logger const* const parent = nullptr;
|
||||
static std::size_t depth;
|
||||
logger(std::string n) : name(n)
|
||||
{
|
||||
std::clog << indent() << name << " begin\n";
|
||||
++depth;
|
||||
}
|
||||
|
||||
logger(logger const& p, std::string n) : parent(&p), name(n)
|
||||
{
|
||||
std::clog << indent() << parent->name << " : " << name << " begin\n";
|
||||
++depth;
|
||||
}
|
||||
|
||||
~logger()
|
||||
{
|
||||
--depth;
|
||||
if (parent)
|
||||
std::clog << indent() << parent->name << " : " << name << " end\n";
|
||||
else
|
||||
std::clog << indent() << name << " end\n";
|
||||
}
|
||||
|
||||
std::string
|
||||
indent()
|
||||
{
|
||||
return std::string(depth, ' ');
|
||||
}
|
||||
};
|
||||
std::size_t logger::depth = 0;
|
||||
|
||||
// Simple HTTP server using Beast for testing
|
||||
class TestHTTPServer
|
||||
{
|
||||
@@ -54,6 +89,7 @@ private:
|
||||
public:
|
||||
TestHTTPServer() : acceptor_(ioc_), port_(0)
|
||||
{
|
||||
logger l("TestHTTPServer()");
|
||||
// Bind to any available port
|
||||
endpoint_ = {boost::asio::ip::tcp::v4(), 0};
|
||||
acceptor_.open(endpoint_.protocol());
|
||||
@@ -69,6 +105,7 @@ public:
|
||||
|
||||
~TestHTTPServer()
|
||||
{
|
||||
logger l("~TestHTTPServer()");
|
||||
stop();
|
||||
}
|
||||
|
||||
@@ -106,6 +143,7 @@ private:
|
||||
void
|
||||
stop()
|
||||
{
|
||||
logger l("TestHTTPServer::stop");
|
||||
running_ = false;
|
||||
acceptor_.close();
|
||||
}
|
||||
@@ -113,6 +151,7 @@ private:
|
||||
void
|
||||
accept()
|
||||
{
|
||||
logger l("TestHTTPServer::accept");
|
||||
if (!running_)
|
||||
return;
|
||||
|
||||
@@ -134,31 +173,37 @@ private:
|
||||
void
|
||||
handleConnection(boost::asio::ip::tcp::socket socket)
|
||||
{
|
||||
logger l("TestHTTPServer::handleConnection");
|
||||
try
|
||||
{
|
||||
std::optional<logger> r(std::in_place, l, "read the http request");
|
||||
// Read the HTTP request
|
||||
boost::beast::flat_buffer buffer;
|
||||
boost::beast::http::request<boost::beast::http::string_body> req;
|
||||
boost::beast::http::read(socket, buffer, req);
|
||||
|
||||
// Create response
|
||||
r.emplace(l, "create response");
|
||||
boost::beast::http::response<boost::beast::http::string_body> res;
|
||||
res.version(req.version());
|
||||
res.result(status_code_);
|
||||
res.set(boost::beast::http::field::server, "TestServer");
|
||||
|
||||
// Add custom headers
|
||||
r.emplace(l, "add custom headers");
|
||||
for (auto const& [name, value] : custom_headers_)
|
||||
{
|
||||
res.set(name, value);
|
||||
}
|
||||
|
||||
// Set body and prepare payload first
|
||||
r.emplace(l, "set body and prepare payload");
|
||||
res.body() = response_body_;
|
||||
res.prepare_payload();
|
||||
|
||||
// Override Content-Length with custom headers after prepare_payload
|
||||
// This allows us to test case-insensitive header parsing
|
||||
r.emplace(l, "override content-length");
|
||||
for (auto const& [name, value] : custom_headers_)
|
||||
{
|
||||
if (boost::iequals(name, "Content-Length"))
|
||||
@@ -169,20 +214,26 @@ private:
|
||||
}
|
||||
|
||||
// Send response
|
||||
r.emplace(l, "send response");
|
||||
boost::beast::http::write(socket, res);
|
||||
|
||||
// Shutdown socket gracefully
|
||||
r.emplace(l, "shutdown socket");
|
||||
boost::system::error_code ec;
|
||||
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
|
||||
}
|
||||
catch (std::exception const&)
|
||||
{
|
||||
// Connection handling errors are expected
|
||||
logger c(l, "catch");
|
||||
}
|
||||
|
||||
if (running_)
|
||||
{
|
||||
logger r(l, "accept");
|
||||
accept();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to run HTTP client test
|
||||
@@ -195,12 +246,16 @@ runHTTPTest(
|
||||
std::string& result_data,
|
||||
boost::system::error_code& result_error)
|
||||
{
|
||||
logger l("runHTTPTest");
|
||||
// Create a null journal for testing
|
||||
beast::Journal j{beast::Journal::getNullSink()};
|
||||
|
||||
std::optional<logger> r(std::in_place, l, "initializeSSLContext");
|
||||
|
||||
// Initialize HTTPClient SSL context
|
||||
HTTPClient::initializeSSLContext("", "", false, j);
|
||||
|
||||
r.emplace(l, "HTTPClient::get");
|
||||
HTTPClient::get(
|
||||
false, // no SSL
|
||||
server.ioc(),
|
||||
@@ -225,6 +280,7 @@ runHTTPTest(
|
||||
while (!completed &&
|
||||
std::chrono::steady_clock::now() - start < std::chrono::seconds(10))
|
||||
{
|
||||
r.emplace(l, "ioc.run_one");
|
||||
if (server.ioc().run_one() == 0)
|
||||
{
|
||||
break;
|
||||
@@ -238,6 +294,8 @@ runHTTPTest(
|
||||
|
||||
TEST_CASE("HTTPClient case insensitive Content-Length")
|
||||
{
|
||||
logger l("HTTPClient case insensitive Content-Length");
|
||||
|
||||
// Test different cases of Content-Length header
|
||||
std::vector<std::string> header_cases = {
|
||||
"Content-Length", // Standard case
|
||||
@@ -249,6 +307,7 @@ TEST_CASE("HTTPClient case insensitive Content-Length")
|
||||
|
||||
for (auto const& header_name : header_cases)
|
||||
{
|
||||
logger h(l, header_name);
|
||||
TestHTTPServer server;
|
||||
std::string test_body = "Hello World!";
|
||||
server.setResponseBody(test_body);
|
||||
@@ -277,6 +336,7 @@ TEST_CASE("HTTPClient case insensitive Content-Length")
|
||||
|
||||
TEST_CASE("HTTPClient basic HTTP request")
|
||||
{
|
||||
logger l("HTTPClient basic HTTP request");
|
||||
TestHTTPServer server;
|
||||
std::string test_body = "Test response body";
|
||||
server.setResponseBody(test_body);
|
||||
@@ -298,6 +358,7 @@ TEST_CASE("HTTPClient basic HTTP request")
|
||||
|
||||
TEST_CASE("HTTPClient empty response")
|
||||
{
|
||||
logger l("HTTPClient empty response");
|
||||
TestHTTPServer server;
|
||||
server.setResponseBody(""); // Empty body
|
||||
server.setHeader("Content-Length", "0");
|
||||
@@ -318,6 +379,7 @@ TEST_CASE("HTTPClient empty response")
|
||||
|
||||
TEST_CASE("HTTPClient different status codes")
|
||||
{
|
||||
logger l("HTTPClient different status codes");
|
||||
std::vector<unsigned int> status_codes = {200, 404, 500};
|
||||
|
||||
for (auto status : status_codes)
|
||||
|
||||
Reference in New Issue
Block a user