| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 | #include "RpcResponse.h"#include <cppunit/extensions/HelperMacros.h>namespace aria2 {namespace rpc {class RpcResponseTest:public CppUnit::TestFixture {  CPPUNIT_TEST_SUITE(RpcResponseTest);  CPPUNIT_TEST(testToJson);#ifdef ENABLE_XML_RPC  CPPUNIT_TEST(testToXml);#endif // ENABLE_XML_RPC  CPPUNIT_TEST_SUITE_END();public:  void testToJson();#ifdef ENABLE_XML_RPC  void testToXml();#endif // ENABLE_XML_RPC};CPPUNIT_TEST_SUITE_REGISTRATION(RpcResponseTest);void RpcResponseTest::testToJson(){  std::vector<RpcResponse> results;  {    SharedHandle<List> param = List::g();    param->append(Integer::g(1));    SharedHandle<String> id = String::g("9");    RpcResponse res(0, param, id);    results.push_back(res);    std::string s = toJson(res, "", false);    CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\","                                     "\"jsonrpc\":\"2.0\","                                     "\"result\":[1]}"),                         s);    // with callback    s = toJson(res, "cb", false);    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\","                                     "\"jsonrpc\":\"2.0\","                                     "\"result\":[1]})"),                         s);  }  {    // error response    SharedHandle<Dict> param = Dict::g();    param->put("code", Integer::g(1));    param->put("message", "HELLO ERROR");    RpcResponse res(1, param, Null::g());    results.push_back(res);    std::string s = toJson(res, "", false);    CPPUNIT_ASSERT_EQUAL(std::string("{\"error\":{\"code\":1,"                                     "\"message\":\"HELLO ERROR\"},"                                     "\"id\":null,"                                     "\"jsonrpc\":\"2.0\""                                     "}"),                         s);    // with callback    s = toJson(res, "cb", false);    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"error\":{\"code\":1,"                                     "\"message\":\"HELLO ERROR\"},"                                     "\"id\":null,"                                     "\"jsonrpc\":\"2.0\""                                     "})"),                         s);  }  {    // batch response    std::string s = toJsonBatch(results, "", false);    CPPUNIT_ASSERT_EQUAL(std::string("["                                     "{\"id\":\"9\","                                     "\"jsonrpc\":\"2.0\","                                     "\"result\":[1]},"                                     "{\"error\":{\"code\":1,"                                     "\"message\":\"HELLO ERROR\"},"                                     "\"id\":null,"                                     "\"jsonrpc\":\"2.0\""                                     "}"                                     "]"),                         s);    // with callback    s = toJsonBatch(results, "cb", false);    CPPUNIT_ASSERT_EQUAL(std::string("cb(["                                     "{\"id\":\"9\","                                     "\"jsonrpc\":\"2.0\","                                     "\"result\":[1]},"                                     "{\"error\":{\"code\":1,"                                     "\"message\":\"HELLO ERROR\"},"                                     "\"id\":null,"                                     "\"jsonrpc\":\"2.0\""                                     "}"                                     "])"),                         s);  }}#ifdef ENABLE_XML_RPCvoid RpcResponseTest::testToXml(){  SharedHandle<Dict> param = Dict::g();  param->put("faultCode", Integer::g(1));  param->put("faultString", "No such method: make.hamburger");  RpcResponse res(1, param, Null::g());  std::string s = toXml(res, false);  CPPUNIT_ASSERT_EQUAL    (std::string("<?xml version=\"1.0\"?>"                 "<methodResponse>"                 "<fault>"                 "<value>"                 "<struct>"                 "<member>"                 "<name>faultCode</name><value><int>1</int></value>"                 "</member>"                 "<member>"                 "<name>faultString</name>"                 "<value>"                 "<string>No such method: make.hamburger</string>"                 "</value>"                 "</member>"                 "</struct>"                 "</value>"                 "</fault>"                 "</methodResponse>"),     s);}#endif // ENABLE_XML_RPC} // namespace rpc} // namespace aria2
 |