Procházet zdrojové kódy

Fixed the bug that JSONP callback function name is not encoded
properly.

Tatsuhiro Tsujikawa před 14 roky
rodič
revize
15730aa560
2 změnil soubory, kde provedl 36 přidání a 3 odebrání
  1. 9 3
      src/RpcResponse.cc
  2. 27 0
      test/RpcResponseTest.cc

+ 9 - 3
src/RpcResponse.cc

@@ -168,7 +168,7 @@ OutputStream& encodeJsonAll
  int code,
  const SharedHandle<ValueBase>& param,
  const SharedHandle<ValueBase>& id,
- const std::string& callback)
+ const std::string& callback = A2STR::NIL)
 {
   if(!callback.empty()) {
     o << callback << "(";
@@ -212,16 +212,22 @@ OutputStream& encodeJsonBatchAll
  const std::vector<RpcResponse>& results,
  const std::string& callback)
 {
+  if(!callback.empty()) {
+    o << callback << "(";
+  }
   o << "[";
   if(!results.empty()) {
-    encodeJsonAll(o, results[0].code, results[0].param, results[0].id,callback);
+    encodeJsonAll(o, results[0].code, results[0].param, results[0].id);
   }
   for(std::vector<RpcResponse>::const_iterator i = results.begin()+1,
         eoi = results.end(); i != eoi; ++i) {
     o << ",";
-    encodeJsonAll(o, (*i).code, (*i).param, (*i).id, callback);
+    encodeJsonAll(o, (*i).code, (*i).param, (*i).id);
   }
   o << "]";
+  if(!callback.empty()) {
+    o << ")";
+  }
   return o;
 }
 } // namespace

+ 27 - 0
test/RpcResponseTest.cc

@@ -36,6 +36,12 @@ void RpcResponseTest::testToJson()
                                      "\"jsonrpc\":\"2.0\","
                                      "\"result\":[1]}"),
                          s);
+    // with callback
+    s = res.toJson("cb", false);
+    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"id\":\"9\","
+                                     "\"jsonrpc\":\"2.0\","
+                                     "\"result\":[1]})"),
+                         s);
   }
   {
     // error response
@@ -51,6 +57,14 @@ void RpcResponseTest::testToJson()
                                      "\"jsonrpc\":\"2.0\""
                                      "}"),
                          s);
+    // with callback
+    s = res.toJson("cb", false);
+    CPPUNIT_ASSERT_EQUAL(std::string("cb({\"error\":{\"code\":1,"
+                                     "\"message\":\"HELLO ERROR\"},"
+                                     "\"id\":null,"
+                                     "\"jsonrpc\":\"2.0\""
+                                     "})"),
+                         s);
   }
   {
     // batch response
@@ -66,6 +80,19 @@ void RpcResponseTest::testToJson()
                                      "}"
                                      "]"),
                          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);
   }
 }