test_jsonrpc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. }
  43. void check_invalid_string(const String &p_str);
  44. TEST_CASE("[JSONRPC] process_string invalid") {
  45. JSONRPC json_rpc = JSONRPC();
  46. check_invalid_string(json_rpc.process_string("\"String is invalid\""));
  47. check_invalid_string(json_rpc.process_string("1234"));
  48. check_invalid_string(json_rpc.process_string("false"));
  49. check_invalid_string(json_rpc.process_string("3.14159"));
  50. }
  51. class TestClassJSONRPC : public JSONRPC {
  52. public:
  53. TestClassJSONRPC() {
  54. set_method("something", callable_mp(this, &TestClassJSONRPC::something));
  55. }
  56. String something(const String &p_in);
  57. };
  58. void test_process_action(const Variant &p_in, const Variant &p_expected, bool p_process_array_elements = false);
  59. TEST_CASE("[JSONRPC] process_action Dictionary") {
  60. Dictionary in_dict = Dictionary();
  61. in_dict["method"] = "something";
  62. in_dict["id"] = "ID";
  63. in_dict["params"] = "yes";
  64. Dictionary expected_dict = Dictionary();
  65. expected_dict["jsonrpc"] = "2.0";
  66. expected_dict["id"] = "ID";
  67. expected_dict["result"] = "yes, please";
  68. test_process_action(in_dict, expected_dict);
  69. }
  70. TEST_CASE("[JSONRPC] process_action Array") {
  71. Array in;
  72. Dictionary in_1;
  73. in_1["method"] = "something";
  74. in_1["id"] = 1;
  75. in_1["params"] = "more";
  76. in.push_back(in_1);
  77. Dictionary in_2;
  78. in_2["method"] = "something";
  79. in_2["id"] = 2;
  80. in_2["params"] = "yes";
  81. in.push_back(in_2);
  82. Array expected;
  83. Dictionary expected_1;
  84. expected_1["jsonrpc"] = "2.0";
  85. expected_1["id"] = 1;
  86. expected_1["result"] = "more, please";
  87. expected.push_back(expected_1);
  88. Dictionary expected_2;
  89. expected_2["jsonrpc"] = "2.0";
  90. expected_2["id"] = 2;
  91. expected_2["result"] = "yes, please";
  92. expected.push_back(expected_2);
  93. test_process_action(in, expected, true);
  94. }
  95. void test_process_string(const String &p_in, const String &p_expected);
  96. TEST_CASE("[JSONRPC] process_string Dictionary") {
  97. const String in = "{\"method\":\"something\",\"id\":\"ID\",\"params\":\"yes\"}";
  98. const String expected = "{\"id\":\"ID\",\"jsonrpc\":\"2.0\",\"result\":\"yes, please\"}";
  99. test_process_string(in, expected);
  100. }
  101. void test_process_action_bad_method(const Dictionary &p_in);
  102. TEST_CASE("[JSONRPC] process_action bad method") {
  103. Dictionary in_dict;
  104. in_dict["method"] = "nothing";
  105. test_process_action_bad_method(in_dict);
  106. }
  107. } // namespace TestJSONRPC