fit(接口测试): jsonpath 断言推荐自动转义正则特殊字符

This commit is contained in:
chenjianxing 2020-11-23 12:08:30 +08:00
parent 21ea6bc70a
commit 744de357c3
2 changed files with 19 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import org.apache.commons.lang3.StringUtils;
public class JsonPathUtils {
@ -68,9 +69,26 @@ public class JsonPathUtils {
.compareTo( (String)b.get("json_path") )
);
// 正则特殊字符转义
allJsons.forEach(item -> {
item.put("regular_expression", escapeExprSpecialWord((String) item.get("json_value")));
});
return allJsons;
}
public static String escapeExprSpecialWord(String keyword) {
if (StringUtils.isNotBlank(keyword)) {
String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
for (String key : fbsArr) {
if (keyword.contains(key)) {
keyword = keyword.replace(key, "\\" + key);
}
}
}
return keyword;
}
private static String formatJson(String json_path){
String ret="";
String reg = ".(\\d{1,3}).{0,1}";

View File

@ -107,7 +107,7 @@ export default {
jsonPathList.forEach(jsonPath => {
let jsonItem = new JSONPath();
jsonItem.expression = jsonPath.json_path;
jsonItem.expect = jsonPath.json_value;
jsonItem.expect = jsonPath.regular_expression;
jsonItem.setJSONPathDescription();
this.assertions.jsonPath.push(jsonItem);
});