Ver código fonte

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().
Tatsuhiro Tsujikawa 14 anos atrás
pai
commit
5a1fb3875f

+ 1 - 2
src/HttpServerBodyCommand.cc

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

+ 1 - 4
src/XmlRpcResponse.cc

@@ -175,10 +175,7 @@ OutputStream& encodeJsonAll
   }
   SharedHandle<Dict> 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 {

+ 1 - 3
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<XmlRpcResponse>& results,
  const std::string& callback,
- bool gzip);
+ bool gzip = false);
 
 } // namespace xmlrpc
 

+ 2 - 1
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\

+ 68 - 0
test/XmlRpcResponseTest.cc

@@ -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