fix(接口测试): 修复Json数组无法使用mock数据的问题

--bug=1020800 --user=宋天阳 【接口测试】github#20570,接口场景测试 JSON请求体中
json数组中的mock函数无效 https://www.tapd.cn/55049933/s/1319974
This commit is contained in:
song-tianyang 2022-12-27 17:00:57 +08:00 committed by 刘瑞斌
parent ce55c0acac
commit 9ccb214247
2 changed files with 48 additions and 14 deletions

View File

@ -16,9 +16,9 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -102,11 +102,19 @@ public class Body {
} else {
try {
if (StringUtils.isNotEmpty(this.getRaw())) {
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
if (!this.getRaw().contains("$ref")) {
jsonMockParse(map);
if (StringUtils.startsWith(this.getRaw(), "[") && StringUtils.endsWith(this.raw, "]")) {
List list = JSON.parseArray(this.getRaw());
if (!this.getRaw().contains("$ref")) {
jsonMockParse(list);
}
this.raw = JSONUtil.parser(list.toString());
} else {
Map<String, Object> map = JSON.parseObject(this.getRaw(), Map.class);
if (!this.getRaw().contains("$ref")) {
jsonMockParse(map);
}
this.raw = JSONUtil.parser(map.toString());
}
this.raw = JSONUtil.parser(map.toString());
}
} catch (Exception e) {
LoggerUtil.error("json mock value is abnormal", e);
@ -118,7 +126,9 @@ public class Body {
private void jsonMockParse(Map map) {
for (Object key : map.keySet()) {
Object value = map.get(key);
if (value instanceof JSONObject) {
if (value instanceof List) {
jsonMockParse((List) value);
} else if (value instanceof Map) {
jsonMockParse((Map) value);
} else if (value instanceof String) {
if (StringUtils.isNotBlank((String) value)) {
@ -129,6 +139,28 @@ public class Body {
}
}
private void jsonMockParse(List list) {
Map<Integer, String> replaceDataMap = new HashMap<>();
for (int index = 0; index < list.size(); index++) {
Object obj = list.get(index);
if (obj instanceof Map) {
jsonMockParse((Map) obj);
} else if (obj instanceof String) {
if (StringUtils.isNotBlank((String) obj)) {
String str = ScriptEngineUtils.buildFunctionCallString((String) obj);
replaceDataMap.put(index, str);
}
}
}
for (Map.Entry<Integer, String> entry : replaceDataMap.entrySet()) {
int replaceIndex = entry.getKey();
String replaceStr = entry.getValue();
list.set(replaceIndex, replaceStr);
}
}
private HTTPFileArg[] httpFileArgs(String requestId) {
List<HTTPFileArg> list = new ArrayList<>();
if (StringUtils.equalsAnyIgnoreCase(this.type, WWW_FROM, FORM_DATA) && CollectionUtils.isNotEmpty(this.getKvs())) {

View File

@ -184,7 +184,7 @@
</el-col>
</el-row>
<template v-if="!hidden && pickValue.properties && !isArray(pickValue) && reloadItemOver">
<template v-if="!hidden && pickValue.properties && !isArray(pickValue)">
<json-schema-editor
v-for="(item, key, index) in pickValue.properties"
:value="{ [key]: item }"
@ -205,7 +205,7 @@
:need-mock="needMock"
@reloadItems="reloadItems" />
</template>
<template v-if="!hidden && isArray(pickValue) && reloadItemOver">
<template v-if="!hidden && isArray(pickValue)">
<json-schema-editor
v-for="(item, key, index) in pickValue.items"
:value="{ [key]: item }"
@ -400,7 +400,6 @@ export default {
hidden: false,
countAdd: 1,
modalVisible: false,
reloadItemOver: true,
reloadSelfOver: true,
advancedValue: {},
addProp: {}, //
@ -414,7 +413,7 @@ export default {
} else {
if (this.pickValue) {
if (this.pickValue.hidden === undefined) {
this.hidden = this.root ? false : true;
this.hidden = !this.root;
} else {
this.hidden = this.root ? false : this.pickValue.hidden;
}
@ -546,6 +545,7 @@ export default {
node.properties || this.$set(node, 'properties', {});
const props = node.properties;
this.$set(props, name, { type: type, mock: { mock: '' } });
this.reloadItems();
}
},
addCustomNode() {
@ -609,10 +609,12 @@ export default {
this.$emit('reloadItems');
},
reloadItems() {
this.reloadItemOver = false;
this.$nextTick(() => {
this.reloadItemOver = true;
});
if (!this.hidden) {
this.hidden = !this.hidden;
this.$nextTick(() => {
this.hidden = !this.hidden;
});
}
},
reloadSelf() {