feat(接口测试): Mock期望设置中key-value参数增加判断范围选项 (#10402)

Mock期望设置中key-value参数增加判断范围选项

Co-authored-by: song-tianyang <tianyang.song@fit2cloud.com>
This commit is contained in:
metersphere-bot 2022-02-10 18:17:02 +08:00 committed by GitHub
parent a9085e2186
commit 22016e704e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 433 additions and 117 deletions

View File

@ -310,8 +310,8 @@ public class ApiDefinitionController {
esbImportService.templateExport(response);
}
@GetMapping("/getMockEnvironment/{projectId}/{protocal}")
public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId, @PathVariable String protocal, HttpServletRequest request) {
@GetMapping("/getMockEnvironment/{projectId}")
public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId) {
return apiTestEnvironmentService.getMockEnvironmentByProjectId(projectId);
}

View File

@ -1,6 +1,6 @@
package io.metersphere.api.controller;
import io.metersphere.api.dto.mock.MockApiUtils;
import io.metersphere.api.mock.utils.MockApiUtils;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.api.tcp.TCPPool;
import io.metersphere.base.domain.Project;

View File

@ -1,11 +1,13 @@
package io.metersphere.api.controller;
import io.metersphere.api.dto.mock.MockApiUtils;
import io.metersphere.api.dto.mock.MockParamSuggestions;
import io.metersphere.api.dto.mock.MockTestDataRequest;
import io.metersphere.api.dto.mockconfig.MockConfigRequest;
import io.metersphere.api.dto.mockconfig.MockExpectConfigRequest;
import io.metersphere.api.dto.mockconfig.response.MockConfigResponse;
import io.metersphere.api.dto.mockconfig.response.MockExpectConfigResponse;
import io.metersphere.api.mock.utils.MockApiUtils;
import io.metersphere.api.mock.utils.MockTestDataUtil;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
@ -74,4 +76,10 @@ public class MockConfigController {
return returnMap;
}
@PostMapping("/getMockTestData")
public List<MockTestDataRequest> getMockTestData(@RequestBody List<MockTestDataRequest> requestArray) {
MockTestDataUtil testDataUtil = new MockTestDataUtil();
return testDataUtil.parseTestDataByRequest(requestArray);
}
}

View File

@ -0,0 +1,12 @@
package io.metersphere.api.dto.mock;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MockConfigRequestParams {
private String key;
private String condition;
private String value;
}

View File

@ -0,0 +1,13 @@
package io.metersphere.api.dto.mock;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class MockTestDataRequest {
//ID 返回前台用于前台拿来取值
private String id;
private String condition;
private String value;
}

View File

@ -0,0 +1,7 @@
package io.metersphere.api.mock.dto;
public enum MockParamConditionEnum {
VALUE_EQUALS,VALUE_NOT_EQUALS,VALUE_CONTAINS,
LENGTH_EQUALS,LENGTH_NOT_EQUALS,LENGTH_LARGE_THAN,LENGTH_SHOT_THAN,
REGULAR_MATCH
}

View File

