mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 01:08:01 +00:00
@@ -112,7 +112,8 @@ if(BUILD_TESTS)
|
|||||||
unittests/rpc/ErrorTests.cpp
|
unittests/rpc/ErrorTests.cpp
|
||||||
unittests/rpc/BaseTests.cpp
|
unittests/rpc/BaseTests.cpp
|
||||||
unittests/rpc/handlers/TestHandlerTests.cpp
|
unittests/rpc/handlers/TestHandlerTests.cpp
|
||||||
unittests/rpc/handlers/DefaultProcessorTests.cpp)
|
unittests/rpc/handlers/DefaultProcessorTests.cpp
|
||||||
|
unittests/rpc/handlers/PingTest.cpp)
|
||||||
include(CMake/deps/gtest.cmake)
|
include(CMake/deps/gtest.cmake)
|
||||||
|
|
||||||
# if CODE_COVERAGE enable, add clio_test-ccov
|
# if CODE_COVERAGE enable, add clio_test-ccov
|
||||||
|
|||||||
@@ -49,12 +49,21 @@ concept Requirement = requires(T a) {
|
|||||||
*/
|
*/
|
||||||
// clang-format off
|
// clang-format off
|
||||||
template <typename T>
|
template <typename T>
|
||||||
concept Handler = requires(T a, typename T::Input in, typename T::Output out) {
|
concept HandlerWithInput = requires(T a, typename T::Input in, typename T::Output out) {
|
||||||
{ a.spec() } -> std::same_as<RpcSpecConstRef>;
|
{ a.spec() } -> std::same_as<RpcSpecConstRef>;
|
||||||
{ a.process(in) } -> std::same_as<HandlerReturnType<decltype(out)>>;
|
{ a.process(in) } -> std::same_as<HandlerReturnType<decltype(out)>>;}
|
||||||
}
|
&& boost::json::has_value_to<typename T::Input>::value;
|
||||||
&& boost::json::has_value_from<typename T::Output>::value
|
|
||||||
&& boost::json::has_value_to<typename T::Input>::value;
|
template <typename T>
|
||||||
|
concept HandlerWithoutInput = requires(T a, typename T::Output out) {
|
||||||
|
{ a.process() } -> std::same_as<HandlerReturnType<decltype(out)>>;};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept Handler =
|
||||||
|
(HandlerWithInput<T>
|
||||||
|
||
|
||||||
|
HandlerWithoutInput<T>)
|
||||||
|
&& boost::json::has_value_from<typename T::Output>::value;
|
||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
} // namespace RPCng
|
} // namespace RPCng
|
||||||
|
|||||||
@@ -53,4 +53,14 @@ struct FieldSpec;
|
|||||||
|
|
||||||
using RpcSpecConstRef = RpcSpec const&;
|
using RpcSpecConstRef = RpcSpec const&;
|
||||||
|
|
||||||
|
struct VoidOutput
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void
|
||||||
|
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, VoidOutput)
|
||||||
|
{
|
||||||
|
jv = boost::json::object{};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace RPCng
|
} // namespace RPCng
|
||||||
|
|||||||
@@ -24,6 +24,9 @@
|
|||||||
|
|
||||||
namespace RPCng::detail {
|
namespace RPCng::detail {
|
||||||
|
|
||||||
|
template <typename>
|
||||||
|
static constexpr bool unsupported_handler_v = false;
|
||||||
|
|
||||||
template <Handler HandlerType>
|
template <Handler HandlerType>
|
||||||
struct DefaultProcessor final
|
struct DefaultProcessor final
|
||||||
{
|
{
|
||||||
@@ -33,19 +36,35 @@ struct DefaultProcessor final
|
|||||||
{
|
{
|
||||||
using boost::json::value_from;
|
using boost::json::value_from;
|
||||||
using boost::json::value_to;
|
using boost::json::value_to;
|
||||||
|
if constexpr (HandlerWithInput<HandlerType>)
|
||||||
|
{
|
||||||
|
// first we run validation
|
||||||
|
auto const spec = handler.spec();
|
||||||
|
if (auto const ret = spec.validate(value); not ret)
|
||||||
|
return Error{ret.error()}; // forward Status
|
||||||
|
|
||||||
// first we run validation
|
auto const inData = value_to<typename HandlerType::Input>(value);
|
||||||
auto const spec = handler.spec();
|
|
||||||
if (auto const ret = spec.validate(value); not ret)
|
|
||||||
return Error{ret.error()}; // forward Status
|
|
||||||
|
|
||||||
auto const inData = value_to<typename HandlerType::Input>(value);
|
// real handler is given expected Input, not json
|
||||||
|
if (auto const ret = handler.process(inData); not ret)
|
||||||
// real handler is given expected Input, not json
|
return Error{ret.error()}; // forward Status
|
||||||
if (auto const ret = handler.process(inData); not ret)
|
else
|
||||||
return Error{ret.error()}; // forward Status
|
return value_from(ret.value());
|
||||||
|
}
|
||||||
|
else if constexpr (HandlerWithoutInput<HandlerType>)
|
||||||
|
{
|
||||||
|
// no input to pass, ignore the value
|
||||||
|
if (auto const ret = handler.process(); not ret)
|
||||||
|
return Error{ret.error()}; // forward Status
|
||||||
|
else
|
||||||
|
return value_from(ret.value());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
return value_from(ret.value());
|
{
|
||||||
|
// when concept HandlerWithInput and HandlerWithoutInput not cover
|
||||||
|
// all Handler case
|
||||||
|
static_assert(unsupported_handler_v<HandlerType>);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
38
src/rpc/ngHandlers/Ping.h
Normal file
38
src/rpc/ngHandlers/Ping.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
This file is part of clio: https://github.com/XRPLF/clio
|
||||||
|
Copyright (c) 2023, the clio developers.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
//==============================================================================
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <rpc/common/Types.h>
|
||||||
|
|
||||||
|
namespace RPCng {
|
||||||
|
|
||||||
|
class PingHandler
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Output = VoidOutput;
|
||||||
|
using Result = HandlerReturnType<Output>;
|
||||||
|
|
||||||
|
Result
|
||||||
|
process() const
|
||||||
|
{
|
||||||
|
return Output{};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace RPCng
|
||||||
@@ -52,6 +52,19 @@ TEST_F(RPCDefaultProcessorTest, ValidInput)
|
|||||||
ASSERT_TRUE(ret); // no error
|
ASSERT_TRUE(ret); // no error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(RPCDefaultProcessorTest, NoInputVaildCall)
|
||||||
|
{
|
||||||
|
HandlerWithoutInputMock handler;
|
||||||
|
RPCng::detail::DefaultProcessor<HandlerWithoutInputMock> processor;
|
||||||
|
|
||||||
|
auto const data = InOutFake{"works"};
|
||||||
|
auto const input = json::parse(R"({})");
|
||||||
|
EXPECT_CALL(handler, process()).WillOnce(Return(data));
|
||||||
|
|
||||||
|
auto const ret = processor(handler, input);
|
||||||
|
ASSERT_TRUE(ret); // no error
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(RPCDefaultProcessorTest, InvalidInput)
|
TEST_F(RPCDefaultProcessorTest, InvalidInput)
|
||||||
{
|
{
|
||||||
HandlerMock handler;
|
HandlerMock handler;
|
||||||
|
|||||||
37
unittests/rpc/handlers/PingTest.cpp
Normal file
37
unittests/rpc/handlers/PingTest.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
/*
|
||||||
|
This file is part of clio: https://github.com/XRPLF/clio
|
||||||
|
Copyright (c) 2023, the clio developers.
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
//==============================================================================
|
||||||
|
|
||||||
|
#include <rpc/common/AnyHandler.h>
|
||||||
|
#include <rpc/ngHandlers/Ping.h>
|
||||||
|
#include <util/Fixtures.h>
|
||||||
|
|
||||||
|
using namespace RPCng;
|
||||||
|
|
||||||
|
class RPCHandlerTest : public NoLoggerFixture
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
// example handler tests
|
||||||
|
TEST_F(RPCHandlerTest, Ping)
|
||||||
|
{
|
||||||
|
auto const handler = AnyHandler{PingHandler{}};
|
||||||
|
auto const output = handler.process(boost::json::parse(R"({})"));
|
||||||
|
ASSERT_TRUE(output);
|
||||||
|
EXPECT_EQ(output.value(), boost::json::parse(R"({})"));
|
||||||
|
}
|
||||||
@@ -50,6 +50,16 @@ TEST_F(RPCTestHandlerTest, HandlerSuccess)
|
|||||||
EXPECT_EQ(val.as_object().at("computed").as_string(), "world_10");
|
EXPECT_EQ(val.as_object().at("computed").as_string(), "world_10");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(RPCTestHandlerTest, NoInputHandlerSuccess)
|
||||||
|
{
|
||||||
|
auto const handler = AnyHandler{NoInputHandlerFake{}};
|
||||||
|
auto const output = handler.process(json::parse(R"({})"));
|
||||||
|
ASSERT_TRUE(output);
|
||||||
|
|
||||||
|
auto const val = output.value();
|
||||||
|
EXPECT_EQ(val.as_object().at("computed").as_string(), "test");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(RPCTestHandlerTest, HandlerErrorHandling)
|
TEST_F(RPCTestHandlerTest, HandlerErrorHandling)
|
||||||
{
|
{
|
||||||
auto const handler = AnyHandler{HandlerFake{}};
|
auto const handler = AnyHandler{HandlerFake{}};
|
||||||
|
|||||||
@@ -98,6 +98,19 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class NoInputHandlerFake
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using Output = TestOutput;
|
||||||
|
using Result = RPCng::HandlerReturnType<Output>;
|
||||||
|
|
||||||
|
Result
|
||||||
|
process() const
|
||||||
|
{
|
||||||
|
return Output{"test"};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// example handler that returns custom error
|
// example handler that returns custom error
|
||||||
class FailingHandlerFake
|
class FailingHandlerFake
|
||||||
{
|
{
|
||||||
@@ -165,4 +178,12 @@ struct HandlerMock
|
|||||||
MOCK_METHOD(Result, process, (Input), (const));
|
MOCK_METHOD(Result, process, (Input), (const));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct HandlerWithoutInputMock
|
||||||
|
{
|
||||||
|
using Output = InOutFake;
|
||||||
|
using Result = RPCng::HandlerReturnType<Output>;
|
||||||
|
|
||||||
|
MOCK_METHOD(Result, process, (), (const));
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace unittests::detail
|
} // namespace unittests::detail
|
||||||
|
|||||||
Reference in New Issue
Block a user