RpcResponseTest.cc 4.4 KB

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