@ -1,25 +1,30 @@
package io.metersphere.api.dto.mock;
package io.metersphere.api.mock.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONValidator;
import io.metersphere.api.dto.mock.MockConfigRequestParams;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.mockconfig.response.JsonSchemaReturnObj;
import io.metersphere.api.mock.dto.MockParamConditionEnum;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.json.JSONSchemaGenerator;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils;
import io.metersphere.jmeter.utils.ScriptEngineUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.samplers.SampleResult;
import org.json.XML;
import javax.script.ScriptException;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author song.tianyang
@ -207,15 +212,23 @@ public class MockApiUtils {
return values;
}
public static JSONObject getParamsByJSONArray(JSONArray array) {
JSONObject returnObject = new JSONObject();
public static List<MockConfigRequestParams> getParamsByJSONArray(JSONArray array) {
List<MockConfigRequestParams> requestParamsList = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JSONObject obj = array.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value")) {
returnObject.put(obj.getString("name"), obj.getString("value"));
String condition = null;
if (obj.containsKey("rangeType")) {
condition = obj.getString("rangeType");
}
MockConfigRequestParams requestParams = new MockConfigRequestParams();
requestParams.setKey(obj.getString("name"));
requestParams.setValue(obj.getString("value"));
requestParams.setCondition(parseCondition(condition));
requestParamsList.add(requestParams);
}
}
return returnObject;
return requestParamsList;
}
public static Map<String, String> getApiResponse(String response) {
@ -470,7 +483,7 @@ public class MockApiUtils {
requestMockParams.setRestParamsObj(urlParamsObject);
requestMockParams.setQueryParamsObj(queryParamsObject);
if(isPostRequest){
if (isPostRequest) {
JSONArray jsonArray = new JSONArray();
jsonArray.add(queryParamsObject);
requestMockParams.setBodyParams(jsonArray);
@ -587,9 +600,9 @@ public class MockApiUtils {
if (charEncoding == null) {
charEncoding = "UTF-8";
}
if(buffer == null){
if (buffer == null) {
return "";
}else {
} else {
return new String(buffer, charEncoding);
}
}
@ -645,4 +658,112 @@ public class MockApiUtils {
}
return buffer;
}
public static String parseCondition(String condition) {
String returnCondition = MockParamConditionEnum.VALUE_EQUALS.name();
if (StringUtils.isNotEmpty(condition)) {
switch (condition) {
case "value_not_eq":
returnCondition = MockParamConditionEnum.VALUE_NOT_EQUALS.name();
break;
case "value_contain":
returnCondition = MockParamConditionEnum.VALUE_CONTAINS.name();
break;
case "length_eq":
returnCondition = MockParamConditionEnum.LENGTH_EQUALS.name();
break;
case "length_not_eq":
returnCondition = MockParamConditionEnum.LENGTH_NOT_EQUALS.name();
break;
case "length_large_than":
returnCondition = MockParamConditionEnum.LENGTH_LARGE_THAN.name();
break;
case "length_shot_than":
returnCondition = MockParamConditionEnum.LENGTH_SHOT_THAN.name();
break;
case "regular_match":
returnCondition = MockParamConditionEnum.REGULAR_MATCH.name();
break;
default:
returnCondition = MockParamConditionEnum.VALUE_EQUALS.name();
break;
}
}
return returnCondition;
}
public static boolean checkParamsCompliance(JSONObject queryParamsObj, List<MockConfigRequestParams> mockConfigRequestParamList) {
if (CollectionUtils.isNotEmpty(mockConfigRequestParamList)) {
for (MockConfigRequestParams params : mockConfigRequestParamList) {
String key = params.getKey();
if (queryParamsObj.containsKey(key)) {
boolean isMatch = MockApiUtils.isValueMatch(String.valueOf(queryParamsObj.get(key)), params);
if (!isMatch) {
return false;
}
}
}
}
return true;
}
public static boolean checkParamsCompliance(JSONArray jsonArray, List<MockConfigRequestParams> mockConfigRequestParamList) {
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
boolean isMatch = checkParamsCompliance(obj, mockConfigRequestParamList);
if (isMatch) {
return true;
}
}
return false;
}
private static boolean isValueMatch(String requestParam, MockConfigRequestParams params) {
if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.VALUE_EQUALS.name())) {
return StringUtils.equals(requestParam, params.getValue());
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.VALUE_NOT_EQUALS.name())) {
return !StringUtils.equals(requestParam, params.getValue());
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.VALUE_CONTAINS.name())) {
return StringUtils.contains(requestParam, params.getValue());
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.LENGTH_EQUALS.name())) {
try {
int length = Integer.parseInt(params.getValue());
return requestParam.length() == length;
} catch (Exception e) {
return false;
}
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.LENGTH_NOT_EQUALS.name())) {
try {
int length = Integer.parseInt(params.getValue());
return requestParam.length() == length;
} catch (Exception e) {
return false;
}
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.LENGTH_SHOT_THAN.name())) {
try {
int length = Integer.parseInt(params.getValue());
return requestParam.length() < length;
} catch (Exception e) {
return false;
}
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.LENGTH_LARGE_THAN.name())) {
try {
int length = Integer.parseInt(params.getValue());
return requestParam.length() > length;
} catch (Exception e) {
return false;
}
} else if (StringUtils.equals(params.getCondition(), MockParamConditionEnum.REGULAR_MATCH.name())) {
try {
Pattern pattern = Pattern.compile(params.getValue());
Matcher matcher = pattern.matcher(requestParam);
boolean isMatch = matcher.matches();
return isMatch;
} catch (Exception e) {
return false;
}
} else {
return false;
}
}
}

