feat: Use new web server by default (#2182)

Fixes #1781.
This commit is contained in:
Sergey Kuznetsov
2025-06-04 15:01:30 +01:00
committed by GitHub
parent 357b96ab0d
commit b3f3259b14
66 changed files with 2177 additions and 6821 deletions

View File

@@ -20,101 +20,213 @@
#include "rpc/Errors.hpp"
#include "util/LoggerFixtures.hpp"
#include "util/NameGenerator.hpp"
#include "util/Taggable.hpp"
#include "util/config/ConfigDefinition.hpp"
#include "util/config/ConfigValue.hpp"
#include "util/config/Types.hpp"
#include "web/Request.hpp"
#include "web/impl/ErrorHandling.hpp"
#include "web/interface/ConnectionBaseMock.hpp"
#include <boost/beast/http/field.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/verb.hpp>
#include <boost/json/object.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serialize.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <variant>
using namespace web::impl;
using namespace web;
using namespace util::config;
namespace http = boost::beast::http;
struct ErrorHandlingTests : NoLoggerFixture {
protected:
util::TagDecoratorFactory tagFactory_{ClioConfigDefinition{
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
}};
std::string const clientIp_ = "some ip";
ConnectionBaseStrictMockPtr connection_ =
std::make_shared<testing::StrictMock<ConnectionBaseMock>>(tagFactory_, clientIp_);
static Request
makeRequest(bool isHttp, std::optional<std::string> body = std::nullopt)
{
if (isHttp)
return Request{http::request<http::string_body>{http::verb::post, "/", 11, body.value_or("")}};
static Request::HttpHeaders const kHEADERS;
return Request{body.value_or(""), kHEADERS};
}
};
struct ErrorHandlingComposeErrorTestBundle {
struct ErrorHandlingMakeErrorTestBundle {
std::string testName;
bool connectionUpgraded;
std::optional<boost::json::object> request;
bool isHttp;
rpc::Status status;
std::string expectedMessage;
boost::beast::http::status expectedStatus;
};
struct ErrorHandlingMakeErrorTest : ErrorHandlingTests,
testing::WithParamInterface<ErrorHandlingMakeErrorTestBundle> {};
TEST_P(ErrorHandlingMakeErrorTest, MakeError)
{
auto const request = makeRequest(GetParam().isHttp);
ErrorHelper const errorHelper{request};
auto response = errorHelper.makeError(GetParam().status);
EXPECT_EQ(response.message(), GetParam().expectedMessage);
if (GetParam().isHttp) {
auto const httpResponse = std::move(response).intoHttpResponse();
EXPECT_EQ(httpResponse.result(), GetParam().expectedStatus);
std::string expectedContentType = "text/html";
if (std::holds_alternative<rpc::RippledError>(GetParam().status.code))
expectedContentType = "application/json";
EXPECT_EQ(httpResponse.at(http::field::content_type), expectedContentType);
}
}
INSTANTIATE_TEST_CASE_P(
ErrorHandlingMakeErrorTestGroup,
ErrorHandlingMakeErrorTest,
testing::ValuesIn({
ErrorHandlingMakeErrorTestBundle{
"WsRequest",
false,
rpc::Status{rpc::RippledError::rpcTOO_BUSY},
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON",
boost::beast::http::status::ok
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_InvalidApiVersion",
true,
rpc::Status{rpc::ClioError::RpcInvalidApiVersion},
"invalid_API_version",
boost::beast::http::status::bad_request
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_CommandIsMissing",
true,
rpc::Status{rpc::ClioError::RpcCommandIsMissing},
"Null method",
boost::beast::http::status::bad_request
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_CommandIsEmpty",
true,
rpc::Status{rpc::ClioError::RpcCommandIsEmpty},
"method is empty",
boost::beast::http::status::bad_request
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_CommandNotString",
true,
rpc::Status{rpc::ClioError::RpcCommandNotString},
"method is not string",
boost::beast::http::status::bad_request
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_ParamsUnparsable",
true,
rpc::Status{rpc::ClioError::RpcParamsUnparsable},
"params unparsable",
boost::beast::http::status::bad_request
},
ErrorHandlingMakeErrorTestBundle{
"HttpRequest_RippledError",
true,
rpc::Status{rpc::RippledError::rpcTOO_BUSY},
R"JSON({"result":{"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"}})JSON",
boost::beast::http::status::bad_request
},
}),
tests::util::kNAME_GENERATOR
);
struct ErrorHandlingMakeInternalErrorTestBundle {
std::string testName;
bool isHttp;
std::optional<std::string> request;
boost::json::object expectedResult;
};
struct ErrorHandlingComposeErrorTest : ErrorHandlingTests,
testing::WithParamInterface<ErrorHandlingComposeErrorTestBundle> {};
struct ErrorHandlingMakeInternalErrorTest : ErrorHandlingTests,
testing::WithParamInterface<ErrorHandlingMakeInternalErrorTestBundle> {};
TEST_P(ErrorHandlingComposeErrorTest, composeError)
TEST_P(ErrorHandlingMakeInternalErrorTest, ComposeError)
{
connection_->upgraded = GetParam().connectionUpgraded;
ErrorHelper const errorHelper{connection_, GetParam().request};
auto const result = errorHelper.composeError(rpc::RippledError::rpcNOT_READY);
EXPECT_EQ(boost::json::serialize(result), boost::json::serialize(GetParam().expectedResult));
auto const request = makeRequest(GetParam().isHttp, GetParam().request);
std::optional<boost::json::object> const requestJson = GetParam().request.has_value()
? std::make_optional(boost::json::parse(*GetParam().request).as_object())
: std::nullopt;
ErrorHelper const errorHelper{request, requestJson};
auto response = errorHelper.makeInternalError();
EXPECT_EQ(response.message(), boost::json::serialize(GetParam().expectedResult));
if (GetParam().isHttp) {
auto const httpResponse = std::move(response).intoHttpResponse();
EXPECT_EQ(httpResponse.result(), boost::beast::http::status::internal_server_error);
EXPECT_EQ(httpResponse.at(http::field::content_type), "application/json");
}
}
INSTANTIATE_TEST_CASE_P(
ErrorHandlingComposeErrorTestGroup,
ErrorHandlingComposeErrorTest,
ErrorHandlingMakeInternalErrorTest,
testing::ValuesIn(
{ErrorHandlingComposeErrorTestBundle{
"NoRequest_UpgradedConnection",
true,
{ErrorHandlingMakeInternalErrorTestBundle{
"NoRequest_WebsocketConnection",
false,
std::nullopt,
{{"error", "notReady"},
{"error_code", 13},
{"error_message", "Not ready to handle this request."},
{{"error", "internal"},
{"error_code", 73},
{"error_message", "Internal error."},
{"status", "error"},
{"type", "response"}}
},
ErrorHandlingComposeErrorTestBundle{
"NoRequest_NotUpgradedConnection",
false,
ErrorHandlingMakeInternalErrorTestBundle{
"NoRequest_HttpConnection",
true,
std::nullopt,
{{"result",
{{"error", "notReady"},
{"error_code", 13},
{"error_message", "Not ready to handle this request."},
{{"error", "internal"},
{"error_code", 73},
{"error_message", "Internal error."},
{"status", "error"},
{"type", "response"}}}}
},
ErrorHandlingComposeErrorTestBundle{
"Request_UpgradedConnection",
true,
boost::json::object{{"id", 1}, {"api_version", 2}},
{{"error", "notReady"},
{"error_code", 13},
{"error_message", "Not ready to handle this request."},
ErrorHandlingMakeInternalErrorTestBundle{
"Request_WebsocketConnection",
false,
std::string{R"JSON({"id": 1, "api_version": 2})JSON"},
{{"error", "internal"},
{"error_code", 73},
{"error_message", "Internal error."},
{"status", "error"},
{"type", "response"},
{"id", 1},
{"api_version", 2},
{"request", {{"id", 1}, {"api_version", 2}}}}
},
ErrorHandlingComposeErrorTestBundle{
"Request_NotUpgradedConnection",
ErrorHandlingMakeInternalErrorTestBundle{
"Request_WebsocketConnection_NoId",
false,
boost::json::object{{"id", 1}, {"api_version", 2}},
std::string{R"JSON({"api_version": 2})JSON"},
{{"error", "internal"},
{"error_code", 73},
{"error_message", "Internal error."},
{"status", "error"},
{"type", "response"},
{"api_version", 2},
{"request", {{"api_version", 2}}}}
},
ErrorHandlingMakeInternalErrorTestBundle{
"Request_HttpConnection",
true,
std::string{R"JSON({"id": 1, "api_version": 2})JSON"},
{{"result",
{{"error", "notReady"},
{"error_code", 13},
{"error_message", "Not ready to handle this request."},
{{"error", "internal"},
{"error_code", 73},
{"error_message", "Internal error."},
{"status", "error"},
{"type", "response"},
{"id", 1},
@@ -124,169 +236,122 @@ INSTANTIATE_TEST_CASE_P(
tests::util::kNAME_GENERATOR
);
struct ErrorHandlingSendErrorTestBundle {
TEST_F(ErrorHandlingTests, MakeNotReadyError)
{
auto const request = makeRequest(true);
auto response = ErrorHelper{request}.makeNotReadyError();
EXPECT_EQ(
response.message(),
std::string{
R"JSON({"result":{"error":"notReady","error_code":13,"error_message":"Not ready to handle this request.","status":"error","type":"response"}})JSON"
}
);
auto const httpResponse = std::move(response).intoHttpResponse();
EXPECT_EQ(httpResponse.result(), http::status::ok);
EXPECT_EQ(httpResponse.at(http::field::content_type), "application/json");
}
TEST_F(ErrorHandlingTests, MakeTooBusyError_WebsocketRequest)
{
auto const request = makeRequest(false);
auto response = ErrorHelper{request}.makeTooBusyError();
EXPECT_EQ(
response.message(),
std::string{
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON"
}
);
}
TEST_F(ErrorHandlingTests, sendTooBusyError_HttpConnection)
{
auto const request = makeRequest(true);
auto response = ErrorHelper{request}.makeTooBusyError();
EXPECT_EQ(
response.message(),
std::string{
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON"
}
);
auto const httpResponse = std::move(response).intoHttpResponse();
EXPECT_EQ(httpResponse.result(), boost::beast::http::status::service_unavailable);
EXPECT_EQ(httpResponse.at(http::field::content_type), "application/json");
}
TEST_F(ErrorHandlingTests, makeJsonParsingError_WebsocketConnection)
{
auto const request = makeRequest(false);
auto response = ErrorHelper{request}.makeJsonParsingError();
EXPECT_EQ(
response.message(),
std::string{
R"JSON({"error":"badSyntax","error_code":1,"error_message":"Syntax error.","status":"error","type":"response"})JSON"
}
);
}
TEST_F(ErrorHandlingTests, makeJsonParsingError_HttpConnection)
{
auto const request = makeRequest(true);
auto response = ErrorHelper{request}.makeJsonParsingError();
EXPECT_EQ(response.message(), std::string{"Unable to parse JSON from the request"});
auto const httpResponse = std::move(response).intoHttpResponse();
EXPECT_EQ(httpResponse.result(), boost::beast::http::status::bad_request);
EXPECT_EQ(httpResponse.at(http::field::content_type), "text/html");
}
struct ErrorHandlingComposeErrorTestBundle {
std::string testName;
bool connectionUpgraded;
rpc::Status status;
bool isHttp;
std::optional<boost::json::object> request;
std::string expectedMessage;
boost::beast::http::status expectedStatus;
};
struct ErrorHandlingSendErrorTest : ErrorHandlingTests,
testing::WithParamInterface<ErrorHandlingSendErrorTestBundle> {};
struct ErrorHandlingComposeErrorTest : ErrorHandlingTests,
testing::WithParamInterface<ErrorHandlingComposeErrorTestBundle> {};
TEST_P(ErrorHandlingSendErrorTest, sendError)
TEST_P(ErrorHandlingComposeErrorTest, ComposeError)
{
connection_->upgraded = GetParam().connectionUpgraded;
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(*connection_, send(std::string{GetParam().expectedMessage}, GetParam().expectedStatus));
errorHelper.sendError(GetParam().status);
auto const request = makeRequest(GetParam().isHttp);
ErrorHelper const errorHelper{request, GetParam().request};
auto const response = errorHelper.composeError(rpc::Status{rpc::RippledError::rpcINTERNAL});
EXPECT_EQ(boost::json::serialize(response), GetParam().expectedMessage);
}
INSTANTIATE_TEST_CASE_P(
ErrorHandlingSendErrorTestGroup,
ErrorHandlingSendErrorTest,
testing::ValuesIn({
ErrorHandlingSendErrorTestBundle{
"UpgradedConnection",
true,
rpc::Status{rpc::RippledError::rpcTOO_BUSY},
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON",
boost::beast::http::status::ok
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_InvalidApiVersion",
false,
rpc::Status{rpc::ClioError::RpcInvalidApiVersion},
"invalid_API_version",
boost::beast::http::status::bad_request
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_CommandIsMissing",
false,
rpc::Status{rpc::ClioError::RpcCommandIsMissing},
"Null method",
boost::beast::http::status::bad_request
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_CommandIsEmpty",
false,
rpc::Status{rpc::ClioError::RpcCommandIsEmpty},
"method is empty",
boost::beast::http::status::bad_request
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_CommandNotString",
false,
rpc::Status{rpc::ClioError::RpcCommandNotString},
"method is not string",
boost::beast::http::status::bad_request
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_ParamsUnparsable",
false,
rpc::Status{rpc::ClioError::RpcParamsUnparsable},
"params unparsable",
boost::beast::http::status::bad_request
},
ErrorHandlingSendErrorTestBundle{
"NotUpgradedConnection_RippledError",
false,
rpc::Status{rpc::RippledError::rpcTOO_BUSY},
R"JSON({"result":{"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"}})JSON",
boost::beast::http::status::bad_request
},
}),
ErrorHandlingComposeErrorTestGroup,
ErrorHandlingComposeErrorTest,
testing::ValuesIn(
{ErrorHandlingComposeErrorTestBundle{
"NoRequest_WebsocketConnection",
false,
std::nullopt,
R"JSON({"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response"})JSON"
},
ErrorHandlingComposeErrorTestBundle{
"NoRequest_HttpConnection",
true,
std::nullopt,
R"JSON({"result":{"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response"}})JSON"
},
ErrorHandlingComposeErrorTestBundle{
"Request_WebsocketConnection",
false,
boost::json::object{{"id", 1}, {"api_version", 2}},
R"JSON({"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response","id":1,"api_version":2,"request":{"id":1,"api_version":2}})JSON",
},
ErrorHandlingComposeErrorTestBundle{
"Request_WebsocketConnection_NoId",
false,
boost::json::object{{"api_version", 2}},
R"JSON({"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response","api_version":2,"request":{"api_version":2}})JSON",
},
ErrorHandlingComposeErrorTestBundle{
"Request_HttpConnection",
true,
boost::json::object{{"id", 1}, {"api_version", 2}},
R"JSON({"result":{"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response","id":1,"request":{"id":1,"api_version":2}}})JSON"
}}
),
tests::util::kNAME_GENERATOR
);
TEST_F(ErrorHandlingTests, sendInternalError)
{
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(
std::string{
R"JSON({"result":{"error":"internal","error_code":73,"error_message":"Internal error.","status":"error","type":"response"}})JSON"
},
boost::beast::http::status::internal_server_error
)
);
errorHelper.sendInternalError();
}
TEST_F(ErrorHandlingTests, sendNotReadyError)
{
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(
std::string{
R"JSON({"result":{"error":"notReady","error_code":13,"error_message":"Not ready to handle this request.","status":"error","type":"response"}})JSON"
},
boost::beast::http::status::ok
)
);
errorHelper.sendNotReadyError();
}
TEST_F(ErrorHandlingTests, sendTooBusyError_UpgradedConnection)
{
connection_->upgraded = true;
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(
std::string{
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON"
},
boost::beast::http::status::ok
)
);
errorHelper.sendTooBusyError();
}
TEST_F(ErrorHandlingTests, sendTooBusyError_NotUpgradedConnection)
{
connection_->upgraded = false;
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(
std::string{
R"JSON({"error":"tooBusy","error_code":9,"error_message":"The server is too busy to help you now.","status":"error","type":"response"})JSON"
},
boost::beast::http::status::service_unavailable
)
);
errorHelper.sendTooBusyError();
}
TEST_F(ErrorHandlingTests, sendJsonParsingError_UpgradedConnection)
{
connection_->upgraded = true;
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(
std::string{
R"JSON({"error":"badSyntax","error_code":1,"error_message":"Syntax error.","status":"error","type":"response"})JSON"
},
boost::beast::http::status::ok
)
);
errorHelper.sendJsonParsingError();
}
TEST_F(ErrorHandlingTests, sendJsonParsingError_NotUpgradedConnection)
{
connection_->upgraded = false;
ErrorHelper const errorHelper{connection_};
EXPECT_CALL(
*connection_,
send(std::string{"Unable to parse JSON from the request"}, boost::beast::http::status::bad_request)
);
errorHelper.sendJsonParsingError();
}