test_jsonrpc.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #pragma once
  31. #include "tests/test_macros.h"
  32. #include "tests/test_utils.h"
  33. #include "../jsonrpc.h"
  34. namespace TestJSONRPC {
  35. void check_invalid(const Dictionary &p_dict);
  36. TEST_CASE("[JSONRPC] process_action invalid") {
  37. JSONRPC json_rpc = JSONRPC();
  38. check_invalid(json_rpc.process_action("String is invalid"));
  39. check_invalid(json_rpc.process_action(1234));
  40. check_invalid(json_rpc.process_action(false));
  41. check_invalid(json_rpc.process_action(3.14159));
  42. check_invalid(json_rpc.process_action(Array()));
  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. check_invalid_string(json_rpc.process_string("[]"));
  52. }
  53. class TestClassJSONRPC : public JSONRPC {
  54. public:
  55. TestClassJSONRPC() {
  56. set_method("something", callable_mp(this, &TestClassJSONRPC::something));
  57. }
  58. String something(const String &p_in);
  59. };
  60. void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
  61. TEST_CASE("[JSONRPC] process_action Dictionary") {
  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["id"] = 1;
  107. in_dict["method"] = "nothing";
  108. test_process_action_bad_method(in_dict);
  109. }
  110. void test_no_response(const Variant &p_in);
  111. TEST_CASE("[JSONRPC] process_action notification") {
  112. Dictionary in_dict = Dictionary();
  113. in_dict["method"] = "something";
  114. in_dict["params"] = "yes";
  115. test_no_response(in_dict);
  116. }
  117. TEST_CASE("[JSONRPC] process_action notification bad method") {
  118. Dictionary in_dict;
  119. in_dict["method"] = "nothing";
  120. test_no_response(in_dict);
  121. }
  122. TEST_CASE("[JSONRPC] process_action notification batch") {
  123. Array in;
  124. Dictionary in_1;
  125. in_1["method"] = "something";
  126. in_1["params"] = "more";
  127. in.push_back(in_1);
  128. Dictionary in_2;
  129. in_2["method"] = "something";
  130. in_2["params"] = "yes";
  131. in.push_back(in_2);
  132. test_no_response(in);
  133. }
  134. TEST_CASE("[JSONRPC] mixed batch") {
  135. Array in;
  136. Dictionary in_1;
  137. in_1["method"] = "something";
  138. in_1["id"] = 1;
  139. in_1["params"] = "more";
  140. in.push_back(in_1);
  141. Dictionary in_2;
  142. in_2["method"] = "something";
  143. in_2["id"] = 2;
  144. in_2["params"] = "yes";
  145. in.push_back(in_2);
  146. Dictionary in_3;
  147. in_3["method"] = "something";
  148. in_3["params"] = "yes";
  149. in.push_back(in_3);
  150. Array expected;
  151. Dictionary expected_1;
  152. expected_1["jsonrpc"] = "2.0";
  153. expected_1["id"] = 1;
  154. expected_1["result"] = "more, please";
  155. expected.push_back(expected_1);
  156. Dictionary expected_2;
  157. expected_2["jsonrpc"] = "2.0";
  158. expected_2["id"] = 2;
  159. expected_2["result"] = "yes, please";
  160. expected.push_back(expected_2);
  161. test_process_action(in, expected, true);
  162. }
  163. } // namespace TestJSONRPC