Skip to content
0.2.0-dev Download ZIP
230 lines ·7.1 KB Raw
1/**************************************************************************/
2/* test_jsonrpc.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#pragma once
32
33#include "../jsonrpc.h"
34
35#include "core/object/callable_mp.h"
36#include "tests/test_macros.h"
37#include "tests/test_utils.h"
38
39namespace TestJSONRPC {
40
41void check_invalid(const Dictionary &p_dict);
42
43TEST_CASE("[JSONRPC] process_action invalid") {
44 JSONRPC json_rpc = JSONRPC();
45
46 check_invalid(json_rpc.process_action("String is invalid"));
47 check_invalid(json_rpc.process_action(1234));
48 check_invalid(json_rpc.process_action(false));
49 check_invalid(json_rpc.process_action(3.14159));
50 check_invalid(json_rpc.process_action(Array()));
51}
52
53void check_invalid_string(const String &p_str);
54
55TEST_CASE("[JSONRPC] process_string invalid") {
56 JSONRPC json_rpc = JSONRPC();
57
58 check_invalid_string(json_rpc.process_string("\"String is invalid\""));
59 check_invalid_string(json_rpc.process_string("1234"));
60 check_invalid_string(json_rpc.process_string("false"));
61 check_invalid_string(json_rpc.process_string("3.14159"));
62 check_invalid_string(json_rpc.process_string("[]"));
63}
64
65class TestClassJSONRPC : public JSONRPC {
66public:
67 TestClassJSONRPC() {
68 set_method("something", callable_mp(this, &TestClassJSONRPC::something));
69 }
70
71 String something(const String &p_in);
72};
73
74void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
75
76TEST_CASE("[JSONRPC] process_action Dictionary") {
77 Dictionary in_dict = Dictionary();
78 in_dict["method"] = "something";
79 in_dict["id"] = "ID";
80 in_dict["params"] = "yes";
81
82 Dictionary expected_dict = Dictionary();
83 expected_dict["jsonrpc"] = "2.0";
84 expected_dict["id"] = "ID";
85 expected_dict["result"] = "yes, please";
86
87 test_process_action(in_dict, expected_dict);
88}
89
90TEST_CASE("[JSONRPC] process_action Array") {
91 Array in;
92 Dictionary in_1;
93 in_1["method"] = "something";
94 in_1["id"] = 1;
95 in_1["params"] = "more";
96 in.push_back(in_1);
97 Dictionary in_2;
98 in_2["method"] = "something";
99 in_2["id"] = 2;
100 in_2["params"] = "yes";
101 in.push_back(in_2);
102
103 Array expected;
104 Dictionary expected_1;
105 expected_1["jsonrpc"] = "2.0";
106 expected_1["id"] = 1;
107 expected_1["result"] = "more, please";
108 expected.push_back(expected_1);
109 Dictionary expected_2;
110 expected_2["jsonrpc"] = "2.0";
111 expected_2["id"] = 2;
112 expected_2["result"] = "yes, please";
113 expected.push_back(expected_2);
114
115 test_process_action(in, expected, true);
116}
117
118void test_process_string(const String &p_in, const String &p_expected);
119
120TEST_CASE("[JSONRPC] process_string Dictionary") {
121 const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}";
122 const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}";
123
124 test_process_string(in, expected);
125}
126
127void test_process_action_bad_method(const Dictionary &p_in);
128
129TEST_CASE("[JSONRPC] process_action bad method") {
130 Dictionary in_dict;
131 in_dict["id"] = 1;
132 in_dict["method"] = "nothing";
133
134 test_process_action_bad_method(in_dict);
135}
136
137void test_no_response(const Variant &p_in);
138
139TEST_CASE("[JSONRPC] process_action notification") {
140 Dictionary in_dict = Dictionary();
141 in_dict["method"] = "something";
142 in_dict["params"] = "yes";
143
144 test_no_response(in_dict);
145}
146
147TEST_CASE("[JSONRPC] process_action notification bad method") {
148 Dictionary in_dict;
149 in_dict["method"] = "nothing";
150
151 test_no_response(in_dict);
152}
153
154TEST_CASE("[JSONRPC] process_action notification batch") {
155 Array in;
156 Dictionary in_1;
157 in_1["method"] = "something";
158 in_1["params"] = "more";
159 in.push_back(in_1);
160 Dictionary in_2;
161 in_2["method"] = "something";
162 in_2["params"] = "yes";
163 in.push_back(in_2);
164
165 test_no_response(in);
166}
167
168TEST_CASE("[JSONRPC] mixed batch") {
169 Array in;
170 Dictionary in_1;
171 in_1["method"] = "something";
172 in_1["id"] = 1;
173 in_1["params"] = "more";
174 in.push_back(in_1);
175 Dictionary in_2;
176 in_2["method"] = "something";
177 in_2["id"] = 2;
178 in_2["params"] = "yes";
179 in.push_back(in_2);
180 Dictionary in_3;
181 in_3["method"] = "something";
182 in_3["params"] = "yes";
183 in.push_back(in_3);
184
185 Array expected;
186 Dictionary expected_1;
187 expected_1["jsonrpc"] = "2.0";
188 expected_1["id"] = 1;
189 expected_1["result"] = "more, please";
190 expected.push_back(expected_1);
191 Dictionary expected_2;
192 expected_2["jsonrpc"] = "2.0";
193 expected_2["id"] = 2;
194 expected_2["result"] = "yes, please";
195 expected.push_back(expected_2);
196
197 test_process_action(in, expected, true);
198}
199
200class TestHandlerJSONRPC : public Object {
201public:
202 bool called_1 = false;
203 bool called_2 = false;
204
205 void method1(const Variant &p_arg) {
206 called_1 = true;
207 }
208
209 void method2(const Variant &p_arg) {
210 called_2 = true;
211 }
212};
213
214TEST_CASE("[JSONRPC] process response") {
215 TestHandlerJSONRPC *handler = memnew(TestHandlerJSONRPC);
216 JSONRPC *jsonrpc = memnew(JSONRPC);
217
218 jsonrpc->set_response_handler(1, callable_mp(handler, &TestHandlerJSONRPC::method1));
219 jsonrpc->set_response_handler(2, callable_mp(handler, &TestHandlerJSONRPC::method2));
220
221 jsonrpc->process_action(jsonrpc->make_response(Variant(), 1));
222
223 CHECK(handler->called_1);
224 CHECK_FALSE(handler->called_2);
225
226 memdelete(jsonrpc);
227 memdelete(handler);
228}
229
230} // namespace TestJSONRPC