fix(Mock测试): 修复mock测试入参是多层json时无法获取某个节点数据
--bug=1008132 --user=宋天阳 【github#7835】json入参含数组时mock自定义脚本匹配成功后无返回值 https://www.tapd.cn/55049933/s/1073422
This commit is contained in:
parent
876dd41ebc
commit
7ff11ff950
|
@ -102,7 +102,6 @@ public class MockApiUtils {
|
|||
if (bodyObj.containsKey("raw")) {
|
||||
String xmlStr = bodyObj.getString("raw");
|
||||
JSONObject matchObj = XMLUtils.XmlToJson(xmlStr);
|
||||
// returnJsonArray.add(matchObj);
|
||||
returnJson = matchObj;
|
||||
}
|
||||
} else if (StringUtils.equalsIgnoreCase(type, "Raw")) {
|
||||
|
@ -111,7 +110,6 @@ public class MockApiUtils {
|
|||
if(StringUtils.isNotEmpty(raw)){
|
||||
JSONObject rawObject = new JSONObject();
|
||||
rawObject.put("raw",raw);
|
||||
// returnJsonArray.add(rawObject);
|
||||
returnJson = rawObject;
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +132,6 @@ public class MockApiUtils {
|
|||
bodyParamArr.put(kv.getString("name"), values);
|
||||
}
|
||||
}
|
||||
// returnJsonArray.add(bodyParamArr);
|
||||
returnJson = bodyParamArr;
|
||||
}
|
||||
}
|
||||
|
@ -388,11 +385,17 @@ public class MockApiUtils {
|
|||
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
|
||||
for(String key : bodyParamObj.keySet()){
|
||||
String value = String.valueOf(bodyParamObj.get(key));
|
||||
value = StringUtils.replace(value,"\\","\\\\");
|
||||
value = StringUtils.replace(value,"\"","\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body."+key+"\",\""+value+"\");\n");
|
||||
if(StringUtils.equalsIgnoreCase(key,"raw")){
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+value+"\");\n");
|
||||
}
|
||||
}
|
||||
String jsonBody = bodyParamObj.toJSONString();
|
||||
jsonBody = StringUtils.replace(jsonBody,"\\","\\\\");
|
||||
jsonBody = StringUtils.replace(jsonBody,"\"","\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body.json\",\""+jsonBody+"\");\n");
|
||||
}else {
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+requestMockParams.getBodyParams().toJSONString()+"\");\n");
|
||||
}
|
||||
|
@ -416,7 +419,6 @@ public class MockApiUtils {
|
|||
}
|
||||
return scriptStringBuffer.toString();
|
||||
}
|
||||
|
||||
private static String runScript(String script, String scriptLanguage) throws ScriptException {
|
||||
JSR223Sampler jmeterScriptSampler = new JSR223Sampler();
|
||||
jmeterScriptSampler.setScriptLanguage(scriptLanguage);
|
||||
|
|
|
@ -54,7 +54,18 @@ export default {
|
|||
},
|
||||
{
|
||||
title: this.$t('api_test.request.body')+this.$t('api_test.variable'),
|
||||
value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;',
|
||||
value: 'var returnMsg = requestParams.get("body.${param}");\nreturn returnMsg;\n' +
|
||||
"\n"+
|
||||
'//如果对象是多层JSON,需要引入fastjson协助解析:\n' +
|
||||
'// 以"{\"name\":\"user\",\"rows\":[{\"type\":1}]}" 为demo,取rows第1个的type数据:\n' +
|
||||
'import com.alibaba.fastjson.JSON;\n'+
|
||||
'import com.alibaba.fastjson.JSONArray;\n'+
|
||||
'import com.alibaba.fastjson.JSONObject;\n'+
|
||||
'\n'+
|
||||
'var jsonParam = requestParams.get("body.json");\n' +
|
||||
'JSONObject jsonObject = JSONObject.parseObject(jsonParam);\n' +
|
||||
'var returnMsg = jsonObject.getJSONArray("rows").getJSONObject(0).getString("type");\n' +
|
||||
'return returnMsg;\n',
|
||||
},
|
||||
{
|
||||
title: this.$t('api_test.request.body')+this.$t('api_test.variable')+" (Raw)",
|
||||
|
|
Loading…
Reference in New Issue