Path: blob/master/modules/jsonrpc/tests/test_jsonrpc.cpp
10278 views
/**************************************************************************/1/* test_jsonrpc.cpp */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#include "test_jsonrpc.h"3132#include "core/io/json.h"3334namespace TestJSONRPC {3536void check_error_code(const Dictionary &p_dict, const JSONRPC::ErrorCode &p_code) {37CHECK(p_dict["jsonrpc"] == "2.0");38REQUIRE(p_dict.has("error"));39const Dictionary &err_body = p_dict["error"];40const int &code = err_body["code"];41CHECK(code == p_code);42}4344void check_invalid(const Dictionary &p_dict) {45check_error_code(p_dict, JSONRPC::INVALID_REQUEST);46}4748void check_invalid_string(const String &p_str) {49JSON json;50REQUIRE(json.parse(p_str) == OK);51const Dictionary &dict = json.get_data();52check_invalid(dict);53}5455String TestClassJSONRPC::something(const String &p_in) {56return p_in + ", please";57}5859void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements) {60TestClassJSONRPC json_rpc = TestClassJSONRPC();61const Variant &observed = json_rpc.process_action(p_in, p_process_array_elements);62CHECK(observed == p_expected);63}6465void test_process_string(const String &p_in, const String &p_expected) {66TestClassJSONRPC json_rpc = TestClassJSONRPC();67const String &out_str = json_rpc.process_string(p_in);68CHECK(out_str == p_expected);69}7071void check_error_no_method(const Dictionary &p_dict) {72check_error_code(p_dict, JSONRPC::METHOD_NOT_FOUND);73}7475void test_process_action_bad_method(const Dictionary &p_in) {76TestClassJSONRPC json_rpc = TestClassJSONRPC();77const Dictionary &out_dict = json_rpc.process_action(p_in);78check_error_no_method(out_dict);79}8081void test_no_response(const Variant &p_in) {82TestClassJSONRPC json_rpc = TestClassJSONRPC();83const Variant &res = json_rpc.process_action(p_in, true);84CHECK(res.get_type() == Variant::NIL);85}8687} // namespace TestJSONRPC888990