View File

@ -0,0 +1,53 @@
package io.metersphere.api.mock.utils;
import com.mifmif.common.regex.Generex;
import io.metersphere.api.dto.mock.MockTestDataRequest;
import io.metersphere.api.mock.dto.MockParamConditionEnum;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
public class MockTestDataUtil {
public List<MockTestDataRequest> parseTestDataByRequest(List<MockTestDataRequest> testDataRequestList) {
for (MockTestDataRequest request : testDataRequestList) {
try{
request.setValue(this.getTestData(request));
}catch (Exception e){
LogUtil.error(e);
}
}
return testDataRequestList;
}
public String getTestData(MockTestDataRequest request) {
if (StringUtils.equals(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.VALUE_EQUALS.name())) {
return request.getValue();
} else if (StringUtils.equalsAny(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.VALUE_CONTAINS.name(), MockParamConditionEnum.VALUE_NOT_EQUALS.name())) {
return request.getValue() + "A";
} else if (StringUtils.equals(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.LENGTH_EQUALS.name())) {
int length = Integer.parseInt(request.getValue());
return RandomStringUtils.randomAlphanumeric(length);
} else if (StringUtils.equalsAny(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.LENGTH_NOT_EQUALS.name(), MockParamConditionEnum.LENGTH_LARGE_THAN.name())) {
int length = Integer.parseInt(request.getValue()) + 1;
return RandomStringUtils.randomAlphanumeric(length);
} else if (StringUtils.equals(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.LENGTH_SHOT_THAN.name())) {
int length = Integer.parseInt(request.getValue());
if(length > 1){
return RandomStringUtils.randomAlphanumeric(length);
}else {
return "";
}
} else if (StringUtils.equals(MockApiUtils.parseCondition(request.getCondition()), MockParamConditionEnum.REGULAR_MATCH.name())) {
if (StringUtils.isNotEmpty(request.getValue())) {
Generex generex = new Generex(request.getValue());
String randomStr = generex.random();
return randomStr;
}else {
return "";
}
} else {
return "";
}
}
}

View File

