Compare commits

...

10 Commits

View File

@@ -10,6 +10,7 @@
#include <doctest/doctest.h> #include <doctest/doctest.h>
#include <atomic> #include <atomic>
#include <iostream>
#include <map> #include <map>
#include <thread> #include <thread>
@@ -17,6 +18,40 @@ using namespace ripple;
namespace { 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 // Simple HTTP server using Beast for testing
class TestHTTPServer class TestHTTPServer
{ {
@@ -35,6 +70,7 @@ private:
public: public:
TestHTTPServer() : acceptor_(ioc_), port_(0) TestHTTPServer() : acceptor_(ioc_), port_(0)
{ {
logger l("TestHTTPServer()");
// Bind to any available port // Bind to any available port
endpoint_ = {boost::asio::ip::tcp::v4(), 0}; endpoint_ = {boost::asio::ip::tcp::v4(), 0};
acceptor_.open(endpoint_.protocol()); acceptor_.open(endpoint_.protocol());
@@ -50,6 +86,7 @@ public:
~TestHTTPServer() ~TestHTTPServer()
{ {
logger l("~TestHTTPServer()");
stop(); stop();
} }
@@ -87,6 +124,7 @@ private:
void void
stop() stop()
{ {
logger l("TestHTTPServer::stop");
running_ = false; running_ = false;
acceptor_.close(); acceptor_.close();
} }
@@ -94,6 +132,7 @@ private:
void void
accept() accept()
{ {
logger l("TestHTTPServer::accept");
if (!running_) if (!running_)
return; return;
@@ -115,31 +154,37 @@ private:
void void
handleConnection(boost::asio::ip::tcp::socket socket) handleConnection(boost::asio::ip::tcp::socket socket)
{ {
logger l("TestHTTPServer::handleConnection");
try try
{ {
std::optional<logger> r(std::in_place, l, "read the http request");
// Read the HTTP request // Read the HTTP request
boost::beast::flat_buffer buffer; boost::beast::flat_buffer buffer;
boost::beast::http::request<boost::beast::http::string_body> req; boost::beast::http::request<boost::beast::http::string_body> req;
boost::beast::http::read(socket, buffer, req); boost::beast::http::read(socket, buffer, req);
// Create response // Create response
r.emplace(l, "create response");
boost::beast::http::response<boost::beast::http::string_body> res; boost::beast::http::response<boost::beast::http::string_body> res;
res.version(req.version()); res.version(req.version());
res.result(status_code_); res.result(status_code_);
res.set(boost::beast::http::field::server, "TestServer"); res.set(boost::beast::http::field::server, "TestServer");
// Add custom headers // Add custom headers
r.emplace(l, "add custom headers");
for (auto const& [name, value] : custom_headers_) for (auto const& [name, value] : custom_headers_)
{ {
res.set(name, value); res.set(name, value);
} }
// Set body and prepare payload first // Set body and prepare payload first
r.emplace(l, "set body and prepare payload");
res.body() = response_body_; res.body() = response_body_;
res.prepare_payload(); res.prepare_payload();
// Override Content-Length with custom headers after prepare_payload // Override Content-Length with custom headers after prepare_payload
// This allows us to test case-insensitive header parsing // This allows us to test case-insensitive header parsing
r.emplace(l, "override content-length");
for (auto const& [name, value] : custom_headers_) for (auto const& [name, value] : custom_headers_)
{ {
if (boost::iequals(name, "Content-Length")) if (boost::iequals(name, "Content-Length"))
@@ -150,20 +195,26 @@ private:
} }
// Send response // Send response
r.emplace(l, "send response");
boost::beast::http::write(socket, res); boost::beast::http::write(socket, res);
// Shutdown socket gracefully // Shutdown socket gracefully
r.emplace(l, "shutdown socket");
boost::system::error_code ec; boost::system::error_code ec;
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec); socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
} }
catch (std::exception const&) catch (std::exception const&)
{ {
// Connection handling errors are expected // Connection handling errors are expected
logger c(l, "catch");
} }
if (running_) if (running_)
{
logger r(l, "accept");
accept(); accept();
} }
}
}; };
// Helper function to run HTTP client test // Helper function to run HTTP client test
@@ -176,12 +227,16 @@ runHTTPTest(
std::string& result_data, std::string& result_data,
boost::system::error_code& result_error) boost::system::error_code& result_error)
{ {
logger l("runHTTPTest");
// Create a null journal for testing // Create a null journal for testing
beast::Journal j{beast::Journal::getNullSink()}; beast::Journal j{beast::Journal::getNullSink()};
std::optional<logger> r(std::in_place, l, "initializeSSLContext");
// Initialize HTTPClient SSL context // Initialize HTTPClient SSL context
HTTPClient::initializeSSLContext("", "", false, j); HTTPClient::initializeSSLContext("", "", false, j);
r.emplace(l, "HTTPClient::get");
HTTPClient::get( HTTPClient::get(
false, // no SSL false, // no SSL
server.ioc(), server.ioc(),
@@ -206,6 +261,7 @@ runHTTPTest(
while (!completed && while (!completed &&
std::chrono::steady_clock::now() - start < std::chrono::seconds(10)) std::chrono::steady_clock::now() - start < std::chrono::seconds(10))
{ {
r.emplace(l, "ioc.run_one");
if (server.ioc().run_one() == 0) if (server.ioc().run_one() == 0)
{ {
break; break;
@@ -219,6 +275,8 @@ runHTTPTest(
TEST_CASE("HTTPClient case insensitive Content-Length") TEST_CASE("HTTPClient case insensitive Content-Length")
{ {
logger l("HTTPClient case insensitive Content-Length");
// Test different cases of Content-Length header // Test different cases of Content-Length header
std::vector<std::string> header_cases = { std::vector<std::string> header_cases = {
"Content-Length", // Standard case "Content-Length", // Standard case
@@ -230,6 +288,7 @@ TEST_CASE("HTTPClient case insensitive Content-Length")
for (auto const& header_name : header_cases) for (auto const& header_name : header_cases)
{ {
logger h(l, header_name);
TestHTTPServer server; TestHTTPServer server;
std::string test_body = "Hello World!"; std::string test_body = "Hello World!";
server.setResponseBody(test_body); server.setResponseBody(test_body);
@@ -258,6 +317,7 @@ TEST_CASE("HTTPClient case insensitive Content-Length")
TEST_CASE("HTTPClient basic HTTP request") TEST_CASE("HTTPClient basic HTTP request")
{ {
logger l("HTTPClient basic HTTP request");
TestHTTPServer server; TestHTTPServer server;
std::string test_body = "Test response body"; std::string test_body = "Test response body";
server.setResponseBody(test_body); server.setResponseBody(test_body);
@@ -279,6 +339,7 @@ TEST_CASE("HTTPClient basic HTTP request")
TEST_CASE("HTTPClient empty response") TEST_CASE("HTTPClient empty response")
{ {
logger l("HTTPClient empty response");
TestHTTPServer server; TestHTTPServer server;
server.setResponseBody(""); // Empty body server.setResponseBody(""); // Empty body
server.setHeader("Content-Length", "0"); server.setHeader("Content-Length", "0");
@@ -299,6 +360,7 @@ TEST_CASE("HTTPClient empty response")
TEST_CASE("HTTPClient different status codes") TEST_CASE("HTTPClient different status codes")
{ {
logger l("HTTPClient different status codes");
std::vector<unsigned int> status_codes = {200, 404, 500}; std::vector<unsigned int> status_codes = {200, 404, 500};
for (auto status : status_codes) for (auto status : status_codes)