From a544293fe6cc6a786c6059e2923e620e94f3b51d Mon Sep 17 00:00:00 2001 From: Yaniv Mordekhay Date: Wed, 19 Aug 2020 11:47:48 +0300 Subject: [PATCH] Differentiate calls with no params and calls with empty list param --- include/jsonrpccxx/client.hpp | 8 +++++-- test/client.cpp | 42 ++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/include/jsonrpccxx/client.hpp b/include/jsonrpccxx/client.hpp index 1cf95a3..7079a57 100644 --- a/include/jsonrpccxx/client.hpp +++ b/include/jsonrpccxx/client.hpp @@ -23,7 +23,9 @@ namespace jsonrpccxx { virtual ~JsonRpcClient() = default; template - T CallMethod(const id_type &id, const std::string &name, const positional_parameter ¶ms = {}) { return call_method(id, name, params).result.get(); } + T CallMethod(const id_type &id, const std::string &name) { return call_method(id, name, json::object()).result.get(); } + template + T CallMethod(const id_type &id, const std::string &name, const positional_parameter ¶ms) { return call_method(id, name, params).result.get(); } template T CallMethodNamed(const id_type &id, const std::string &name, const named_parameter ¶ms = {}) { return call_method(id, name, params).result.get(); } @@ -48,6 +50,8 @@ namespace jsonrpccxx { } if (!params.empty() && !params.is_null()) { j["params"] = params; + } else if (params.is_array()) { + j["params"] = params; } else if (v == version::v1) { j["params"] = nullptr; } @@ -83,4 +87,4 @@ namespace jsonrpccxx { connector.Send(j.dump()); } }; -} // namespace jsonrpccxx \ No newline at end of file +} // namespace jsonrpccxx diff --git a/test/client.cpp b/test/client.cpp index 033af0e..79171d7 100644 --- a/test/client.cpp +++ b/test/client.cpp @@ -17,11 +17,6 @@ struct F { }; TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) { - c.SetResult(true); - clientV2.CallMethod("000-000-000", "some.method_1", {}); - c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000"); - CHECK(!has_key(c.request, "params")); - c.SetResult(true); clientV2.CallMethod("000-000-000", "some.method_1"); c.VerifyMethodRequest(version::v2, "some.method_1", "000-000-000"); @@ -30,14 +25,41 @@ TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) { TEST_CASE_METHOD(F, "v1_method_noparams", TEST_MODULE) { c.SetResult(true); - clientV1.CallMethod(37, "some.method_1", {}); + clientV1.CallMethod(37, "some.method_1"); c.VerifyMethodRequest(version::v1, "some.method_1", 37); CHECK(has_key_type(c.request, "params", json::value_t::null)); +} +TEST_CASE_METHOD(F, "v2_method_call_params_empty", TEST_MODULE) { c.SetResult(true); - clientV1.CallMethod(37, "some.method_1"); - c.VerifyMethodRequest(version::v1, "some.method_1", 37); - CHECK(has_key_type(c.request, "params", json::value_t::null)); + clientV2.CallMethod("1", "some.method_1", {}); + c.VerifyMethodRequest(version::v2, "some.method_1", "1"); + CHECK(c.request["params"].is_array()); + CHECK(c.request["params"].empty()); + CHECK(c.request["params"].dump() == "[]"); + + c.SetResult(true); + clientV2.CallMethod("1", "some.method_1", json::array()); + c.VerifyMethodRequest(version::v2, "some.method_1", "1"); + CHECK(c.request["params"].is_array()); + CHECK(c.request["params"].empty()); + CHECK(c.request["params"].dump() == "[]"); +} + +TEST_CASE_METHOD(F, "v1_method_call_params_empty", TEST_MODULE) { + c.SetResult(true); + clientV1.CallMethod("1", "some.method_1", {}); + c.VerifyMethodRequest(version::v1, "some.method_1", "1"); + CHECK(c.request["params"].is_array()); + CHECK(c.request["params"].empty()); + CHECK(c.request["params"].dump() == "[]"); + + c.SetResult(true); + clientV1.CallMethod("1", "some.method_1", json::array()); + c.VerifyMethodRequest(version::v1, "some.method_1", "1"); + CHECK(c.request["params"].is_array()); + CHECK(c.request["params"].empty()); + CHECK(c.request["params"].dump() == "[]"); } TEST_CASE_METHOD(F, "v2_method_call_params_byname", TEST_MODULE) { @@ -256,4 +278,4 @@ TEST_CASE_METHOD(F, "v1_notification_call_params_byposition", TEST_MODULE) { CHECK(c.request["params"][2] == true); } -// TODO: test cases with return type mapping and param mapping for v1/v2 method and notification \ No newline at end of file +// TODO: test cases with return type mapping and param mapping for v1/v2 method and notification