@ -9,7 +9,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
import io.metersphere.api.dto.automation.parse.TcpTreeTableDataParser;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.dto.mock.MockApiUtils;
import io.metersphere.api.dto.mock.MockConfigRequestParams;
import io.metersphere.api.dto.mock.MockParamSuggestions;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.api.dto.mockconfig.MockConfigImportDTO;
@ -17,6 +17,7 @@ import io.metersphere.api.dto.mockconfig.MockConfigRequest;
import io.metersphere.api.dto.mockconfig.MockExpectConfigRequest;
import io.metersphere.api.dto.mockconfig.response.MockConfigResponse;
import io.metersphere.api.dto.mockconfig.response.MockExpectConfigResponse;
import io.metersphere.api.mock.utils.MockApiUtils;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.MockConfigMapper;
import io.metersphere.base.mapper.MockExpectConfigMapper;
@ -330,7 +331,9 @@ public class MockConfigService {
return resultModel;
}
}
return null;
}
for (MockExpectConfigResponse model : mockExpectConfigList) {
try {
if (!model.isStatus()) {
@ -376,94 +379,43 @@ public class MockConfigService {
if (expectParamsObj.containsKey("body")) {
JSONObject expectBodyObject = expectParamsObj.getJSONObject("body");
JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
JSONArray jsonArray = requestMockParams.getBodyParams();
if (mockExpectJsonArray instanceof JSONObject) {
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONObject) mockExpectJsonArray)) {
String type = expectBodyObject.getString("type");
if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.containsKey("kvs")) {
JSONArray kvsArr = expectBodyObject.getJSONArray("kvs");
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr);
if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams)) {
return false;
}
} else if (mockExpectJsonArray instanceof JSONArray) {
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONArray) mockExpectJsonArray)) {
return false;
}else {
JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
if (mockExpectJsonArray instanceof JSONObject) {
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONObject) mockExpectJsonArray)) {
return false;
}
} else if (mockExpectJsonArray instanceof JSONArray) {
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONArray) mockExpectJsonArray)) {
return false;
}
}
}
}
if (expectParamsObj.containsKey("arguments")) {
JSONArray argumentsArray = expectParamsObj.getJSONArray("arguments");
JSONObject urlRequestParamObj = MockApiUtils.getParamsByJSONArray(argumentsArray);
if (!JsonStructUtils.checkJsonObjCompliance(requestMockParams.getQueryParamsObj(), urlRequestParamObj)) {
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(argumentsArray);
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getQueryParamsObj(), mockConfigRequestParams)) {
return false;
}
}
if (expectParamsObj.containsKey("rest")) {
JSONArray restArray = expectParamsObj.getJSONArray("rest");
JSONObject restRequestParamObj = MockApiUtils.getParamsByJSONArray(restArray);
if (!JsonStructUtils.checkJsonObjCompliance(requestMockParams.getRestParamsObj(), restRequestParamObj)) {
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(restArray);
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getRestParamsObj(), mockConfigRequestParams)) {
return false;
}
}
// JSONArray jsonArray = requestMockParams.getBodyParams();
// if (jsonArray == null) {
// //url or get 参数
// JSONArray argumentsArray = expectParamsObj.getJSONArray("arguments");
// JSONArray restArray = expectParamsObj.getJSONArray("rest");
//
// JSONObject urlRequestParamObj = MockApiUtils.getParams(argumentsArray);
// JSONObject restRequestParamObj = MockApiUtils.getParams(restArray);
//
// if (requestMockParams.getQueryParamsObj() == null || requestMockParams.getQueryParamsObj().isEmpty()) {
// return JsonStructUtils.checkJsonObjCompliance(requestMockParams.getRestParamsObj(), restRequestParamObj);
// } else if (requestMockParams.getRestParamsObj() == null || requestMockParams.getRestParamsObj().isEmpty()) {
// return JsonStructUtils.checkJsonObjCompliance(requestMockParams.getQueryParamsObj(), urlRequestParamObj);
// } else {
// return JsonStructUtils.checkJsonObjCompliance(requestMockParams.getQueryParamsObj(), urlRequestParamObj)
// && JsonStructUtils.checkJsonObjCompliance(requestMockParams.getRestParamsObj(), restRequestParamObj);
// }
//
// } else {
// // body参数
// JSONObject expectBodyObject = expectParamsObj.getJSONObject("body");
// JSONArray mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
//
// return JsonStructUtils.checkJsonArrayCompliance(jsonArray, mockExpectJsonArray);
// }
// JSONObject mockExpectJson = new JSONObject();
// if (isJsonParam) {
// String jsonParams = mockExpectRequestObj.getString("jsonData");
// JSONValidator jsonValidator = JSONValidator.from(jsonParams);
// if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) {
// JSONArray mockExpectArr = JSONArray.parseArray(jsonParams);
// for (int expectIndex = 0; expectIndex < mockExpectArr.size(); expectIndex++) {
// JSONObject itemObj = mockExpectArr.getJSONObject(expectIndex);
// mockExpectJson = itemObj;
// }
// } else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) {
// JSONObject mockExpectJsonItem = JSONObject.parseObject(jsonParams);
// mockExpectJson = mockExpectJsonItem;
// }
// } else {
// JSONArray jsonArray = mockExpectRequestObj.getJSONArray("variables");
// for (int i = 0; i < jsonArray.size(); i++) {
// JSONObject object = jsonArray.getJSONObject(i);
// String name = "";
// String value = "";
// if (object.containsKey("name")) {
// name = String.valueOf(object.get("name")).trim();
// }
// if (object.containsKey("value")) {
// value = String.valueOf(object.get("value")).trim();
// }
// if (StringUtils.isNotEmpty(name)) {
// mockExpectJson.put(name, value);
// }
// }
// }
// boolean isMatching = JsonStructUtils.checkJsonObjCompliance(mockExpectRequestObj, mockExpectJson);
return true;
}
@ -1151,10 +1103,9 @@ public class MockConfigService {
String returnStr = "";
boolean isMatch = false;
String url = request.getRequestURL().toString();
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
if (project != null) {
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
JSON paramJson = MockApiUtils.getPostParamMap(request);
JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request);
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {

View File

@ -1,7 +1,7 @@
package io.metersphere.api.tcp.server;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.mock.MockApiUtils;
import io.metersphere.api.mock.utils.MockApiUtils;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
import io.metersphere.commons.utils.CommonBeanFactory;

View File

@ -283,9 +283,9 @@ export default {
if (param.params) {
requestParam = param.params;
}
this.$refs.httpTestPage.setRequestParam(requestParam);
this.$refs.httpTestPage.setRequestParam(requestParam,true);
} else if (this.currentProtocol === "TCP" && this.$refs.tcpTestPage) {
this.$refs.tcpTestPage.setRequestParam(param);
this.$refs.tcpTestPage.setRequestParam(param,true);
}
});
},

