mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-01 00:55:51 +00:00
@@ -31,83 +31,73 @@ using namespace unittests::detail;
|
||||
|
||||
namespace json = boost::json;
|
||||
|
||||
class RPCTestHandlerTest : public NoLoggerFixture
|
||||
class RPCTestHandlerTest : public HandlerBaseTest
|
||||
{
|
||||
};
|
||||
|
||||
// example handler tests
|
||||
TEST_F(RPCTestHandlerTest, HandlerSuccess)
|
||||
{
|
||||
auto const handler = AnyHandler{HandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "world",
|
||||
"limit": 10
|
||||
})");
|
||||
runSpawn([](auto& yield) {
|
||||
auto const handler = AnyHandler{HandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "world",
|
||||
"limit": 10
|
||||
})");
|
||||
|
||||
auto const output = handler.process(input);
|
||||
ASSERT_TRUE(output);
|
||||
|
||||
auto const val = output.value();
|
||||
EXPECT_EQ(val.as_object().at("computed").as_string(), "world_10");
|
||||
}
|
||||
|
||||
TEST_F(RPCTestHandlerTest, CoroutineHandlerSuccess)
|
||||
{
|
||||
auto const handler = AnyHandler{CoroutineHandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "world",
|
||||
"limit": 10
|
||||
})");
|
||||
boost::asio::io_context ctx;
|
||||
boost::asio::spawn(ctx, [&](boost::asio::yield_context yield) {
|
||||
auto const output = handler.process(input, Context{std::ref(yield)});
|
||||
ASSERT_TRUE(output);
|
||||
|
||||
auto const val = output.value();
|
||||
EXPECT_EQ(val.as_object().at("computed").as_string(), "world_10");
|
||||
});
|
||||
ctx.run();
|
||||
}
|
||||
|
||||
TEST_F(RPCTestHandlerTest, NoInputHandlerSuccess)
|
||||
{
|
||||
auto const handler = AnyHandler{NoInputHandlerFake{}};
|
||||
auto const output = handler.process(json::parse(R"({})"));
|
||||
ASSERT_TRUE(output);
|
||||
runSpawn([](auto& yield) {
|
||||
auto const handler = AnyHandler{NoInputHandlerFake{}};
|
||||
auto const output = handler.process(json::parse(R"({})"), Context{std::ref(yield)});
|
||||
ASSERT_TRUE(output);
|
||||
|
||||
auto const val = output.value();
|
||||
EXPECT_EQ(val.as_object().at("computed").as_string(), "test");
|
||||
auto const val = output.value();
|
||||
EXPECT_EQ(val.as_object().at("computed").as_string(), "test");
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(RPCTestHandlerTest, HandlerErrorHandling)
|
||||
{
|
||||
auto const handler = AnyHandler{HandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "not world",
|
||||
"limit": 10
|
||||
})");
|
||||
runSpawn([](auto& yield) {
|
||||
auto const handler = AnyHandler{HandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "not world",
|
||||
"limit": 10
|
||||
})");
|
||||
|
||||
auto const output = handler.process(input);
|
||||
ASSERT_FALSE(output);
|
||||
auto const output = handler.process(input, Context{std::ref(yield)});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = RPC::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "invalidParams");
|
||||
EXPECT_EQ(err.at("error_message").as_string(), "Invalid parameters.");
|
||||
EXPECT_EQ(err.at("error_code").as_uint64(), 31);
|
||||
auto const err = RPC::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "invalidParams");
|
||||
EXPECT_EQ(err.at("error_message").as_string(), "Invalid parameters.");
|
||||
EXPECT_EQ(err.at("error_code").as_uint64(), 31);
|
||||
});
|
||||
}
|
||||
|
||||
TEST_F(RPCTestHandlerTest, HandlerInnerErrorHandling)
|
||||
{
|
||||
auto const handler = AnyHandler{FailingHandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "world",
|
||||
"limit": 10
|
||||
})");
|
||||
runSpawn([](auto& yield) {
|
||||
auto const handler = AnyHandler{FailingHandlerFake{}};
|
||||
auto const input = json::parse(R"({
|
||||
"hello": "world",
|
||||
"limit": 10
|
||||
})");
|
||||
|
||||
// validation succeeds but handler itself returns error
|
||||
auto const output = handler.process(input);
|
||||
ASSERT_FALSE(output);
|
||||
// validation succeeds but handler itself returns error
|
||||
auto const output = handler.process(input, Context{std::ref(yield)});
|
||||
ASSERT_FALSE(output);
|
||||
|
||||
auto const err = RPC::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "Very custom error");
|
||||
auto const err = RPC::makeError(output.error());
|
||||
EXPECT_EQ(err.at("error").as_string(), "Very custom error");
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user