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

修复Mock期望可以使用JMeter函数提示对问题
This commit is contained in:
song-tianyang 2022-03-10 18:59:42 +08:00 committed by CountryBuilder
parent 1a8d6bf065
commit f641f6f6b7
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

@ -782,139 +782,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,47 +1,45 @@
<template>
<div class="request-form">
<keep-alive>
<component
v-bind:is="component"
:isMax="isMax"
:show-btn="showBtn"
:show-version = "showVersion"
:expandedNode="expandedNode"
:scenario="scenario"
:controller="scenario"
:timer="scenario"
:assertions="scenario"
:extract="scenario"
:jsr223-processor="scenario"
:request="scenario"
:currentScenario="currentScenario"
:currentEnvironmentId="currentEnvironmentId"
:node="node"
:draggable="draggable"
:title="title"
:color="titleColor"
:response="response"
:environment-type="environmentType"
:environment-group-id="envGroupId"
:background-color="backgroundColor"
:project-list="projectList"
:env-map="envMap"
:message="message"
:api-id="apiId"
:scenario-definition="scenarioDefinition"
:if-from-variable-advance="ifFromVariableAdvance"
@suggestClick="suggestClick(node)"
@remove="remove"
@runScenario="runScenario"
@stopScenario="stopScenario"
@copyRow="copyRow"
@refReload="refReload"
@openScenario="openScenario"
@setDomain="setDomain"
@savePreParams="savePreParams"
@editScenarioAdvance="editScenarioAdvance"
/>
</keep-alive>
<component
v-bind:is="component"
:isMax="isMax"
:show-btn="showBtn"
:show-version="showVersion"
:expandedNode="expandedNode"
:scenario="scenario"
:controller="scenario"
:timer="scenario"
:assertions="scenario"
:extract="scenario"
:jsr223-processor="scenario"
:request="scenario"
:currentScenario="currentScenario"
:currentEnvironmentId="currentEnvironmentId"
:node="node"
:draggable="draggable"
:title="title"
:color="titleColor"
:response="response"
:environment-type="environmentType"
:environment-group-id="envGroupId"
:background-color="backgroundColor"
:project-list="projectList"
:env-map="envMap"
:message="message"
:api-id="apiId"
:scenario-definition="scenarioDefinition"
:if-from-variable-advance="ifFromVariableAdvance"
@suggestClick="suggestClick(node)"
@remove="remove"
@runScenario="runScenario"
@stopScenario="stopScenario"
@copyRow="copyRow"
@refReload="refReload"
@openScenario="openScenario"
@setDomain="setDomain"
@savePreParams="savePreParams"
@editScenarioAdvance="editScenarioAdvance"
/>
</div>
</template>

View File

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

@ -1 +1 @@
Subproject commit f12e04064dc1288becfa63c48c4db59dc5322292
Subproject commit fe4593657ec9d2733ad31057fa636a3562e38230

View File

@ -3056,6 +3056,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

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

View File

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