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

83 lines
2.2 KiB
C++
Raw Normal View History

2020-06-16 21:39:45 +00:00
#include "webfuse_provider/impl/jsonrpc/response_intern.h"
#include "webfuse_provider/impl/jsonrpc/error.h"
#include "webfuse_provider/impl/json/parser.h"
2019-02-13 19:49:05 +00:00
#include <gtest/gtest.h>
2019-01-27 02:45:03 +00:00
TEST(response_parser, fail_no_object)
2019-01-27 02:45:03 +00:00
{
char text[] = "[]";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_error, fail_empty_object)
{
char text[] = "{}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_error, fail_no_data)
{
char text[] = "{\"id\":42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_error, fail_with_custom_error_code)
{
char text[] = "{\"error\":{\"code\": 42}, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
2019-01-27 02:45:03 +00:00
ASSERT_NE(nullptr, response.error);
ASSERT_EQ(42, response.error->code);
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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
}
TEST(response_parser, fail_invalid_response)
{
char text[] = "{\"result\": true, \"id\": 42}";
wfp_json_doc * doc = wfp_impl_json_parse(text);
struct wfp_jsonrpc_response response;
wfp_jsonrpc_response_init(&response, wfp_impl_json_root(doc));
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);
wfp_jsonrpc_response_cleanup(&response);
wfp_impl_json_dispose(doc);
2019-01-27 02:45:03 +00:00
}