Fixed the bug that JSONP callback function name is not encoded

properly.
pull/1/head
Tatsuhiro Tsujikawa 2011-03-16 12:55:12 +09:00
parent dfdf98d2f7
commit 15730aa560
2 changed files with 36 additions and 3 deletions

View File

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

View File

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