diff --git a/src/HttpServerBodyCommand.cc b/src/HttpServerBodyCommand.cc index 7577d3eb..44eaa2f0 100644 --- a/src/HttpServerBodyCommand.cc +++ b/src/HttpServerBodyCommand.cc @@ -149,8 +149,7 @@ HttpServerBodyCommand::processJsonRpcRequest(const Dict* jsondict) SharedHandle id = jsondict->get("id"); if(!id) { - return createJsonRpcErrorResponse(-32600, "Invalid Request.", - SharedHandle()); + return createJsonRpcErrorResponse(-32600, "Invalid Request.", Null::g()); } const String* methodName = asString(jsondict->get("method")); if(!methodName) { diff --git a/src/XmlRpcResponse.cc b/src/XmlRpcResponse.cc index 57404f5b..a0553c7b 100644 --- a/src/XmlRpcResponse.cc +++ b/src/XmlRpcResponse.cc @@ -175,10 +175,7 @@ OutputStream& encodeJsonAll } SharedHandle dict = Dict::g(); dict->put("jsonrpc", "2.0"); - // TODO id may be null? - if(id) { - dict->put("id", id); - } + dict->put("id", id); if(code == 0) { dict->put("result", param); } else { diff --git a/src/XmlRpcResponse.h b/src/XmlRpcResponse.h index 13465bf9..e0bd6ea3 100644 --- a/src/XmlRpcResponse.h +++ b/src/XmlRpcResponse.h @@ -70,14 +70,12 @@ struct XmlRpcResponse { // Encodes RPC response in JSON. If callback is not empty, the // resulting string is JSONP. std::string toJson(const std::string& callback, bool gzip = false) const; - - std::string toJsonBatch(const std::string& callback, bool gzip = false) const; }; std::string toJsonBatch (const std::vector& results, const std::string& callback, - bool gzip); + bool gzip = false); } // namespace xmlrpc diff --git a/test/Makefile.am b/test/Makefile.am index 7b2d1b33..d86c759c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -77,7 +77,8 @@ aria2c_SOURCES = AllTest.cc\ MockSegment.h\ TripletTest.cc\ CookieHelperTest.cc\ - JsonTest.cc + JsonTest.cc\ + XmlRpcResponseTest.cc if ENABLE_XML_RPC aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc\ diff --git a/test/XmlRpcResponseTest.cc b/test/XmlRpcResponseTest.cc new file mode 100644 index 00000000..3c94b40e --- /dev/null +++ b/test/XmlRpcResponseTest.cc @@ -0,0 +1,68 @@ +#include "XmlRpcResponse.h" + +#include + +namespace aria2 { + +namespace xmlrpc { + +class XmlRpcResponseTest:public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(XmlRpcResponseTest); + CPPUNIT_TEST(testToJson); + CPPUNIT_TEST_SUITE_END(); +public: + void testToJson(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcResponseTest); + +void XmlRpcResponseTest::testToJson() +{ + std::vector results; + { + SharedHandle param = List::g(); + param->append(Integer::g(1)); + SharedHandle id = String::g("9"); + XmlRpcResponse res(0, param, id); + results.push_back(res); + std::string s = res.toJson("", false); + CPPUNIT_ASSERT_EQUAL(std::string("{\"id\":\"9\"," + "\"jsonrpc\":\"2.0\"," + "\"result\":[1]}"), + s); + } + { + // error response + SharedHandle param = Dict::g(); + param->put("code", Integer::g(1)); + param->put("message", "HELLO ERROR"); + XmlRpcResponse res(1, param, Null::g()); + results.push_back(res); + std::string s = res.toJson("", false); + CPPUNIT_ASSERT_EQUAL(std::string("{\"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); + } +} + +} // namespace xmlrpc + +} // namespace aria2