refactor(接口测试): 修复Mock期望可以使用JMeter函数提示对问题

修复Mock期望可以使用JMeter函数提示对问题
This commit is contained in:
song-tianyang 2022-03-10 18:59:42 +08:00 committed by 刘瑞斌
parent bb77395e3e
commit 78bf105b45
9 changed files with 60 additions and 186 deletions

View File

@ -146,7 +146,7 @@ public class MockApiUtils {
return returnJson;
}
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) {
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj,boolean useJMeterFunc) {
JSONObject returnObj = new JSONObject();
if (bodyReturnObj == null) {
return returnObj;
@ -157,7 +157,7 @@ public class MockApiUtils {
try {
JsonSchemaReturnObj obj = bodyReturnObj.getObject(key, JsonSchemaReturnObj.class);
if (StringUtils.equals("object", obj.getType())) {
JSONObject itemObj = parseJsonSchema(obj.getProperties());
JSONObject itemObj = parseJsonSchema(obj.getProperties(),useJMeterFunc);
if (!itemObj.isEmpty()) {
returnObj.put(key, itemObj);
}
@ -167,13 +167,16 @@ public class MockApiUtils {
if (itemObj.containsKey("type")) {
if (StringUtils.equals("object", itemObj.getString("type")) && itemObj.containsKey("properties")) {
JSONObject arrayObj = itemObj.getJSONObject("properties");
JSONObject parseObj = parseJsonSchema(arrayObj);
JSONObject parseObj = parseJsonSchema(arrayObj,useJMeterFunc);
JSONArray array = new JSONArray();
array.add(parseObj);
returnObj.put(key, array);
} else if (StringUtils.equals("string", itemObj.getString("type")) && itemObj.containsKey("mock")) {
JsonSchemaReturnObj arrayObj = JSONObject.toJavaObject(itemObj, JsonSchemaReturnObj.class);
String value = getMockValues(arrayObj.getMockValue());
String value = arrayObj.getMockValue();
if(useJMeterFunc){
value = getMockValues(arrayObj.getMockValue());
}
JSONArray array = new JSONArray();
array.add(value);
returnObj.put(key, array);
@ -184,7 +187,7 @@ public class MockApiUtils {
String values = obj.getMockValue();
if (StringUtils.isEmpty(values)) {
values = "";
} else {
} else if(useJMeterFunc){
try {
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
} catch (Exception e) {
@ -254,7 +257,7 @@ public class MockApiUtils {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false);
returnStr = returnObj.toJSONString();
}
} else {
@ -379,7 +382,7 @@ public class MockApiUtils {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj,false);
returnStr = returnObj.toJSONString();
}
} else {

View File

@ -257,7 +257,7 @@ public class MockScriptEngineUtils {
LogUtil.error(e);
}
}
if (StringUtils.isEmpty(value)) {
if (StringUtils.isEmpty(value) || StringUtils.equals(value,"null")) {
value = paramKey;
}
return value;

View File

@ -786,139 +786,6 @@ public class MockConfigService {
return returnStr;
}
public String updateHttpServletResponse(List<ApiDefinitionWithBLOBs> apis, HttpServletResponse response) {
String returnStr = "";
try {
if (CollectionUtils.isEmpty(apis)) {
response.setStatus(404);
} else {
for (ApiDefinitionWithBLOBs api : apis) {
int status = 404;
if (api.getResponse() != null) {
JSONObject respObj = JSONObject.parseObject(api.getResponse());
if (respObj.containsKey("headers")) {
JSONArray headersArr = respObj.getJSONArray("headers");
for (int i = 0; i < headersArr.size(); i++) {
JSONObject obj = headersArr.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) {
response.setHeader(obj.getString("name"), obj.getString("value"));
}
}
}
if (respObj.containsKey("statusCode")) {
JSONArray statusCodeArr = respObj.getJSONArray("statusCode");
for (int i = 0; i < statusCodeArr.size(); i++) {
JSONObject obj = statusCodeArr.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) {
// response.setHeader(obj.getString("name"), obj.getString("value"));
try {
status = Integer.parseInt(obj.getString("name"));
} catch (Exception e) {
}
}
}
}
if (respObj.containsKey("body")) {
JSONObject bodyObj = respObj.getJSONObject("body");
if (bodyObj.containsKey("type")) {
String type = bodyObj.getString("type");
if (StringUtils.equals(type, "JSON")) {
//判断是否是JsonSchema
boolean isJsonSchema = false;
if (bodyObj.containsKey("format")) {
String foramtValue = String.valueOf(bodyObj.get("format"));
if (StringUtils.equals("JSON-SCHEMA", foramtValue)) {
isJsonSchema = true;
}
}
if (isJsonSchema) {
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
JSONObject bodyReturnObj = JSONObject.parseObject(bodyRetunStr);
JSONObject returnObj = MockApiUtils.parseJsonSchema(bodyReturnObj);
returnStr = returnObj.toJSONString();
}
} else {
if (bodyObj.containsKey("raw")) {
returnStr = bodyObj.getString("raw");
}
}
} else if (StringUtils.equalsAny(type, "XML", "Raw")) {
if (bodyObj.containsKey("raw")) {
String raw = bodyObj.getString("raw");
returnStr = raw;
}
} else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
Map<String, String> paramMap = new LinkedHashMap<>();
if (bodyObj.containsKey("kvs")) {
JSONArray bodyParamArr = new JSONArray();
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
for (int i = 0; i < kvsArr.size(); i++) {
JSONObject kv = kvsArr.getJSONObject(i);
if (kv.containsKey("name")) {
String values = kv.getString("value");
if (StringUtils.isEmpty(values)) {
values = "";
} else {
try {
values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
} catch (Exception e) {
}
}
paramMap.put(kv.getString("name"), values);
}
}
}
returnStr = JSONObject.toJSONString(paramMap);
//Binary的先不处理
// } else if (StringUtils.equals(type, "BINARY")) {
// Map<String, String> paramMap = new LinkedHashMap<>();
// if (bodyObj.containsKey("binary")) {
// JSONArray kvsArr = bodyObj.getJSONArray("kvs");
// for (int i = 0; i < kvsArr.size(); i++) {
// JSONObject kv = kvsArr.getJSONObject(i);
// if (kv.containsKey("description") && kv.containsKey("files")) {
// String name = kv.getString("description");
// JSONArray fileArr = kv.getJSONArray("files");
// String allValue = "";
// for (int j = 0; j < fileArr.size(); j++) {
// JSONObject fileObj = fileArr.getJSONObject(j);
// if (fileObj.containsKey("name")) {
// String values = fileObj.getString("name");
// if (StringUtils.isEmpty(values)) {
// values = "";
// } else {
// try {
// values = values.startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(values) : values;
// } catch (Exception e) {
// }
// }
//
// allValue += values + " ;";
// }
// }
// paramMap.put(name, allValue);
// }
// }
// }
// returnStr = JSONObject.toJSONString(paramMap);
}
}
}
}
if (StringUtils.isNotEmpty(returnStr) && status == 404) {
status = 200;
}
response.setStatus(status);
}
}
} catch (Exception e) {
LogUtil.error(e);
}
return returnStr;
}
public MockExpectConfigWithBLOBs findMockExpectConfigById(String id) {
return mockExpectConfigMapper.selectByPrimaryKey(id);
}

View File

@ -1,11 +1,10 @@
<template>
<div class="request-form">
<keep-alive>
<component
v-bind:is="component"
:isMax="isMax"
:show-btn="showBtn"
:show-version = "showVersion"
:show-version="showVersion"
:expandedNode="expandedNode"
:scenario="scenario"
:controller="scenario"
@ -41,7 +40,6 @@
@savePreParams="savePreParams"
@editScenarioAdvance="editScenarioAdvance"
/>
</keep-alive>
</div>
</template>

View File

@ -67,8 +67,11 @@
},
methods: {
funcSearch(queryString, cb) {
let results = [];
if(!this.showMockVars){
let funcs = MOCKJS_FUNC.concat(JMETER_FUNC);
let results = queryString ? funcs.filter(this.funcFilter(queryString)) : funcs;
results = queryString ? funcs.filter(this.funcFilter(queryString)) : funcs;
}
// callback
cb(results);
},

@ -1 +1 @@
Subproject commit ddcb1d3381bedaad8174e55a07df5c9371af5e48
Subproject commit ed3f0a6630c6f986fba4f9817f51310a6d37f5aa

View File

@ -3072,6 +3072,7 @@ export default {
error_report_library: {
name: "Error report",
assertion: "Error code assertion",
tips:"Tips: Use error report in \"Project Settings - Project environment-Assertions\"",
use_error_report: "Use error code",
use_desc: "In case of conflict between error code and error assertions, it is treated as error code",
option: {

View File

@ -3076,6 +3076,7 @@ export default {
error_report_library: {
name: "误报库",
assertion: "误报断言",
tips:"注:在\"项目设置-设置环境-全局断言\"中启用误报将进行以下规则匹配",
use_error_report: "启用误报",
use_desc: "失败断言与误报规则冲突时统一处理为误报",
option: {

View File

@ -3075,6 +3075,7 @@ export default {
error_report_library: {
name: "誤報庫",
assertion: "誤報斷言",
tips:"注:在\"項目設置-設置環境-全局斷言\"中啟用誤報將進行以下規則匹配",
use_error_report: "啟用誤報",
use_desc: "失敗斷言與誤報規則衝突時統一處理為誤報",
option: {