2019-05-19 12:33:42 +00:00
|
|
|
#include <gtest/gtest.h>
|
2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/jsonrpc/proxy.h"
|
2020-07-16 17:30:18 +00:00
|
|
|
#include "webfuse/impl/jsonrpc/error.h"
|
2020-07-18 21:16:18 +00:00
|
|
|
#include "webfuse/impl/json/node.h"
|
2020-07-13 20:41:19 +00:00
|
|
|
#include "webfuse/impl/message.h"
|
2020-06-28 13:51:34 +00:00
|
|
|
#include "webfuse/status.h"
|
2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/timer/manager.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 18:21:27 +00:00
|
|
|
#include "webfuse/jsonrpc/mock_timer.hpp"
|
2020-07-18 21:16:18 +00:00
|
|
|
#include "webfuse/test_util/json_doc.hpp"
|
2020-03-22 19:09:40 +00:00
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
2020-03-22 19:09:40 +00:00
|
|
|
using wf_jsonrpc_test::MockTimer;
|
2020-07-18 21:16:18 +00:00
|
|
|
using webfuse_test::JsonDoc;
|
2020-03-22 19:09:40 +00:00
|
|
|
using testing::Return;
|
|
|
|
using testing::_;
|
|
|
|
using testing::DoAll;
|
|
|
|
using testing::SaveArg;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
#define WF_DEFAULT_TIMEOUT (10 * 1000)
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
struct SendContext
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc;
|
|
|
|
wf_json const * response;
|
2019-05-19 12:33:42 +00:00
|
|
|
bool result;
|
|
|
|
bool is_called;
|
|
|
|
|
|
|
|
explicit SendContext(bool result_ = true)
|
2020-07-18 21:16:18 +00:00
|
|
|
: doc("null")
|
|
|
|
, response(nullptr)
|
2019-05-19 12:33:42 +00:00
|
|
|
, result(result_)
|
|
|
|
, is_called(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool jsonrpc_send(
|
2020-07-13 20:41:19 +00:00
|
|
|
wf_message * request,
|
2019-05-19 12:33:42 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
SendContext * context = reinterpret_cast<SendContext*>(user_data);
|
|
|
|
context->is_called = true;
|
2020-07-18 21:16:18 +00:00
|
|
|
context->doc = std::move(JsonDoc(std::string(request->data, request->length)));
|
|
|
|
context->response = context->doc.root();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
wf_impl_message_dispose(request);
|
2019-05-19 12:33:42 +00:00
|
|
|
return context->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FinishedContext
|
|
|
|
{
|
|
|
|
bool is_called;
|
2020-07-16 17:30:18 +00:00
|
|
|
wf_jsonrpc_error * error;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext()
|
|
|
|
: is_called(false)
|
2020-02-28 22:17:41 +00:00
|
|
|
, error(nullptr)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~FinishedContext()
|
|
|
|
{
|
2020-07-16 17:30:18 +00:00
|
|
|
wf_impl_jsonrpc_error_dispose(error);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void jsonrpc_finished(
|
|
|
|
void * user_data,
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const *,
|
2020-07-16 17:30:18 +00:00
|
|
|
wf_jsonrpc_error const * error)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
|
|
|
|
context->is_called = true;
|
2020-07-16 17:30:18 +00:00
|
|
|
|
|
|
|
if (nullptr != error)
|
|
|
|
{
|
|
|
|
context->error = wf_impl_jsonrpc_error(wf_impl_jsonrpc_error_code(error),
|
|
|
|
wf_impl_jsonrpc_error_message(error));
|
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, init)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext context;
|
|
|
|
void * user_data = reinterpret_cast<void*>(&context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(context.is_called);
|
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, invoke)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * method = wf_impl_json_object_get(send_context.response, "method");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_string(method));
|
|
|
|
ASSERT_STREQ("foo", wf_impl_json_string_get(method));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * params = wf_impl_json_object_get(send_context.response, "params");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_array(params));
|
|
|
|
ASSERT_EQ(2, wf_impl_json_array_size(params));
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_string(wf_impl_json_array_get(params, 0)));
|
|
|
|
ASSERT_STREQ("bar", wf_impl_json_string_get(wf_impl_json_array_get(params, 0)));
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(wf_impl_json_array_get(params, 1)));
|
|
|
|
ASSERT_EQ(42, wf_impl_json_int_get(wf_impl_json_array_get(params, 1)));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(id));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(nullptr == finished_context.error);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, invoke_calls_finish_if_send_fails)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context(false);
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(nullptr == finished_context.error);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 20:22:23 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, invoke_if_another_request_is_pending)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context2;
|
|
|
|
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, invoke_invalid_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, on_result)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(id));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc response("{\"result\": \"okay\", \"id\": " + std::to_string(wf_impl_json_int_get(id)) + "}");
|
|
|
|
wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_EQ(nullptr, finished_context.error);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(id));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc response("{\"result\": \"okay\", \"id\": " + std::to_string(wf_impl_json_int_get(id) + 1) + "}");
|
|
|
|
wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, timeout)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_timer_manager_check(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_TIMEOUT, wf_impl_jsonrpc_error_code(finished_context.error));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, cleanup_pending_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, notify)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_object(send_context.response));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * method = wf_impl_json_object_get(send_context.response, "method");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_string(method));
|
|
|
|
ASSERT_STREQ("foo", wf_impl_json_string_get(method));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * params = wf_impl_json_object_get(send_context.response, "params");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_array(params));
|
|
|
|
ASSERT_EQ(2, wf_impl_json_array_size(params));
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_string(wf_impl_json_array_get(params, 0)));
|
|
|
|
ASSERT_STREQ("bar", wf_impl_json_string_get(wf_impl_json_array_get(params, 0)));
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(wf_impl_json_array_get(params, 1)));
|
|
|
|
ASSERT_EQ(42, wf_impl_json_int_get(wf_impl_json_array_get(params, 1)));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_json const * id = wf_impl_json_object_get(send_context.response, "id");
|
|
|
|
ASSERT_TRUE(wf_impl_json_is_undefined(id));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
TEST(wf_jsonrpc_proxy, notify_send_invalid_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_notify(proxy, "foo", "?");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-13 20:41:19 +00:00
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
|
|
|
{
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_timer_manager * timer_manager = wf_impl_timer_manager_create();
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-28 17:43:08 +00:00
|
|
|
struct wf_jsonrpc_proxy * proxy = wf_impl_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2020-03-22 19:09:40 +00:00
|
|
|
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc response("{\"result\": \"okay\", \"id\": 42}");
|
|
|
|
wf_impl_jsonrpc_proxy_onresult(proxy, response.root());
|
2020-03-22 19:09:40 +00:00
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wf_impl_timer_manager_dispose(timer_manager);
|
2020-03-22 19:09:40 +00:00
|
|
|
}
|
|
|
|
|