wb-hx510875 před 6 roky
rodič
revize
b4e95dcd5a

+ 3 - 1
core/src/Utils.cc

@@ -235,11 +235,13 @@ std::string AlibabaCloud::GetEnv(const std::string env)
 std::string AlibabaCloud::MapToJson(const std::map<std::string, std::string> &maps)
 std::string AlibabaCloud::MapToJson(const std::map<std::string, std::string> &maps)
 {
 {
   Json::Value jsonObject;
   Json::Value jsonObject;
+  Json::FastWriter writer; 
   for (std::map<std::string, std::string>::const_iterator iter = maps.begin(); iter != maps.end(); ++iter)
   for (std::map<std::string, std::string>::const_iterator iter = maps.begin(); iter != maps.end(); ++iter)
   {
   {
     jsonObject[iter->first] = iter->second;
     jsonObject[iter->first] = iter->second;
   }
   }
-  return jsonObject.toStyledString();
+  std::string unformat_str = writer.write(jsonObject);
+  return unformat_str.substr(0, unformat_str.length() - 1);
 }
 }
 
 
 std::map<std::string, std::string> AlibabaCloud::JsonToMap(const std::string &json)
 std::map<std::string, std::string> AlibabaCloud::JsonToMap(const std::string &json)

+ 1 - 1
test/core/commonclient_ut.cc

@@ -155,7 +155,7 @@ namespace {
     EXPECT_TRUE(nbr == 0);
     EXPECT_TRUE(nbr == 0);
     client->asyncExecute(rf);
     client->asyncExecute(rf);
     usleep(10000);
     usleep(10000);
-    EXPECT_TRUE(nbr == 1);
+    EXPECT_EQ(nbr, 1);
     ShutdownSdk();
     ShutdownSdk();
   }
   }
 
 

+ 0 - 1
test/core/roaserviceclient_ut.cc

@@ -95,5 +95,4 @@ TEST(RoaServiceClient, basic) {
   req.setParameter("Content-Type", "test-content-type");
   req.setParameter("Content-Type", "test-content-type");
   http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
   http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
   EXPECT_TRUE(http_req.url().toString() == "http://cn-shanghai/?Accept=tets-accept&Content-Type=test-content-type&a=b");
   EXPECT_TRUE(http_req.url().toString() == "http://cn-shanghai/?Accept=tets-accept&Content-Type=test-content-type&a=b");
-
 }
 }

+ 10 - 4
test/core/servicerequest_ut.cc

@@ -23,16 +23,17 @@ namespace {
     {}
     {}
 
 
     using ServiceRequest::addParameter;
     using ServiceRequest::addParameter;
-    using ServiceRequest::removeParameter;
+    using ServiceRequest::coreParameter;
     using ServiceRequest::parameter;
     using ServiceRequest::parameter;
+    using ServiceRequest::removeParameter;
+    using ServiceRequest::setBodyParameter;
     using ServiceRequest::setContent;
     using ServiceRequest::setContent;
+    using ServiceRequest::setCoreParameter;
     using ServiceRequest::setParameter;
     using ServiceRequest::setParameter;
     using ServiceRequest::setParameters;
     using ServiceRequest::setParameters;
-    using ServiceRequest::setResourcePath;
     using ServiceRequest::setProduct;
     using ServiceRequest::setProduct;
+    using ServiceRequest::setResourcePath;
     using ServiceRequest::setVersion;
     using ServiceRequest::setVersion;
-    using ServiceRequest::setCoreParameter;
-    using ServiceRequest::coreParameter;
   };
   };
 
 
   TEST(ServiceRequest, basic) {
   TEST(ServiceRequest, basic) {
@@ -63,6 +64,8 @@ namespace {
     EXPECT_TRUE(sr1.parameter("ka") == "va");
     EXPECT_TRUE(sr1.parameter("ka") == "va");
     EXPECT_TRUE(sr1.coreParameter("ka") == "va");
     EXPECT_TRUE(sr1.coreParameter("ka") == "va");
 
 
+    sr1.setBodyParameter("name", "value");
+
     sr1.setContent("123456", 6);
     sr1.setContent("123456", 6);
     EXPECT_TRUE(sr1.contentSize() == 6);
     EXPECT_TRUE(sr1.contentSize() == 6);
     EXPECT_TRUE(sr1.hasContent() == true);
     EXPECT_TRUE(sr1.hasContent() == true);
@@ -90,6 +93,9 @@ namespace {
     EXPECT_TRUE(pc.at("km") == "vm");
     EXPECT_TRUE(pc.at("km") == "vm");
     EXPECT_TRUE(pc.at("kn") == "vn");
     EXPECT_TRUE(pc.at("kn") == "vn");
 
 
+    ServiceRequest::ParameterCollection bp = sr1.bodyParameters();
+    EXPECT_EQ(bp.at("name"), "value");
+
     EXPECT_TRUE(sr1.connectTimeout() == kInvalidTimeout);
     EXPECT_TRUE(sr1.connectTimeout() == kInvalidTimeout);
     EXPECT_TRUE(sr1.readTimeout() == kInvalidTimeout);
     EXPECT_TRUE(sr1.readTimeout() == kInvalidTimeout);
     sr1.setConnectTimeout(1234);
     sr1.setConnectTimeout(1234);

+ 17 - 0
test/core/utils_ut.cc

@@ -61,6 +61,23 @@ namespace {
     EXPECT_TRUE(decoded == url);
     EXPECT_TRUE(decoded == url);
   }
   }
 
 
+  TEST(UtilsTest, MapToJson){
+    std::map<std::string, std::string> maps;
+    maps.insert(std::make_pair("foo", "bar"));
+    std::string jsonStr = MapToJson(maps);
+    EXPECT_EQ(jsonStr, "{\"foo\":\"bar\"}");
+  }
+
+  TEST(UtilsTest, JsonToMap){
+    std::map<std::string, std::string> targetMaps;
+    targetMaps.insert(std::make_pair("foo", "bar"));
+    targetMaps.insert(std::make_pair("int", "1"));
+    targetMaps.insert(std::make_pair("array","foo,bar"));
+    std::string jsonStr = "{\"foo\":\"bar\",\"int\":1,\"array\":[\"foo\",\"bar\"]}";
+    std::map<std::string, std::string> maps = JsonToMap(jsonStr);
+    EXPECT_EQ(targetMaps, maps);
+  }
+
   TEST(Utils, GetEnv) {
   TEST(Utils, GetEnv) {
     const std::string var1 = GetEnv("PATH");
     const std::string var1 = GetEnv("PATH");
     EXPECT_FALSE(var1.empty());
     EXPECT_FALSE(var1.empty());