View File

@ -510,7 +510,7 @@ export default {
let protocol = document.location.protocol;
protocol = protocol.substring(0, protocol.indexOf(":"));
let url = "/api/definition/getMockEnvironment/";
this.$get(url + this.projectId + "/" + protocol, response => {
this.$get(url + this.projectId, response => {
this.mockEnvironment = response.data;
let httpConfig = JSON.parse(this.mockEnvironment.config);
if (httpConfig != null) {

View File

@ -104,6 +104,9 @@
}
this.$refs.environmentConfig.open(this.projectId);
},
setEnvironment(enviromentId){
this.currentData.environmentId = enviromentId;
}
}
}
</script>

View File

@ -21,7 +21,6 @@
</el-select>
</template>
</el-input>
<el-autocomplete :disabled="isReadOnly" v-if="suggestions" v-model="item.name" size="small"
:fetch-suggestions="querySearch" @change="change" :placeholder="keyText" show-word-limit>
<template v-slot:prepend>
@ -37,18 +36,18 @@
</el-col>
<el-col class="item" v-if="isActive && item.type !== 'file'">
<el-autocomplete
:disabled="isReadOnly"
size="small"
class="input-with-autocomplete"
v-model="item.value"
:fetch-suggestions="funcSearch"
:placeholder="valueText"
value-key="name"
highlight-first-item
@select="change">
<i slot="suffix" class="el-input__icon el-icon-edit pointer" @click="advanced(item)"></i>
</el-autocomplete>
<el-select :disabled="isReadOnly" :maxlength="100" v-model="item.rangeType" size="small" style="width: 100%">
<el-option
v-for="item in rangeTypeOptions"
:key="item.value"
:label="item.label"
:value="item.value"></el-option>
</el-select>
</el-col>
<el-col class="item" v-if="isActive && item.type !== 'file'">
<el-input-number v-if="item.rangeType === 'length_eq' || item.rangeType === 'length_not_eq' || item.rangeType === 'length_large_than' || item.rangeType === 'length_shot_than'"
v-model="item.value" size="small" :placeholder="valueText" show-word-limit />
<el-input v-else v-model="item.value" size="small" :placeholder="valueText" show-word-limit />
</el-col>
<el-col class="item">
@ -81,13 +80,13 @@
<ms-api-variable-json :append-to-body="appendDialogToBody" ref="variableJson" @callback="callback"/>
<api-variable-setting :append-to-body="appendDialogToBody"
ref="apiVariableSetting"/>
ref="apiVariableSetting"/>
</div>
</template>
<script>
import {KeyValue,Scenario} from "@/business/components/api/definition/model/ApiTestModel";
import {KeyValue, Scenario} from "@/business/components/api/definition/model/ApiTestModel";
import {JMETER_FUNC, MOCKJS_FUNC} from "@/common/js/constants";
import MsApiVariableAdvance from "@/business/components/api/definition/components/ApiVariableAdvance";
import MsApiVariableJson from "@/business/components/api/definition/components/ApiVariableJson";
@ -136,6 +135,44 @@ export default {
],
isSelectAll: true,
isActive: true,
rangeTypeOptions: [
{
value: "",
label: this.$t("commons.please_select"),
},
{
value: "value_eq",
label: this.$t("api_test.mock.range_type.value_eq"),
},
{
value: "value_not_eq",
label: this.$t("api_test.mock.range_type.value_not_eq"),
},
{
value: "value_contain",
label: this.$t("api_test.mock.range_type.value_contain"),
},
{
value: "length_eq",
label: this.$t("api_test.mock.range_type.length_eq"),
},
{
value: "length_not_eq",
label: this.$t("api_test.mock.range_type.length_not_eq"),
},
{
value: "length_large_than",
label: this.$t("api_test.mock.range_type.length_large_than"),
},
{
value: "length_shot_than",
label: this.$t("api_test.mock.range_type.length_shot_than"),
},
{
value: "regular_match",
label: this.$t("api_test.mock.range_type.regular_match"),
},
],
}
},
watch: {

View File

@ -53,7 +53,8 @@ export default {
headerSuggestions: Array
},
data() {
return {};
return {
};
},
methods: {
remove: function (index) {

View File

@ -153,9 +153,67 @@ export default {
redirectToTest(row) {
let requestParam = null;
if (row && row.request) {
requestParam = row.request;
requestParam = JSON.parse(JSON.stringify(row.request));
}
if(requestParam.params){
let selectParma = [];
if(requestParam.params.arguments && requestParam.params.arguments.length > 0){
requestParam.params.arguments.forEach(item => {
if(item.rangeType && item.value &&item.uuid){
let paramObj = {id:item.uuid,value:item.value,condition:item.rangeType};
selectParma.push(paramObj);
}
});
}
if(requestParam.params.rest && requestParam.params.rest.length > 0){
requestParam.params.rest.forEach(item => {
if(item.rangeType && item.value &&item.uuid){
let paramObj = {id:item.uuid,value:item.value,condition:item.rangeType};
selectParma.push(paramObj);
}
});
}
if(requestParam.params.body.kvs && requestParam.params.body.kvs.length > 0){
requestParam.params.body.kvs.forEach(item => {
if(item.rangeType && item.value &&item.uuid){
let paramObj = {id:item.uuid,value:item.value,condition:item.rangeType};
selectParma.push(paramObj);
}
});
}
//mock
this.$post("/mockConfig/getMockTestData", selectParma, response => {
let returnData = response.data;
if(returnData && returnData.length > 0){
returnData.forEach(data => {
if(requestParam.params.arguments && requestParam.params.arguments.length > 0){
for(let i = 0; i < requestParam.params.arguments.length; i++){
if(requestParam.params.arguments[i].uuid === data.id){
requestParam.params.arguments[i].value = data.value;
}
}
}
if(requestParam.params.rest && requestParam.params.rest.length > 0){
for(let i = 0; i < requestParam.params.rest.length; i++){
if(requestParam.params.rest[i].uuid === data.id){
requestParam.params.rest[i].value = data.value;
}
}
}
if(requestParam.params.body.kvs && requestParam.params.body.kvs.length > 0){
for(let i = 0; i < requestParam.params.body.kvs.length; i++){
if(requestParam.params.body.kvs[i].uuid === data.id){
requestParam.params.body.kvs[i].value = data.value;
}
}
}
});
}
this.$emit("redirectToTest", requestParam);
}, error => {
this.$emit("redirectToTest", requestParam);
});
}
this.$emit("redirectToTest", requestParam);
},
searchApiParams(apiId) {
let selectUrl = "/mockConfig/getApiParams/" + apiId;

View File

@ -18,7 +18,7 @@
<!-- 执行环境 -->
<el-form-item prop="environmentId">
<environment-select :current-data="api" :project-id="projectId"/>
<environment-select :current-data="api" :project-id="projectId" ref="environmentSelect"/>
</el-form-item>
<!-- 请求地址 -->
@ -140,22 +140,33 @@ export default {
}
},
methods: {
setRequestParam(param) {
setRequestParam(param, isEnvironmentMock) {
this.init();
if (param) {
if(param.headers){
if (param.headers) {
this.api.request.headers = param.headers;
}
if(param.argument){
this.api.request.argument = param.argument;
if (param.arguments!==null && param.arguments.length > 0) {
this.api.request.arguments = param.arguments;
}
if(param.body){
if (param.body) {
this.api.request.body = param.body;
}
if(param.rest){
if (param.rest) {
this.api.request.rest = param.rest;
}
}
if (isEnvironmentMock) {
this.$nextTick(() => {
let url = "/api/definition/getMockEnvironment/";
this.$get(url + this.projectId, response => {
let mockEnvironment = response.data;
if (mockEnvironment !== null) {
this.$refs.environmentSelect.setEnvironment(mockEnvironment.id);
}
});
})
}
},
handleCommand(e) {
switch (e) {

View File

@ -29,7 +29,7 @@
<!-- 执行环境 -->
<el-form-item prop="environmentId">
{{ $t('api_test.definition.request.run_env') }}
<environment-select :type="'TCP'" :current-data="api" :project-id="projectId"/>
<environment-select :type="'TCP'" :current-data="api" :project-id="projectId" ref="environmentSelect"/>
</el-form-item>
@ -128,7 +128,7 @@ export default {
},
props: {apiData: {}, currentProtocol: String, syncTabs: Array, projectId: String},
methods: {
setRequestParam(param) {
setRequestParam(param, isEnvironmentMock) {
this.init();
if (this.api.method === "TCP" && param && this.api.request) {
if (param.reportType) {
@ -148,6 +148,17 @@ export default {
this.$refs.requestForm.setReportType(param.reportType);
});
}
if (isEnvironmentMock) {
this.$nextTick(() => {
let url = "/api/definition/getMockEnvironment/";
this.$get(url + this.projectId, response => {
let mockEnvironment = response.data;
if (mockEnvironment !== null) {
this.$refs.environmentSelect.setEnvironment(mockEnvironment.id);
}
});
})
}
},
handleCommand(e) {
switch (e) {

View File

@ -1133,6 +1133,16 @@ export default {
delete_mock_expect: "Confirm to delete this expect info ?",
rule: {
input_code: "Please input HTTP Code"
},
range_type:{
value_eq: "value=",
value_not_eq: "value!=",
value_contain:"include=",
length_eq: "length=",
length_not_eq: "length!=",
length_large_than:"length>",
length_shot_than:"length<",
regular_match: "Regular match",
}
},
definition: {

View File

@ -1139,6 +1139,16 @@ export default {
delete_mock_expect: "确认删除这条预期吗?",
rule: {
input_code: "请输入 HTTP Code"
},
range_type:{
value_eq: "值-等于[value=]",
value_not_eq: "值-不等于[value!=]",
value_contain:"值-包含[include=]",
length_eq: "长度-等于[length=]",
length_not_eq: "长度-不等于[length!=]",
length_large_than:"长度-大于[length>]",
length_shot_than:"长度-小于[length<]",
regular_match: "正则匹配",
}
},
definition: {

View File

@ -1139,6 +1139,16 @@ export default {
delete_mock_expect: "確認刪除這條預期嗎?",
rule: {
input_code: "請輸入 HTTP Code"
},
range_type:{
value_eq: "值-等於[value=]",
value_not_eq: "值-不等於[value!=]",
value_contain:"值-包含[include=]",
length_eq: "長度-等于[length=]",
length_not_eq: "長度-不等於[length!=]",
length_large_than:"長度-大於[length>]",
length_shot_than:"長度-小於[length<]",
regular_match: "正則匹配",
}
},
definition: {