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_parser.cc

71 lines
1.9 KiB
C++
Raw Normal View History

2019-01-27 02:45:03 +00:00
#include "webfuse/impl/jsonrpc/response_intern.h"
#include "webfuse/impl/jsonrpc/error.h"
2020-07-18 21:16:18 +00:00
#include "webfuse/test_util/json_doc.hpp"
2019-02-13 19:49:05 +00:00
2020-07-18 21:16:18 +00:00
#include <gtest/gtest.h>
#include <string>
2019-01-27 02:45:03 +00:00
2020-07-18 21:16:18 +00:00
using webfuse_test::JsonDoc;
2019-01-27 02:45:03 +00:00
2020-07-18 21:16:18 +00:00
TEST(response_parser, invalid_no_object)
2019-01-27 02:45:03 +00:00
{
2020-07-18 21:16:18 +00:00
JsonDoc doc("[]");
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-01-27 02:45:03 +00:00
ASSERT_NE(nullptr, response.error);
2019-01-27 02:45:03 +00:00
ASSERT_EQ(-1, response.id);
2019-01-30 20:53:57 +00:00
ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
2020-07-18 21:16:18 +00:00
}
TEST(response_parser, invalid_empty_object)
{
JsonDoc doc("{}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
2019-01-27 02:45:03 +00:00
ASSERT_NE(nullptr, response.error);
2019-01-27 02:45:03 +00:00
ASSERT_EQ(-1, response.id);
2019-01-30 20:53:57 +00:00
ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
2020-07-18 21:16:18 +00:00
}
TEST(response_parser, invalid_missin_result_and_error)
{
JsonDoc doc("{\"id\":42}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
2019-01-27 02:45:03 +00:00
ASSERT_NE(nullptr, response.error);
2019-01-27 02:45:03 +00:00
ASSERT_EQ(42, response.id);
2019-01-30 20:53:57 +00:00
ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
2020-07-18 21:16:18 +00:00
}
TEST(response_parser, custom_error_code)
{
JsonDoc doc("{\"error\":{\"code\": 42}, \"id\": 42}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
2019-01-27 02:45:03 +00:00
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, wf_impl_jsonrpc_error_code(response.error));
2019-01-27 02:45:03 +00:00
ASSERT_EQ(42, response.id);
2019-01-30 20:53:57 +00:00
ASSERT_EQ(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
2020-07-18 21:16:18 +00:00
}
TEST(response_parser, valid_response)
{
JsonDoc doc("{\"result\": true, \"id\": 42}");
struct wf_jsonrpc_response response;
wf_impl_jsonrpc_response_init(&response, doc.root());
2019-01-27 02:45:03 +00:00
ASSERT_EQ(nullptr, response.error);
2019-01-27 02:45:03 +00:00
ASSERT_EQ(42, response.id);
2019-01-30 20:53:57 +00:00
ASSERT_NE(nullptr, response.result);
wf_impl_jsonrpc_response_cleanup(&response);
2019-01-27 02:45:03 +00:00
}