1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00
falk-werner_webfuse/test/webfuse/jsonrpc/test_response.cc

124 lines
3.6 KiB
C++
Raw Normal View History

#include "webfuse/impl/jsonrpc/response_intern.h"
#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;
2020-03-01 15:55:58 +00:00
TEST(wf_json_response, init_result)
{
2020-07-18 21:16:18 +00:00
JsonDoc doc("{\"result\": 47, \"id\": 11}");
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());
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));
ASSERT_EQ(11, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
}
2020-03-01 15:55:58 +00:00
TEST(wf_json_response, init_error)
{
2020-07-18 21:16:18 +00:00
JsonDoc doc("{\"error\": {\"code\": 42, \"message\": \"Don't Panic!\"}, \"id\": 23}");
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());
ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error));
ASSERT_STREQ("Don't Panic!", wf_impl_jsonrpc_error_message(response.error));
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(23, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
}
2020-03-22 11:51:54 +00:00
TEST(wf_json_response, init_fail_missing_result_and_error)
{
2020-07-18 21:16:18 +00:00
JsonDoc doc("{\"id\": 12}");
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());
ASSERT_EQ(WF_BAD_FORMAT, wf_impl_jsonrpc_error_code(response.error));
ASSERT_EQ(nullptr, response.result);
ASSERT_EQ(12, response.id);
wf_impl_jsonrpc_response_cleanup(&response);
}
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
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);
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
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);
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
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);
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
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);
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
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);
wf_impl_jsonrpc_response_cleanup(&response);
2020-03-22 11:51:54 +00:00
}