RpcResponseTest.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "RpcResponse.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. namespace rpc {
  5. class RpcResponseTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(RpcResponseTest);
  7. CPPUNIT_TEST(testToJson);
  8. #ifdef ENABLE_XML_RPC
  9. CPPUNIT_TEST(testToXml);
  10. #endif // ENABLE_XML_RPC
  11. CPPUNIT_TEST_SUITE_END();
  12. public:
  13. void testToJson();
  14. #ifdef ENABLE_XML_RPC
  15. void testToXml();
  16. #endif // ENABLE_XML_RPC
  17. };
  18. CPPUNIT_TEST_SUITE_REGISTRATION(RpcResponseTest);
  19. void RpcResponseTest::testToJson()
  20. {
  21. std::vector<RpcResponse> results;
  22. {
  23. auto param = List::g();
  24. param->append(Integer::g(1));
  25. RpcResponse res(0, std::move(param), String::g("9"));
  26. results.push_back(std::move(res));
  27. std::string s = toJson(results.back(), "", false);
  28. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
  29. "\"jsonrpc\":\"2.0\","
  30. "\"result\":[1]}"),
  31. s);
  32. // with callback
  33. s = toJson(results.back(), "cb", false);
  34. CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\","
  35. "\"jsonrpc\":\"2.0\","
  36. "\"result\":[1]})"),
  37. s);
  38. }
  39. {
  40. // error response
  41. auto param = Dict::g();
  42. param->put("code", Integer::g(1));
  43. param->put("message", "HELLO ERROR");
  44. RpcResponse res(1, std::move(param), Null::g());
  45. results.push_back(std::move(res));
  46. std::string s = toJson(results.back(), "", false);
  47. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":null,"
  48. "\"jsonrpc\":\"2.0\","
  49. "\"error\":{\"code\":1,"
  50. "\"message\":\"HELLO ERROR\"}"
  51. "}"),
  52. s);
  53. // with callback
  54. s = toJson(results.back(), "cb", false);
  55. CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":null,"
  56. "\"jsonrpc\":\"2.0\","
  57. "\"error\":{\"code\":1,"
  58. "\"message\":\"HELLO ERROR\"}"
  59. "})"),
  60. s);
  61. }
  62. {
  63. // batch response
  64. std::string s = toJsonBatch(results, "", false);
  65. CPPUNIT_ASSERT_EQUAL(std::string("["
  66. "{\"id\":\"9\","
  67. "\"jsonrpc\":\"2.0\","
  68. "\"result\":[1]},"
  69. "{\"id\":null,"
  70. "\"jsonrpc\":\"2.0\","
  71. "\"error\":{\"code\":1,"
  72. "\"message\":\"HELLO ERROR\"}"
  73. "}"
  74. "]"),
  75. s);
  76. // with callback
  77. s = toJsonBatch(results, "cb", false);
  78. CPPUNIT_ASSERT_EQUAL(std::string("cb(["
  79. "{\"id\":\"9\","
  80. "\"jsonrpc\":\"2.0\","
  81. "\"result\":[1]},"
  82. "{\"id\":null,"
  83. "\"jsonrpc\":\"2.0\","
  84. "\"error\":{\"code\":1,"
  85. "\"message\":\"HELLO ERROR\"}"
  86. "}"
  87. "])"),
  88. s);
  89. }
  90. }
  91. #ifdef ENABLE_XML_RPC
  92. void RpcResponseTest::testToXml()
  93. {
  94. auto param = Dict::g();
  95. param->put("faultCode", Integer::g(1));
  96. param->put("faultString", "No such method: make.hamburger");
  97. RpcResponse res(1, std::move(param), Null::g());
  98. std::string s = toXml(res, false);
  99. CPPUNIT_ASSERT_EQUAL
  100. (std::string("<?xml version=\"1.0\"?>"
  101. "<methodResponse>"
  102. "<fault>"
  103. "<value>"
  104. "<struct>"
  105. "<member>"
  106. "<name>faultCode</name><value><int>1</int></value>"
  107. "</member>"
  108. "<member>"
  109. "<name>faultString</name>"
  110. "<value>"
  111. "<string>No such method: make.hamburger</string>"
  112. "</value>"
  113. "</member>"
  114. "</struct>"
  115. "</value>"
  116. "</fault>"
  117. "</methodResponse>"),
  118. s);
  119. }
  120. #endif // ENABLE_XML_RPC
  121. } // namespace rpc
  122. } // namespace aria2