fix(接口测试): 带参数的mock函数不生效

--bug=1044204 --user=陈建星 【接口测试】请求体json类型-参数值为带参数的mock函数-执行接口-函数获取失败 https://www.tapd.cn/55049933/s/1551122
This commit is contained in:
AgAngle 2024-07-19 15:01:11 +08:00 committed by Craftsman
parent 03a5af4f73
commit 2f77188fd3
2 changed files with 45 additions and 10 deletions

View File

@ -87,23 +87,22 @@ public abstract class MsBodyConverter<T> {
* @return * @return
*/ */
protected String parseTextMock(String text) { protected String parseTextMock(String text) {
String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]"; String pattern = "[\"\\s:]@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]";
Pattern regex = Pattern.compile(pattern); Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text); Matcher matcher = regex.matcher(text);
while (matcher.find()) { while (matcher.find()) {
//取出group的最后一个字符 主要是防止 @string|number @string 这种情况 //取出group的最后一个字符 主要是防止 @string|number @string 这种情况
//如果是 或者, 结尾的 需要截取 //如果是 或者, 结尾的 需要截取
String group = matcher.group(); String group = matcher.group();
String lastChar = null;
if (group.endsWith(",") || group.endsWith("\"")) { if (group.endsWith(",") || group.endsWith("\"")) {
lastChar = group.substring(group.length() - 1);
group = group.substring(0, group.length() - 1); group = group.substring(0, group.length() - 1);
} }
text = text.replace(matcher.group(), StringUtils.join("${__Mock(", group, ")}", lastChar)); // 去掉第一个字符因为第一个字符是 " : 或者空格
group = group.substring(1, group.length());
text = text.replace(group, StringUtils.join("${__Mock(", group.replace(",", "\\,"), ")}"));
} }
return text; return text;
} }
/** /**
* 处理raw格式参数 * 处理raw格式参数
* 包含了 json 等格式 * 包含了 json 等格式

View File

@ -1,7 +1,7 @@
package io.metersphere.api.parser.jmeter.body; package io.metersphere.api.parser.jmeter.body;
import io.metersphere.api.dto.request.http.body.JsonBody; import io.metersphere.api.dto.request.http.body.JsonBody;
import io.metersphere.jmeter.mock.Mock; import io.metersphere.jmeter.functions.MockFunction;
import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
@ -12,6 +12,8 @@ import org.springframework.http.MediaType;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @Author: jianxing * @Author: jianxing
@ -44,11 +46,11 @@ public class MsJsonBodyConverter extends MsBodyConverter<JsonBody> {
if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) { if (StringUtils.startsWith(value, "[") && StringUtils.endsWith(value, "]")) {
List list = JSON.parseArray(jsonStr); List list = JSON.parseArray(jsonStr);
parseMock(list); parseMock(list);
return JSON.toJSONString(list); return replaceMockComma(JSON.toJSONString(list));
} else { } else {
Map<String, Object> map = JSON.parseObject(jsonStr, Map.class); Map<String, Object> map = JSON.parseObject(jsonStr, Map.class);
parseMock(map); parseMock(map);
return JSON.toJSONString(map); return replaceMockComma(JSON.toJSONString(map));
} }
} catch (Exception e) { } catch (Exception e) {
// 如果json串中的格式不是标准的json格式正则替换变量 // 如果json串中的格式不是标准的json格式正则替换变量
@ -66,7 +68,7 @@ public class MsJsonBodyConverter extends MsBodyConverter<JsonBody> {
parseMock((Map) obj); parseMock((Map) obj);
} else if (obj instanceof String) { } else if (obj instanceof String) {
if (StringUtils.isNotBlank((String) obj)) { if (StringUtils.isNotBlank((String) obj)) {
String str = Mock.buildFunctionCallString((String) obj); String str = buildFunctionCallString((String) obj);
replaceDataMap.put(index, str); replaceDataMap.put(index, str);
} }
} }
@ -87,10 +89,44 @@ public class MsJsonBodyConverter extends MsBodyConverter<JsonBody> {
parseMock((Map) value); parseMock((Map) value);
} else if (value instanceof String) { } else if (value instanceof String) {
if (StringUtils.isNotBlank((String) value)) { if (StringUtils.isNotBlank((String) value)) {
value = Mock.buildFunctionCallString((String) value); value = buildFunctionCallString((String) value);
} }
map.put(key, value); map.put(key, value);
} }
} }
} }
/**
* ${__Mock(@integer(1,100))} -> ${__Mock(@integer(1\,100))}
* mock 函数中的逗号转义 jmeter 识别
*
* @param text
* @return
*/
protected String replaceMockComma(String text) {
String pattern = "\\$\\{__Mock\\((.+)\\)\\}";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(text);
while (matcher.find()) {
String group = matcher.group();
if (StringUtils.contains(group, ",")) {
text = text.replace(group, group.replace(",", "\\,"));
}
}
return text;
}
/**
* 将文本中的 @xxx 转换成 ${__Mock(@xxx)}
* 这里不使用 Mock.buildFunctionCallString 的逗号转义
*
* @param input
* @return
*/
public static String buildFunctionCallString(String input) {
if (!StringUtils.startsWith(input, "@")) {
return input;
}
return String.format("${%s(%s)}", MockFunction.KEY, input);
}
} }