Path: blob/master/modules/jsonrpc/tests/test_jsonrpc.h
10278 views
/**************************************************************************/1/* test_jsonrpc.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "tests/test_macros.h"33#include "tests/test_utils.h"3435#include "../jsonrpc.h"3637namespace TestJSONRPC {3839void check_invalid(const Dictionary &p_dict);4041TEST_CASE("[JSONRPC] process_action invalid") {42JSONRPC json_rpc = JSONRPC();4344check_invalid(json_rpc.process_action("String is invalid"));45check_invalid(json_rpc.process_action(1234));46check_invalid(json_rpc.process_action(false));47check_invalid(json_rpc.process_action(3.14159));48check_invalid(json_rpc.process_action(Array()));49}5051void check_invalid_string(const String &p_str);5253TEST_CASE("[JSONRPC] process_string invalid") {54JSONRPC json_rpc = JSONRPC();5556check_invalid_string(json_rpc.process_string("\"String is invalid\""));57check_invalid_string(json_rpc.process_string("1234"));58check_invalid_string(json_rpc.process_string("false"));59check_invalid_string(json_rpc.process_string("3.14159"));60check_invalid_string(json_rpc.process_string("[]"));61}6263class TestClassJSONRPC : public JSONRPC {64public:65TestClassJSONRPC() {66set_method("something", callable_mp(this, &TestClassJSONRPC::something));67}6869String something(const String &p_in);70};7172void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);7374TEST_CASE("[JSONRPC] process_action Dictionary") {75Dictionary in_dict = Dictionary();76in_dict["method"] = "something";77in_dict["id"] = "ID";78in_dict["params"] = "yes";7980Dictionary expected_dict = Dictionary();81expected_dict["jsonrpc"] = "2.0";82expected_dict["id"] = "ID";83expected_dict["result"] = "yes, please";8485test_process_action(in_dict, expected_dict);86}8788TEST_CASE("[JSONRPC] process_action Array") {89Array in;90Dictionary in_1;91in_1["method"] = "something";92in_1["id"] = 1;93in_1["params"] = "more";94in.push_back(in_1);95Dictionary in_2;96in_2["method"] = "something";97in_2["id"] = 2;98in_2["params"] = "yes";99in.push_back(in_2);100101Array expected;102Dictionary expected_1;103expected_1["jsonrpc"] = "2.0";104expected_1["id"] = 1;105expected_1["result"] = "more, please";106expected.push_back(expected_1);107Dictionary expected_2;108expected_2["jsonrpc"] = "2.0";109expected_2["id"] = 2;110expected_2["result"] = "yes, please";111expected.push_back(expected_2);112113test_process_action(in, expected, true);114}115116void test_process_string(const String &p_in, const String &p_expected);117118TEST_CASE("[JSONRPC] process_string Dictionary") {119const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}";120const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}";121122test_process_string(in, expected);123}124125void test_process_action_bad_method(const Dictionary &p_in);126127TEST_CASE("[JSONRPC] process_action bad method") {128Dictionary in_dict;129in_dict["id"] = 1;130in_dict["method"] = "nothing";131132test_process_action_bad_method(in_dict);133}134135void test_no_response(const Variant &p_in);136137TEST_CASE("[JSONRPC] process_action notification") {138Dictionary in_dict = Dictionary();139in_dict["method"] = "something";140in_dict["params"] = "yes";141142test_no_response(in_dict);143}144145TEST_CASE("[JSONRPC] process_action notification bad method") {146Dictionary in_dict;147in_dict["method"] = "nothing";148149test_no_response(in_dict);150}151152TEST_CASE("[JSONRPC] process_action notification batch") {153Array in;154Dictionary in_1;155in_1["method"] = "something";156in_1["params"] = "more";157in.push_back(in_1);158Dictionary in_2;159in_2["method"] = "something";160in_2["params"] = "yes";161in.push_back(in_2);162163test_no_response(in);164}165166TEST_CASE("[JSONRPC] mixed batch") {167Array in;168Dictionary in_1;169in_1["method"] = "something";170in_1["id"] = 1;171in_1["params"] = "more";172in.push_back(in_1);173Dictionary in_2;174in_2["method"] = "something";175in_2["id"] = 2;176in_2["params"] = "yes";177in.push_back(in_2);178Dictionary in_3;179in_3["method"] = "something";180in_3["params"] = "yes";181in.push_back(in_3);182183Array expected;184Dictionary expected_1;185expected_1["jsonrpc"] = "2.0";186expected_1["id"] = 1;187expected_1["result"] = "more, please";188expected.push_back(expected_1);189Dictionary expected_2;190expected_2["jsonrpc"] = "2.0";191expected_2["id"] = 2;192expected_2["result"] = "yes, please";193expected.push_back(expected_2);194195test_process_action(in, expected, true);196}197198} // namespace TestJSONRPC199200201