| 1 | /**************************************************************************/ |
| 2 | /* test_jsonrpc.cpp */ |
| 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 | #include "test_jsonrpc.h" |
| 32 | |
| 33 | #include "core/io/json.h" |
| 34 | |
| 35 | namespace TestJSONRPC { |
| 36 | |
| 37 | void check_error_code(const Dictionary &p_dict, const JSONRPC::ErrorCode &p_code) { |
| 38 | CHECK(p_dict["jsonrpc"] == "2.0"); |
| 39 | REQUIRE(p_dict.has("error")); |
| 40 | const Dictionary &err_body = p_dict["error"]; |
| 41 | const int &code = err_body["code"]; |
| 42 | CHECK(code == p_code); |
| 43 | } |
| 44 | |
| 45 | void check_invalid(const Dictionary &p_dict) { |
| 46 | check_error_code(p_dict, JSONRPC::INVALID_REQUEST); |
| 47 | } |
| 48 | |
| 49 | void check_invalid_string(const String &p_str) { |
| 50 | JSON json; |
| 51 | REQUIRE(json.parse(p_str) == OK); |
| 52 | const Dictionary &dict = json.get_data(); |
| 53 | check_invalid(dict); |
| 54 | } |
| 55 | |
| 56 | String TestClassJSONRPC::something(const String &p_in) { |
| 57 | return p_in + ", please"; |
| 58 | } |
| 59 | |
| 60 | void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) { |
| 61 | TestClassJSONRPC json_rpc = TestClassJSONRPC(); |
| 62 | const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements); |
| 63 | CHECK(observed == p_expected); |
| 64 | } |
| 65 | |
| 66 | void test_process_string(const String &p_in, const String &p_expected) { |
| 67 | TestClassJSONRPC json_rpc = TestClassJSONRPC(); |
| 68 | const String &out_str = json_rpc.process_string(p_in); |
| 69 | CHECK(out_str == p_expected); |
| 70 | } |
| 71 | |
| 72 | void check_error_no_method(const Dictionary &p_dict) { |
| 73 | check_error_code(p_dict, JSONRPC::METHOD_NOT_FOUND); |
| 74 | } |
| 75 | |
| 76 | void test_process_action_bad_method(const Dictionary &p_in) { |
| 77 | TestClassJSONRPC json_rpc = TestClassJSONRPC(); |
| 78 | const Dictionary &out_dict = json_rpc.process_action(p_in); |
| 79 | check_error_no_method(out_dict); |
| 80 | } |
| 81 | |
| 82 | void test_no_response(const Variant &p_in) { |
| 83 | TestClassJSONRPC json_rpc = TestClassJSONRPC(); |
| 84 | const Variant &res = json_rpc.process_action(p_in, true); |
| 85 | CHECK(res.get_type() == Variant::NIL); |
| 86 | } |
| 87 | |
| 88 | } // namespace TestJSONRPC |