XmlRpcResponseTest.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "XmlRpcResponse.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. namespace xmlrpc {
  5. class XmlRpcResponseTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(XmlRpcResponseTest);
  7. CPPUNIT_TEST(testToJson);
  8. CPPUNIT_TEST_SUITE_END();
  9. public:
  10. void testToJson();
  11. };
  12. CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcResponseTest);
  13. void XmlRpcResponseTest::testToJson()
  14. {
  15. std::vector<XmlRpcResponse> results;
  16. {
  17. SharedHandle<List> param = List::g();
  18. param->append(Integer::g(1));
  19. SharedHandle<String> id = String::g("9");
  20. XmlRpcResponse res(0, param, id);
  21. results.push_back(res);
  22. std::string s = res.toJson("", false);
  23. CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","
  24. "\"jsonrpc\":\"2.0\","
  25. "\"result\":[1]}"),
  26. s);
  27. }
  28. {
  29. // error response
  30. SharedHandle<Dict> param = Dict::g();
  31. param->put("code", Integer::g(1));
  32. param->put("message", "HELLO ERROR");
  33. XmlRpcResponse res(1, param, Null::g());
  34. results.push_back(res);
  35. std::string s = res.toJson("", false);
  36. CPPUNIT_ASSERT_EQUAL(std::string("{\"error\":{\"code\":1,"
  37. "\"message\":\"HELLO ERROR\"},"
  38. "\"id\":null,"
  39. "\"jsonrpc\":\"2.0\""
  40. "}"),
  41. s);
  42. }
  43. {
  44. // batch response
  45. std::string s = toJsonBatch(results, "", false);
  46. CPPUNIT_ASSERT_EQUAL(std::string("["
  47. "{\"id\":\"9\","
  48. "\"jsonrpc\":\"2.0\","
  49. "\"result\":[1]},"
  50. "{\"error\":{\"code\":1,"
  51. "\"message\":\"HELLO ERROR\"},"
  52. "\"id\":null,"
  53. "\"jsonrpc\":\"2.0\""
  54. "}"
  55. "]"),
  56. s);
  57. }
  58. }
  59. } // namespace xmlrpc
  60. } // namespace aria2