RpcResponseTest.cc 4.5 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. auto param = List::g();
  24. param->append(Integer::g(1));
  25. RpcResponse res(0, RpcResponse::AUTHORIZED, std::move(param),
  26. String::g("9"));
  27. results.push_back(std::move(res));
  28. std::string s = toJson(results.back(), "", false);
  29. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
  30. "\"jsonrpc\":\"2.0\","
  31. "\"result\":[1]}"),
  32. s);
  33. // with callback
  34. s = toJson(results.back(), "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. auto param = Dict::g();
  43. param->put("code", Integer::g(1));
  44. param->put("message", "HELLO ERROR");
  45. RpcResponse res(1, RpcResponse::AUTHORIZED, std::move(param), Null::g());
  46. results.push_back(std::move(res));
  47. std::string s = toJson(results.back(), "", false);
  48. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":null,"
  49. "\"jsonrpc\":\"2.0\","
  50. "\"error\":{\"code\":1,"
  51. "\"message\":\"HELLO ERROR\"}"
  52. "}"),
  53. s);
  54. // with callback
  55. s = toJson(results.back(), "cb", false);
  56. CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":null,"
  57. "\"jsonrpc\":\"2.0\","
  58. "\"error\":{\"code\":1,"
  59. "\"message\":\"HELLO ERROR\"}"
  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. "{\"id\":null,"
  71. "\"jsonrpc\":\"2.0\","
  72. "\"error\":{\"code\":1,"
  73. "\"message\":\"HELLO ERROR\"}"
  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. "{\"id\":null,"
  84. "\"jsonrpc\":\"2.0\","
  85. "\"error\":{\"code\":1,"
  86. "\"message\":\"HELLO ERROR\"}"
  87. "}"
  88. "])"),
  89. s);
  90. }
  91. }
  92. #ifdef ENABLE_XML_RPC
  93. void RpcResponseTest::testToXml()
  94. {
  95. auto param = Dict::g();
  96. param->put("faultCode", Integer::g(1));
  97. param->put("faultString", "No such method: make.hamburger");
  98. RpcResponse res(1, RpcResponse::AUTHORIZED, std::move(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