2020-06-28 17:43:08 +00:00
|
|
|
#include "webfuse/impl/jsonrpc/response_intern.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-06-28 13:51:34 +00:00
|
|
|
#include "webfuse/status.h"
|
2020-07-18 21:16:18 +00:00
|
|
|
#include "webfuse/test_util/json_doc.hpp"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
using webfuse_test::JsonDoc;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_json_response, init_result)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"result\": 47, \"id\": 11}");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_EQ(nullptr, response.error);
|
2020-07-18 21:16:18 +00:00
|
|
|
ASSERT_TRUE(wf_impl_json_is_int(response.result));
|
|
|
|
ASSERT_EQ(47, wf_impl_json_int_get(response.result));
|
2019-05-19 12:33:42 +00:00
|
|
|
ASSERT_EQ(11, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
TEST(wf_json_response, init_error)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error));
|
|
|
|
ASSERT_STREQ("Don't Panic!", wf_impl_jsonrpc_error_message(response.error));
|
2019-05-19 12:33:42 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(23, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 11:51:54 +00:00
|
|
|
TEST(wf_json_response, init_fail_missing_result_and_error)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"id\": 12}");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-03-01 15:55:58 +00:00
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2019-05-19 12:33:42 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(12, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
TEST(wf_json_response, init_fail_missing_id)
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"result\": 47}");
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2020-03-22 11:51:54 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2020-03-22 11:51:54 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2020-03-22 11:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wf_json_response, init_fail_wrong_id_type)
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"result\": 47, \"id\": \"42\"}");
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2020-03-22 11:51:54 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2020-03-22 11:51:54 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(-1, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2020-03-22 11:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wf_json_response, init_fail_error_missing_code)
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"error\": {\"message\": \"Don't Panic!\"}, \"id\": 23}");
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2020-03-22 11:51:54 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2020-03-22 11:51:54 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(23, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2020-03-22 11:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wf_json_response, init_fail_error_wrong_code_type)
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"error\": {\"code\": \"42\", \"message\": \"Don't Panic!\"}, \"id\": 23}");
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2020-03-22 11:51:54 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2020-03-22 11:51:54 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(23, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2020-03-22 11:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(wf_json_response, init_fail_error_wrong_type)
|
|
|
|
{
|
2020-07-18 21:16:18 +00:00
|
|
|
JsonDoc doc("{\"error\": \"invalid error type\", \"id\": 23}");
|
2020-03-22 11:51:54 +00:00
|
|
|
|
|
|
|
struct wf_jsonrpc_response response;
|
2020-07-18 21:16:18 +00:00
|
|
|
wf_impl_jsonrpc_response_init(&response, doc.root());
|
2020-03-22 11:51:54 +00:00
|
|
|
|
2020-07-16 17:30:18 +00:00
|
|
|
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
|
2020-03-22 11:51:54 +00:00
|
|
|
ASSERT_EQ(nullptr, response.result);
|
|
|
|
ASSERT_EQ(23, response.id);
|
|
|
|
|
2020-06-28 17:43:08 +00:00
|
|
|
wf_impl_jsonrpc_response_cleanup(&response);
|
2020-03-22 11:51:54 +00:00
|
|
|
}
|