test_jsonrpc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #ifndef TEST_JSONRPC_H
  31. #define TEST_JSONRPC_H
  32. #include "tests/test_macros.h"
  33. #include "tests/test_utils.h"
  34. #include "../jsonrpc.h"
  35. namespace TestJSONRPC {
  36. void check_invalid(const Dictionary &p_dict);
  37. TEST_CASE("[JSONRPC] process_action invalid") {
  38. JSONRPC json_rpc = JSONRPC();
  39. check_invalid(json_rpc.process_action("String is invalid"));
  40. check_invalid(json_rpc.process_action(1234));
  41. check_invalid(json_rpc.process_action(false));
  42. check_invalid(json_rpc.process_action(3.14159));
  43. }
  44. void check_invalid_string(const String &p_str);
  45. TEST_CASE("[JSONRPC] process_string invalid") {
  46. JSONRPC json_rpc = JSONRPC();
  47. check_invalid_string(json_rpc.process_string("\"String is invalid\""));
  48. check_invalid_string(json_rpc.process_string("1234"));
  49. check_invalid_string(json_rpc.process_string("false"));
  50. check_invalid_string(json_rpc.process_string("3.14159"));
  51. }
  52. class TestClassJSONRPC : public JSONRPC {
  53. GDCLASS(TestClassJSONRPC, JSONRPC)
  54. public:
  55. String something(const String &p_in);
  56. protected:
  57. static void _bind_methods();
  58. };
  59. void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
  60. TEST_CASE("[JSONRPC] process_action Dictionary") {
  61. ClassDB::register_class<TestClassJSONRPC>();
  62. Dictionary in_dict = Dictionary();
  63. in_dict["method"] = "something";
  64. in_dict["id"] = "ID";
  65. in_dict["params"] = "yes";
  66. Dictionary expected_dict = Dictionary();
  67. expected_dict["jsonrpc"] = "2.0";
  68. expected_dict["id"] = "ID";
  69. expected_dict["result"] = "yes, please";
  70. test_process_action(in_dict, expected_dict);
  71. }
  72. TEST_CASE("[JSONRPC] process_action Array") {
  73. Array in;
  74. Dictionary in_1;
  75. in_1["method"] = "something";
  76. in_1["id"] = 1;
  77. in_1["params"] = "more";
  78. in.push_back(in_1);
  79. Dictionary in_2;
  80. in_2["method"] = "something";
  81. in_2["id"] = 2;
  82. in_2["params"] = "yes";
  83. in.push_back(in_2);
  84. Array expected;
  85. Dictionary expected_1;
  86. expected_1["jsonrpc"] = "2.0";
  87. expected_1["id"] = 1;
  88. expected_1["result"] = "more, please";
  89. expected.push_back(expected_1);
  90. Dictionary expected_2;
  91. expected_2["jsonrpc"] = "2.0";
  92. expected_2["id"] = 2;
  93. expected_2["result"] = "yes, please";
  94. expected.push_back(expected_2);
  95. test_process_action(in, expected, true);
  96. }
  97. void test_process_string(const String &p_in, const String &p_expected);
  98. TEST_CASE("[JSONRPC] process_string Dictionary") {
  99. const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}";
  100. const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}";
  101. test_process_string(in, expected);
  102. }
  103. void test_process_action_bad_method(const Dictionary &p_in);
  104. TEST_CASE("[JSONRPC] process_action bad method") {
  105. Dictionary in_dict;
  106. in_dict["method"] = "nothing";
  107. test_process_action_bad_method(in_dict);
  108. }
  109. } // namespace TestJSONRPC
  110. #endif // TEST_JSONRPC_H