| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "tests/test_macros.h" |
| 34 |
#include "tests/test_utils.h" |
| 35 |
|
| 36 |
#include "../jsonrpc.h" |
| 37 |
|
| 38 |
namespace TestJSONRPC { |
| 39 |
|
| 40 |
void check_invalid(const Dictionary &p_dict); |
| 41 |
|
| 42 |
TEST_CASE("[JSONRPC] process_action invalid") { |
| 43 |
JSONRPC json_rpc = JSONRPC(); |
| 44 |
|
| 45 |
check_invalid(json_rpc.process_action("String is invalid")); |
| 46 |
check_invalid(json_rpc.process_action(1234)); |
| 47 |
check_invalid(json_rpc.process_action(false)); |
| 48 |
check_invalid(json_rpc.process_action(3.14159)); |
| 49 |
check_invalid(json_rpc.process_action(Array())); |
| 50 |
} |
| 51 |
|
| 52 |
void check_invalid_string(const String &p_str); |
| 53 |
|
| 54 |
TEST_CASE("[JSONRPC] process_string invalid") { |
| 55 |
JSONRPC json_rpc = JSONRPC(); |
| 56 |
|
| 57 |
check_invalid_string(json_rpc.process_string("\"String is invalid\"")); |
| 58 |
check_invalid_string(json_rpc.process_string("1234")); |
| 59 |
check_invalid_string(json_rpc.process_string("false")); |
| 60 |
check_invalid_string(json_rpc.process_string("3.14159")); |
| 61 |
check_invalid_string(json_rpc.process_string("[]")); |
| 62 |
} |
| 63 |
|
| 64 |
class TestClassJSONRPC : public JSONRPC { |
| 65 |
public: |
| 66 |
TestClassJSONRPC() { |
| 67 |
set_method("something", callable_mp(this, &TestClassJSONRPC::something)); |
| 68 |
} |
| 69 |
|
| 70 |
String something(const String &p_in); |
| 71 |
}; |
| 72 |
|
| 73 |
void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false); |
| 74 |
|
| 75 |
TEST_CASE("[JSONRPC] process_action Dictionary") { |
| 76 |
Dictionary in_dict = Dictionary(); |
| 77 |
in_dict["method"] = "something"; |
| 78 |
in_dict["id"] = "ID"; |
| 79 |
in_dict["params"] = "yes"; |
| 80 |
|
| 81 |
Dictionary expected_dict = Dictionary(); |
| 82 |
expected_dict["jsonrpc"] = "2.0"; |
| 83 |
expected_dict["id"] = "ID"; |
| 84 |
expected_dict["result"] = "yes, please"; |
| 85 |
|
| 86 |
test_process_action(in_dict, expected_dict); |
| 87 |
} |
| 88 |
|
| 89 |
TEST_CASE("[JSONRPC] process_action Array") { |
| 90 |
Array in; |
| 91 |
Dictionary in_1; |
| 92 |
in_1["method"] = "something"; |
| 93 |
in_1["id"] = 1; |
| 94 |
in_1["params"] = "more"; |
| 95 |
in.push_back(in_1); |
| 96 |
Dictionary in_2; |
| 97 |
in_2["method"] = "something"; |
| 98 |
in_2["id"] = 2; |
| 99 |
in_2["params"] = "yes"; |
| 100 |
in.push_back(in_2); |
| 101 |
|
| 102 |
Array expected; |
| 103 |
Dictionary expected_1; |
| 104 |
expected_1["jsonrpc"] = "2.0"; |
| 105 |
expected_1["id"] = 1; |
| 106 |
expected_1["result"] = "more, please"; |
| 107 |
expected.push_back(expected_1); |
| 108 |
Dictionary expected_2; |
| 109 |
expected_2["jsonrpc"] = "2.0"; |
| 110 |
expected_2["id"] = 2; |
| 111 |
expected_2["result"] = "yes, please"; |
| 112 |
expected.push_back(expected_2); |
| 113 |
|
| 114 |
test_process_action(in, expected, true); |
| 115 |
} |
| 116 |
|
| 117 |
void test_process_string(const String &p_in, const String &p_expected); |
| 118 |
|
| 119 |
TEST_CASE("[JSONRPC] process_string Dictionary") { |
| 120 |
const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}"; |
| 121 |
const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}"; |
| 122 |
|
| 123 |
test_process_string(in, expected); |
| 124 |
} |
| 125 |
|
| 126 |
void test_process_action_bad_method(const Dictionary &p_in); |
| 127 |
|
| 128 |
TEST_CASE("[JSONRPC] process_action bad method") { |
| 129 |
Dictionary in_dict; |
| 130 |
in_dict["id"] = 1; |
| 131 |
in_dict["method"] = "nothing"; |
| 132 |
|
| 133 |
test_process_action_bad_method(in_dict); |
| 134 |
} |
| 135 |
|
| 136 |
void test_no_response(const Variant &p_in); |
| 137 |
|
| 138 |
TEST_CASE("[JSONRPC] process_action notification") { |
| 139 |
Dictionary in_dict = Dictionary(); |
| 140 |
in_dict["method"] = "something"; |
| 141 |
in_dict["params"] = "yes"; |
| 142 |
|
| 143 |
test_no_response(in_dict); |
| 144 |
} |
| 145 |
|
| 146 |
TEST_CASE("[JSONRPC] process_action notification bad method") { |
| 147 |
Dictionary in_dict; |
| 148 |
in_dict["method"] = "nothing"; |
| 149 |
|
| 150 |
test_no_response(in_dict); |
| 151 |
} |
| 152 |
|
| 153 |
TEST_CASE("[JSONRPC] process_action notification batch") { |
| 154 |
Array in; |
| 155 |
Dictionary in_1; |
| 156 |
in_1["method"] = "something"; |
| 157 |
in_1["params"] = "more"; |
| 158 |
in.push_back(in_1); |
| 159 |
Dictionary in_2; |
| 160 |
in_2["method"] = "something"; |
| 161 |
in_2["params"] = "yes"; |
| 162 |
in.push_back(in_2); |
| 163 |
|
| 164 |
test_no_response(in); |
| 165 |
} |
| 166 |
|
| 167 |
TEST_CASE("[JSONRPC] mixed batch") { |
| 168 |
Array in; |
| 169 |
Dictionary in_1; |
| 170 |
in_1["method"] = "something"; |
| 171 |
in_1["id"] = 1; |
| 172 |
in_1["params"] = "more"; |
| 173 |
in.push_back(in_1); |
| 174 |
Dictionary in_2; |
| 175 |
in_2["method"] = "something"; |
| 176 |
in_2["id"] = 2; |
| 177 |
in_2["params"] = "yes"; |
| 178 |
in.push_back(in_2); |
| 179 |
Dictionary in_3; |
| 180 |
in_3["method"] = "something"; |
| 181 |
in_3["params"] = "yes"; |
| 182 |
in.push_back(in_3); |
| 183 |
|
| 184 |
Array expected; |
| 185 |
Dictionary expected_1; |
| 186 |
expected_1["jsonrpc"] = "2.0"; |
| 187 |
expected_1["id"] = 1; |
| 188 |
expected_1["result"] = "more, please"; |
| 189 |
expected.push_back(expected_1); |
| 190 |
Dictionary expected_2; |
| 191 |
expected_2["jsonrpc"] = "2.0"; |
| 192 |
expected_2["id"] = 2; |
| 193 |
expected_2["result"] = "yes, please"; |
| 194 |
expected.push_back(expected_2); |
| 195 |
|
| 196 |
test_process_action(in, expected, true); |
| 197 |
} |
| 198 |
|
| 199 |
} |