Added XmlRpcResponseTest. Set null as id if id is not sent.

Removed unused XmlRpcResponse::toJsonBatch(). Set default value false
to gzip argument of xmlrpc::toJsonBatch().
pull/1/head
Tatsuhiro Tsujikawa 2011-03-14 15:31:26 +09:00
parent bf01bb84b6
commit 5a1fb3875f
5 changed files with 73 additions and 10 deletions

View File

@ -149,8 +149,7 @@ HttpServerBodyCommand::processJsonRpcRequest(const Dict* jsondict)
SharedHandle<ValueBase> id = jsondict->get("id"); SharedHandle<ValueBase> id = jsondict->get("id");
if(!id) { if(!id) {
return createJsonRpcErrorResponse(-32600, "Invalid Request.", return createJsonRpcErrorResponse(-32600, "Invalid Request.", Null::g());
SharedHandle<ValueBase>());
} }
const String* methodName = asString(jsondict->get("method")); const String* methodName = asString(jsondict->get("method"));
if(!methodName) { if(!methodName) {

View File

@ -175,10 +175,7 @@ OutputStream& encodeJsonAll
} }
SharedHandle<Dict> dict = Dict::g(); SharedHandle<Dict> dict = Dict::g();
dict->put("jsonrpc", "2.0"); dict->put("jsonrpc", "2.0");
// TODO id may be null? dict->put("id", id);
if(id) {
dict->put("id", id);
}
if(code == 0) { if(code == 0) {
dict->put("result", param); dict->put("result", param);
} else { } else {

View File

@ -70,14 +70,12 @@ struct XmlRpcResponse {
// Encodes RPC response in JSON. If callback is not empty, the // Encodes RPC response in JSON. If callback is not empty, the
// resulting string is JSONP. // resulting string is JSONP.
std::string toJson(const std::string& callback, bool gzip = false) const; 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 std::string toJsonBatch
(const std::vector<XmlRpcResponse>& results, (const std::vector<XmlRpcResponse>& results,
const std::string& callback, const std::string& callback,
bool gzip); bool gzip = false);
} // namespace xmlrpc } // namespace xmlrpc

View File

@ -77,7 +77,8 @@ aria2c_SOURCES = AllTest.cc\
MockSegment.h\ MockSegment.h\
TripletTest.cc\ TripletTest.cc\
CookieHelperTest.cc\ CookieHelperTest.cc\
JsonTest.cc JsonTest.cc\
XmlRpcResponseTest.cc
if ENABLE_XML_RPC if ENABLE_XML_RPC
aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc\ aria2c_SOURCES += XmlRpcRequestParserControllerTest.cc\

View File

@ -0,0 +1,68 @@
#include "XmlRpcResponse.h"
#include <cppunit/extensions/HelperMacros.h>
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<XmlRpcResponse> results;
{
SharedHandle<List> param = List::g();
param->append(Integer::g(1));
SharedHandle<String> 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<Dict> 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