2019-05-19 12:33:42 +00:00
|
|
|
#include <gtest/gtest.h>
|
2020-02-28 22:17:41 +00:00
|
|
|
#include "jsonrpc/response.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, valid_result)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "result", json_object());
|
|
|
|
json_object_set_new(message, "id", json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_TRUE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, valid_result_string)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "result", json_string("also valid"));
|
|
|
|
json_object_set_new(message, "id", json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_TRUE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, valid_error)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "error", json_object());
|
|
|
|
json_object_set_new(message, "id", json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_TRUE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_null)
|
|
|
|
{
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(nullptr));
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_message)
|
|
|
|
{
|
|
|
|
json_t * message = json_array();
|
|
|
|
json_array_append_new(message, json_object());
|
|
|
|
json_array_append_new(message, json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_missing_id)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "result", json_object());
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_id_wrong_type)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "result", json_object());
|
|
|
|
json_object_set_new(message, "id", json_string("42"));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_missing_result_and_error)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "id", json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(jsonrpc_is_response, invalid_error_wrong_type)
|
|
|
|
{
|
|
|
|
json_t * message = json_object();
|
|
|
|
json_object_set_new(message, "error", json_array());
|
|
|
|
json_object_set_new(message, "id", json_integer(42));
|
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(jsonrpc_is_response(message));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
json_decref(message);
|
|
|
|
}
|