RpcResponseTest.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = res.toJson("", false);
  29. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
  30. "\"jsonrpc\":\"2.0\","
  31. "\"result\":[1]}"),
  32. s);
  33. }
  34. {
  35. // error response
  36. SharedHandle<Dict> param = Dict::g();
  37. param->put("code", Integer::g(1));
  38. param->put("message", "HELLO ERROR");
  39. RpcResponse res(1, param, Null::g());
  40. results.push_back(res);
  41. std::string s = res.toJson("", false);
  42. CPPUNIT_ASSERT_EQUAL(std::string("{\"error\":{\"code\":1,"
  43. "\"message\":\"HELLO ERROR\"},"
  44. "\"id\":null,"
  45. "\"jsonrpc\":\"2.0\""
  46. "}"),
  47. s);
  48. }
  49. {
  50. // batch response
  51. std::string s = toJsonBatch(results, "", false);
  52. CPPUNIT_ASSERT_EQUAL(std::string("["
  53. "{\"id\":\"9\","
  54. "\"jsonrpc\":\"2.0\","
  55. "\"result\":[1]},"
  56. "{\"error\":{\"code\":1,"
  57. "\"message\":\"HELLO ERROR\"},"
  58. "\"id\":null,"
  59. "\"jsonrpc\":\"2.0\""
  60. "}"
  61. "]"),
  62. s);
  63. }
  64. }
  65. #ifdef ENABLE_XML_RPC
  66. void RpcResponseTest::testToXml()
  67. {
  68. SharedHandle<Dict> param = Dict::g();
  69. param->put("faultCode", Integer::g(1));
  70. param->put("faultString", "No such method: make.hamburger");
  71. RpcResponse res(1, param, Null::g());
  72. std::string s = res.toXml(false);
  73. CPPUNIT_ASSERT_EQUAL
  74. (std::string("<?xml version=\"1.0\"?>"
  75. "<methodResponse>"
  76. "<fault>"
  77. "<value>"
  78. "<struct>"
  79. "<member>"
  80. "<name>faultCode</name><value><int>1</int></value>"
  81. "</member>"
  82. "<member>"
  83. "<name>faultString</name>"
  84. "<value>"
  85. "<string>No such method: make.hamburger</string>"
  86. "</value>"
  87. "</member>"
  88. "</struct>"
  89. "</value>"
  90. "</fault>"
  91. "</methodResponse>"),
  92. s);
  93. }
  94. #endif // ENABLE_XML_RPC
  95. } // namespace rpc
  96. } // namespace aria2