Merge branch 'main' of https://github.com/metersphere/metersphere
This commit is contained in:
commit
6fe7247777
|
@ -2,7 +2,7 @@ package io.metersphere.api.dto.mock;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.json.JSONArray;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -12,30 +12,31 @@ import org.json.JSONObject;
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
public class RequestMockParams {
|
public class RequestMockParams {
|
||||||
|
private boolean isPost;
|
||||||
|
private String paramType;
|
||||||
private JSONObject restParamsObj;
|
private JSONObject restParamsObj;
|
||||||
private JSONObject queryParamsObj;
|
|
||||||
private JSONArray bodyParams;
|
|
||||||
|
|
||||||
public JSONObject getParamsObj() {
|
//form-data的kv类型参数也存储在queryParamObj中
|
||||||
JSONObject returnObj = new JSONObject();
|
private JSONObject queryParamsObj;
|
||||||
if (restParamsObj != null) {
|
|
||||||
for (String key : restParamsObj.keySet()) {
|
//JSONArray 或 JSONObject
|
||||||
Object value = restParamsObj.get(key);
|
private Object jsonParam;
|
||||||
returnObj.put(key, value);
|
|
||||||
}
|
private JSONObject xmlToJsonParam;
|
||||||
}
|
|
||||||
if (queryParamsObj != null) {
|
|
||||||
for (String key : queryParamsObj.keySet()) {
|
private String raw;
|
||||||
Object value = queryParamsObj.get(key);
|
|
||||||
returnObj.put(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return (restParamsObj == null || restParamsObj == null) &&
|
if (isPost) {
|
||||||
(queryParamsObj == null || queryParamsObj == null) &&
|
return (restParamsObj == null || restParamsObj.isEmpty()) &&
|
||||||
(bodyParams == null || bodyParams == null);
|
(queryParamsObj == null || queryParamsObj.isEmpty())
|
||||||
|
&& StringUtils.isBlank(raw);
|
||||||
|
} else {
|
||||||
|
return (restParamsObj == null || restParamsObj.isEmpty()) &&
|
||||||
|
(queryParamsObj == null || queryParamsObj.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package io.metersphere.commons.enums;
|
||||||
|
|
||||||
|
public enum MockRequestType {
|
||||||
|
JSON, KV, XML, RAW
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import io.metersphere.api.exec.generator.JSONSchemaGenerator;
|
||||||
import io.metersphere.commons.constants.ElementConstants;
|
import io.metersphere.commons.constants.ElementConstants;
|
||||||
import io.metersphere.commons.constants.PropertyConstant;
|
import io.metersphere.commons.constants.PropertyConstant;
|
||||||
import io.metersphere.commons.enums.MockParamConditionEnums;
|
import io.metersphere.commons.enums.MockParamConditionEnums;
|
||||||
|
import io.metersphere.commons.enums.MockRequestType;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
import io.metersphere.commons.utils.*;
|
import io.metersphere.commons.utils.*;
|
||||||
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
import io.metersphere.jmeter.utils.ScriptEngineUtils;
|
||||||
|
@ -288,13 +289,7 @@ public class MockApiUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (headerMap == null) {
|
if (bodyObj == null || bodyObj.isEmpty()) {
|
||||||
headerMap = new HashMap<>();
|
|
||||||
}
|
|
||||||
if (requestMockParams == null) {
|
|
||||||
requestMockParams = new RequestMockParams();
|
|
||||||
}
|
|
||||||
if (bodyObj == null && bodyObj == null) {
|
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
} else {
|
} else {
|
||||||
String returnStr = StringUtils.EMPTY;
|
String returnStr = StringUtils.EMPTY;
|
||||||
|
@ -321,8 +316,7 @@ public class MockApiUtils {
|
||||||
}
|
}
|
||||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "Raw")) {
|
} else if (StringUtils.equalsAnyIgnoreCase(type, "Raw")) {
|
||||||
if (bodyObj.has("raw")) {
|
if (bodyObj.has("raw")) {
|
||||||
String raw = bodyObj.optString("raw");
|
returnStr = bodyObj.optString("raw");
|
||||||
returnStr = raw;
|
|
||||||
}
|
}
|
||||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "XML")) {
|
} else if (StringUtils.equalsAnyIgnoreCase(type, "XML")) {
|
||||||
if (bodyObj.has("xmlHeader")) {
|
if (bodyObj.has("xmlHeader")) {
|
||||||
|
@ -339,8 +333,7 @@ public class MockApiUtils {
|
||||||
}
|
}
|
||||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "fromApi")) {
|
} else if (StringUtils.equalsAnyIgnoreCase(type, "fromApi")) {
|
||||||
if (bodyObj.has("apiRspRaw")) {
|
if (bodyObj.has("apiRspRaw")) {
|
||||||
String raw = bodyObj.optString("apiRspRaw");
|
returnStr = bodyObj.optString("apiRspRaw");
|
||||||
returnStr = raw;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -351,67 +344,21 @@ public class MockApiUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject, Object paramJson, boolean isPostRequest) {
|
public static void complementRestParam(String urlParams, String apiPath, RequestMockParams requestMockParams) {
|
||||||
RequestMockParams returnParams = getGetParamMap(urlParams, apiPath, queryParamsObject, isPostRequest);
|
|
||||||
if (paramJson != null) {
|
|
||||||
if (paramJson instanceof JSONObject) {
|
|
||||||
if (!((JSONObject) paramJson).keySet().isEmpty()) {
|
|
||||||
JSONArray bodyParams = returnParams.getBodyParams();
|
|
||||||
if (bodyParams == null) {
|
|
||||||
bodyParams = new JSONArray();
|
|
||||||
bodyParams.put(paramJson);
|
|
||||||
} else {
|
|
||||||
JSONArray oldArray = returnParams.getBodyParams();
|
|
||||||
if (!JsonStructUtils.checkJsonArrayCompliance(oldArray, ((JSONObject) paramJson))) {
|
|
||||||
bodyParams.put(((JSONObject) paramJson));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
returnParams.setBodyParams(bodyParams);
|
|
||||||
}
|
|
||||||
} else if (paramJson instanceof JSONArray) {
|
|
||||||
JSONArray paramArray = (JSONArray) paramJson;
|
|
||||||
if (paramArray != null) {
|
|
||||||
returnParams.setBodyParams(paramArray);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return returnParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JSONObject getParameterJsonObject(HttpServletRequest request) {
|
|
||||||
JSONObject queryParamsObject = new JSONObject();
|
|
||||||
Enumeration<String> paramNameItor = request.getParameterNames();
|
|
||||||
while (paramNameItor.hasMoreElements()) {
|
|
||||||
String key = paramNameItor.nextElement();
|
|
||||||
String value = request.getParameter(key);
|
|
||||||
queryParamsObject.put(key, value);
|
|
||||||
}
|
|
||||||
return queryParamsObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static RequestMockParams getGetParamMap(String urlParams, String apiPath, JSONObject queryParamsObject, boolean isPostRequest) {
|
|
||||||
RequestMockParams requestMockParams = new RequestMockParams();
|
|
||||||
|
|
||||||
JSONObject urlParamsObject = getSendRestParamMapByIdAndUrl(apiPath, urlParams);
|
JSONObject urlParamsObject = getSendRestParamMapByIdAndUrl(apiPath, urlParams);
|
||||||
|
|
||||||
requestMockParams.setRestParamsObj(urlParamsObject);
|
requestMockParams.setRestParamsObj(urlParamsObject);
|
||||||
requestMockParams.setQueryParamsObj(queryParamsObject);
|
|
||||||
|
|
||||||
if (isPostRequest && !queryParamsObject.keySet().isEmpty()) {
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
|
||||||
if (queryParamsObject.length() != 0) {
|
|
||||||
jsonArray.put(queryParamsObject);
|
|
||||||
}
|
|
||||||
requestMockParams.setBodyParams(jsonArray);
|
|
||||||
}
|
|
||||||
return requestMockParams;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object getPostParamMap(HttpServletRequest request) {
|
public static RequestMockParams genRequestMockParamsFromHttpRequest(HttpServletRequest request, boolean isPost) {
|
||||||
|
RequestMockParams mockParams = new RequestMockParams();
|
||||||
|
mockParams.setPost(isPost);
|
||||||
|
|
||||||
if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/JSON")) {
|
if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/JSON")) {
|
||||||
|
mockParams.setParamType(MockRequestType.JSON.name());
|
||||||
Object returnJson = null;
|
Object returnJson = null;
|
||||||
try {
|
try {
|
||||||
String param = getRequestPostStr(request);
|
String param = getRequestPostStr(request);
|
||||||
|
mockParams.setRaw(param);
|
||||||
if (StringUtils.isNotEmpty(param)) {
|
if (StringUtils.isNotEmpty(param)) {
|
||||||
JSONValidator jsonValidator = JSONValidator.from(param);
|
JSONValidator jsonValidator = JSONValidator.from(param);
|
||||||
if (StringUtils.equalsIgnoreCase(PropertyConstant.ARRAY, jsonValidator.getType().name())) {
|
if (StringUtils.equalsIgnoreCase(PropertyConstant.ARRAY, jsonValidator.getType().name())) {
|
||||||
|
@ -420,39 +367,52 @@ public class MockApiUtils {
|
||||||
returnJson = JSONUtil.parseObject(param);
|
returnJson = JSONUtil.parseObject(param);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mockParams.setJsonParam(returnJson);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
return returnJson;
|
|
||||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/xml")) {
|
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/xml")) {
|
||||||
|
mockParams.setParamType(MockRequestType.XML.name());
|
||||||
String xmlString = readXml(request);
|
String xmlString = readXml(request);
|
||||||
JSONObject object = XMLUtil.xmlStringToJSONObject(xmlString);
|
JSONObject xmlJsonObject = XMLUtil.xmlStringToJSONObject(xmlString);
|
||||||
return object;
|
mockParams.setXmlToJsonParam(xmlJsonObject);
|
||||||
|
mockParams.setRaw(xmlString);
|
||||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/x-www-form-urlencoded")) {
|
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/x-www-form-urlencoded")) {
|
||||||
|
mockParams.setParamType(MockRequestType.KV.name());
|
||||||
JSONObject object = new JSONObject();
|
JSONObject object = new JSONObject();
|
||||||
Enumeration<String> paramNameItor = request.getParameterNames();
|
Enumeration<String> paramNameItr = request.getParameterNames();
|
||||||
while (paramNameItor.hasMoreElements()) {
|
while (paramNameItr.hasMoreElements()) {
|
||||||
String key = paramNameItor.nextElement();
|
String key = paramNameItr.nextElement();
|
||||||
String value = request.getParameter(key);
|
String value = request.getParameter(key);
|
||||||
object.put(key, value);
|
object.put(key, value);
|
||||||
}
|
}
|
||||||
return object;
|
mockParams.setQueryParamsObj(object);
|
||||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/plain")) {
|
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/plain")) {
|
||||||
JSONObject object = new JSONObject();
|
mockParams.setParamType(MockRequestType.RAW.name());
|
||||||
String bodyParam = readBody(request);
|
String bodyParam = readBody(request);
|
||||||
if (StringUtils.isNotEmpty(bodyParam)) {
|
if (StringUtils.isNotEmpty(bodyParam)) {
|
||||||
object.put("raw", bodyParam);
|
mockParams.setRaw(bodyParam);
|
||||||
|
}
|
||||||
|
} else if (isPost) {
|
||||||
|
String bodyParam = readBody(request);
|
||||||
|
if (StringUtils.isNotEmpty(bodyParam)) {
|
||||||
|
mockParams.setParamType(MockRequestType.RAW.name());
|
||||||
|
mockParams.setRaw(bodyParam);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return object;
|
|
||||||
|
|
||||||
} else {
|
if (!StringUtils.equals(mockParams.getParamType(), MockRequestType.KV.name())) {
|
||||||
|
//非kv类型的请求要检查一下是否带有其它kv参数
|
||||||
JSONObject object = new JSONObject();
|
JSONObject object = new JSONObject();
|
||||||
String bodyParam = readBody(request);
|
Enumeration<String> paramNameItr = request.getParameterNames();
|
||||||
if (StringUtils.isNotEmpty(bodyParam)) {
|
while (paramNameItr.hasMoreElements()) {
|
||||||
object.put("raw", bodyParam);
|
String key = paramNameItr.nextElement();
|
||||||
|
String value = request.getParameter(key);
|
||||||
|
object.put(key, value);
|
||||||
}
|
}
|
||||||
return object;
|
mockParams.setQueryParamsObj(object);
|
||||||
}
|
}
|
||||||
|
return mockParams;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static JSONObject getSendRestParamMapByIdAndUrl(String path, String urlParams) {
|
private static JSONObject getSendRestParamMapByIdAndUrl(String path, String urlParams) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package io.metersphere.commons.utils.mock;
|
||||||
|
|
||||||
import io.metersphere.api.dto.mock.RequestMockParams;
|
import io.metersphere.api.dto.mock.RequestMockParams;
|
||||||
import io.metersphere.commons.constants.ElementConstants;
|
import io.metersphere.commons.constants.ElementConstants;
|
||||||
|
import io.metersphere.commons.enums.MockRequestType;
|
||||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
import io.metersphere.commons.utils.JSON;
|
import io.metersphere.commons.utils.JSON;
|
||||||
import io.metersphere.commons.utils.JSONUtil;
|
import io.metersphere.commons.utils.JSONUtil;
|
||||||
|
@ -15,9 +16,6 @@ import org.json.JSONObject;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
import javax.script.ScriptEngineManager;
|
import javax.script.ScriptEngineManager;
|
||||||
import java.io.File;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -48,24 +46,6 @@ public class MockScriptEngineUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadJar(String jarPath) throws Exception {
|
|
||||||
try {
|
|
||||||
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
|
||||||
try {
|
|
||||||
Method method = classLoader.getClass().getDeclaredMethod("addURL", URL.class);
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(classLoader, new File(jarPath).toURI().toURL());
|
|
||||||
} catch (NoSuchMethodException e) {
|
|
||||||
Method method = classLoader.getClass()
|
|
||||||
.getDeclaredMethod("appendToClassPathForInstrumentation", String.class);
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(classLoader, jarPath);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogUtil.error(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载jar包
|
* 加载jar包
|
||||||
*/
|
*/
|
||||||
|
@ -96,9 +76,12 @@ public class MockScriptEngineUtils {
|
||||||
engine = scriptEngineFactory.getEngineByName(scriptLanguage);
|
engine = scriptEngineFactory.getEngineByName(scriptLanguage);
|
||||||
preScript = this.genPythonPreScript(url, headerMap, requestMockParams);
|
preScript = this.genPythonPreScript(url, headerMap, requestMockParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (engine != null) {
|
||||||
MsDynamicClassLoader loader = MsClassLoader.loadJar(getJarPaths(projectId));
|
MsDynamicClassLoader loader = MsClassLoader.loadJar(getJarPaths(projectId));
|
||||||
Thread.currentThread().setContextClassLoader(loader);
|
Thread.currentThread().setContextClassLoader(loader);
|
||||||
engine.eval(preScript);
|
engine.eval(preScript);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
|
@ -106,9 +89,9 @@ public class MockScriptEngineUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String genBeanshellPreScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
private String genBeanshellPreScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
||||||
StringBuffer preScriptBuffer = new StringBuffer();
|
StringBuilder preScriptBuffer = new StringBuilder();
|
||||||
preScriptBuffer.append("Map vars = new HashMap();\n");
|
preScriptBuffer.append("Map vars = new HashMap();\n");
|
||||||
preScriptBuffer.append("vars.put(\"address\",\"" + url + "\");\n");
|
preScriptBuffer.append("vars.put(\"address\",\"").append(url).append("\");\n");
|
||||||
//写入请求头
|
//写入请求头
|
||||||
if (headerMap != null) {
|
if (headerMap != null) {
|
||||||
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
|
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
|
||||||
|
@ -116,48 +99,53 @@ public class MockScriptEngineUtils {
|
||||||
String headerValue = headEntry.getValue();
|
String headerValue = headEntry.getValue();
|
||||||
headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\"");
|
headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\"");
|
headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n");
|
preScriptBuffer.append("vars.put(\"header.").append(headerKey).append("\",\"").append(headerValue).append("\");\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//写入body参数
|
|
||||||
if (requestMockParams != null) {
|
if (requestMockParams != null) {
|
||||||
if (requestMockParams.getBodyParams() != null) {
|
//写入body参数
|
||||||
if (requestMockParams.getBodyParams().length() == 1) {
|
if (requestMockParams.isPost()) {
|
||||||
//参数是jsonObject
|
|
||||||
JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0);
|
|
||||||
for (String key : bodyParamObj.keySet()) {
|
|
||||||
String value = String.valueOf(bodyParamObj.get(key));
|
|
||||||
value = StringUtils.replace(value, "\\", "\\\\");
|
|
||||||
value = StringUtils.replace(value, "\"", "\\\"");
|
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars.put(\"body." + key + "\",\"" + value + "\");\n");
|
|
||||||
if (StringUtils.equalsIgnoreCase(key, "raw")) {
|
|
||||||
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + value + "\");\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String jsonBody = bodyParamObj.toString();
|
|
||||||
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
|
|
||||||
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars.put(\"body.json\",\"" + jsonBody + "\");\n");
|
|
||||||
} else {
|
|
||||||
String bodyRowString = requestMockParams.getBodyParams().toString();
|
|
||||||
bodyRowString = StringUtils.replace(bodyRowString, "\\", "\\\\").replace("\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars.put(\"bodyRaw\",\"" + bodyRowString + "\");\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//写入query参数
|
|
||||||
if (requestMockParams.getQueryParamsObj() != null) {
|
if (requestMockParams.getQueryParamsObj() != null) {
|
||||||
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
||||||
for (String key : queryParamsObj.keySet()) {
|
for (String key : queryParamsObj.keySet()) {
|
||||||
String value = String.valueOf(queryParamsObj.get(key));
|
String value = String.valueOf(queryParamsObj.get(key));
|
||||||
value = StringUtils.replace(value, "\\", "\\\\");
|
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
value = StringUtils.replace(value, "\"", "\\\"");
|
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars.put(\"query." + key + "\",\"" + value + "\");\n");
|
preScriptBuffer.append("vars.put(\"body.").append(key).append("\",\"").append(value).append("\");\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.JSON.name())) {
|
||||||
|
String jsonBody = requestMockParams.getRaw();
|
||||||
|
jsonBody = StringUtils.replace(jsonBody, "\n", "");
|
||||||
|
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
|
||||||
|
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars.put(\"body.json\",\"").append(jsonBody).append("\");\n");
|
||||||
|
} else if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.XML.name())) {
|
||||||
|
String xmlRaw = requestMockParams.getRaw();
|
||||||
|
xmlRaw = StringUtils.chomp(xmlRaw);
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\n", "");
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\\", "\\\\");
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars.put(\"body.xml\",\"").append(xmlRaw).append("\");\n");
|
||||||
|
} else if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.RAW.name())) {
|
||||||
|
String bodyRowString = requestMockParams.getRaw();
|
||||||
|
bodyRowString = StringUtils.replace(bodyRowString, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars.put(\"bodyRaw\",\"").append(bodyRowString).append("\");\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//写入query参数
|
||||||
|
if (!requestMockParams.isPost() && requestMockParams.getQueryParamsObj() != null) {
|
||||||
|
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
||||||
|
for (String key : queryParamsObj.keySet()) {
|
||||||
|
String value = String.valueOf(queryParamsObj.get(key));
|
||||||
|
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars.put(\"query.").append(key).append("\",\"").append(value).append("\");\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//写入rest参数
|
//写入rest参数
|
||||||
if (requestMockParams.getRestParamsObj() != null) {
|
if (requestMockParams.getRestParamsObj() != null) {
|
||||||
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
|
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
|
||||||
|
@ -166,7 +154,7 @@ public class MockScriptEngineUtils {
|
||||||
key = StringUtils.replace(key, "\"", "\\\"");
|
key = StringUtils.replace(key, "\"", "\\\"");
|
||||||
value = StringUtils.replace(value, "\"", "\\\"");
|
value = StringUtils.replace(value, "\"", "\\\"");
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars.put(\"rest." + key + "\",\"" + value + "\");\n");
|
preScriptBuffer.append("vars.put(\"rest.").append(key).append("\",\"").append(value).append("\");\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,53 +162,61 @@ public class MockScriptEngineUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String genPythonPreScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
private String genPythonPreScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
||||||
StringBuffer preScriptBuffer = new StringBuffer();
|
StringBuilder preScriptBuffer = new StringBuilder();
|
||||||
preScriptBuffer.append("vars = {}; \n");
|
preScriptBuffer.append("vars = {}; \n");
|
||||||
preScriptBuffer.append("vars[\"address\"]=\"" + url + "\";\n");
|
preScriptBuffer.append("vars[\"address\"]=\"").append(url).append("\";\n");
|
||||||
//写入请求头
|
//写入请求头
|
||||||
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
|
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
|
||||||
String headerKey = headEntry.getKey();
|
String headerKey = headEntry.getKey();
|
||||||
String headerValue = headEntry.getValue();
|
String headerValue = headEntry.getValue();
|
||||||
headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\"");
|
headerKey = StringUtils.replace(headerKey, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\"");
|
headerValue = StringUtils.replace(headerValue, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars[\"header." + headerKey + "\"]=\"" + headerValue + "\";\n");
|
preScriptBuffer.append("vars[\"header.").append(headerKey).append("\"]=\"").append(headerValue).append("\";\n");
|
||||||
}
|
}
|
||||||
|
if (requestMockParams != null) {
|
||||||
//写入body参数
|
//写入body参数
|
||||||
if (requestMockParams.getBodyParams() != null) {
|
if (requestMockParams.isPost()) {
|
||||||
if (requestMockParams.getBodyParams().length() == 1) {
|
|
||||||
//参数是jsonObject
|
|
||||||
JSONObject bodyParamObj = requestMockParams.getBodyParams().optJSONObject(0);
|
|
||||||
for (String key : bodyParamObj.keySet()) {
|
|
||||||
String value = String.valueOf(bodyParamObj.get(key));
|
|
||||||
value = StringUtils.replace(value, "\\", "\\\\");
|
|
||||||
value = StringUtils.replace(value, "\"", "\\\"");
|
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars[\"body." + key + "\"]=\"" + value + "\";\n");
|
|
||||||
if (StringUtils.equalsIgnoreCase(key, "raw")) {
|
|
||||||
preScriptBuffer.append("vars[\"bodyRaw\"]=\"" + value + "\";\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String jsonBody = bodyParamObj.toString();
|
|
||||||
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
|
|
||||||
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars[\"body.json\"]=\"" + jsonBody + "\";\n");
|
|
||||||
} else {
|
|
||||||
String bodyRaw = StringUtils.replace(requestMockParams.getBodyParams().toString(), "\\", "\\\\").replace("\"", "\\\"");
|
|
||||||
preScriptBuffer.append("vars[\"bodyRaw\"]=\"" + bodyRaw + "\";\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//写入query参数
|
|
||||||
if (requestMockParams.getQueryParamsObj() != null) {
|
if (requestMockParams.getQueryParamsObj() != null) {
|
||||||
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
||||||
for (String key : queryParamsObj.keySet()) {
|
for (String key : queryParamsObj.keySet()) {
|
||||||
String value = String.valueOf(queryParamsObj.get(key));
|
String value = String.valueOf(queryParamsObj.get(key));
|
||||||
value = StringUtils.replace(value, "\\", "\\\\");
|
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
value = StringUtils.replace(value, "\"", "\\\"");
|
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars[\"query." + key + "\"]=\"" + value + "\";\n");
|
preScriptBuffer.append("vars[\"body.").append(key).append("\"]=\"").append(value).append("\";\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.JSON.name())) {
|
||||||
|
String jsonRaw = requestMockParams.getRaw();
|
||||||
|
jsonRaw = StringUtils.chomp(jsonRaw);
|
||||||
|
jsonRaw = StringUtils.replace(jsonRaw, "\n", "");
|
||||||
|
jsonRaw = StringUtils.replace(jsonRaw, "\\", "\\\\");
|
||||||
|
jsonRaw = StringUtils.replace(jsonRaw, "\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars[\"body.json\"]=\"").append(jsonRaw).append("\";\n");
|
||||||
|
} else if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.XML.name())) {
|
||||||
|
String xmlRaw = requestMockParams.getRaw();
|
||||||
|
xmlRaw = StringUtils.chomp(xmlRaw);
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\n", "");
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\\", "\\\\");
|
||||||
|
xmlRaw = StringUtils.replace(xmlRaw, "\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars[\"body.xml\"]=\"").append(xmlRaw).append("\";\n");
|
||||||
|
} else if (StringUtils.equals(requestMockParams.getParamType(), MockRequestType.RAW.name())) {
|
||||||
|
String bodyRowString = requestMockParams.getRaw();
|
||||||
|
bodyRowString = StringUtils.replace(bodyRowString, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars[\"bodyRaw\"]=\"").append(bodyRowString).append("\";\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//写入query参数
|
||||||
|
if (!requestMockParams.isPost() && requestMockParams.getQueryParamsObj() != null) {
|
||||||
|
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
||||||
|
for (String key : queryParamsObj.keySet()) {
|
||||||
|
String value = String.valueOf(queryParamsObj.get(key));
|
||||||
|
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
preScriptBuffer.append("vars[\"query.").append(key).append("\"]=\"").append(value).append("\";\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//写入rest参数
|
//写入rest参数
|
||||||
if (requestMockParams.getRestParamsObj() != null) {
|
if (requestMockParams.getRestParamsObj() != null) {
|
||||||
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
|
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
|
||||||
|
@ -228,7 +224,8 @@ public class MockScriptEngineUtils {
|
||||||
String value = String.valueOf(restParamsObj.get(key));
|
String value = String.valueOf(restParamsObj.get(key));
|
||||||
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
key = StringUtils.replace(key, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
value = StringUtils.replace(value, "\\", "\\\\").replace("\"", "\\\"");
|
||||||
preScriptBuffer.append("vars[\"rest." + key + "\"]=\"" + value + "\";\n");
|
preScriptBuffer.append("vars[\"rest.").append(key).append("\"]=\"").append(value).append("\";\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return preScriptBuffer.toString();
|
return preScriptBuffer.toString();
|
||||||
|
|
|
@ -8,15 +8,13 @@ import io.metersphere.api.dto.mock.config.MockConfigRequest;
|
||||||
import io.metersphere.api.dto.mock.config.MockExpectConfigRequest;
|
import io.metersphere.api.dto.mock.config.MockExpectConfigRequest;
|
||||||
import io.metersphere.api.dto.mock.config.response.MockConfigResponse;
|
import io.metersphere.api.dto.mock.config.response.MockConfigResponse;
|
||||||
import io.metersphere.api.dto.mock.config.response.MockExpectConfigResponse;
|
import io.metersphere.api.dto.mock.config.response.MockExpectConfigResponse;
|
||||||
import io.metersphere.commons.constants.NoticeConstants;
|
|
||||||
import io.metersphere.commons.utils.mock.MockApiUtils;
|
|
||||||
import io.metersphere.commons.utils.mock.MockTestDataUtil;
|
|
||||||
import io.metersphere.notice.annotation.SendNotice;
|
|
||||||
import io.metersphere.service.definition.ApiDefinitionService;
|
|
||||||
import io.metersphere.service.MockConfigService;
|
|
||||||
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
|
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
|
||||||
import io.metersphere.base.domain.MockExpectConfig;
|
import io.metersphere.base.domain.MockExpectConfig;
|
||||||
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
|
import io.metersphere.base.domain.MockExpectConfigWithBLOBs;
|
||||||
|
import io.metersphere.commons.utils.mock.MockApiUtils;
|
||||||
|
import io.metersphere.commons.utils.mock.MockTestDataUtil;
|
||||||
|
import io.metersphere.service.MockConfigService;
|
||||||
|
import io.metersphere.service.definition.ApiDefinitionService;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
@ -30,7 +28,7 @@ import java.util.Map;
|
||||||
* @Description
|
* @Description
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/mock/config")
|
@RequestMapping("/mock-config")
|
||||||
public class MockConfigController {
|
public class MockConfigController {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
|
|
@ -105,7 +105,7 @@ public class MockConfigService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MockConfigResponse assemblyMockConfingResponse(List<MockConfig> configList) {
|
private MockConfigResponse assemblyMockConingResponse(List<MockConfig> configList) {
|
||||||
if (!configList.isEmpty()) {
|
if (!configList.isEmpty()) {
|
||||||
MockConfig config = configList.get(0);
|
MockConfig config = configList.get(0);
|
||||||
MockExpectConfigExample expectConfigExample = new MockExpectConfigExample();
|
MockExpectConfigExample expectConfigExample = new MockExpectConfigExample();
|
||||||
|
@ -131,9 +131,6 @@ public class MockConfigService {
|
||||||
* 如果没数据就生成,有数据就返回一套数据结构!
|
* 如果没数据就生成,有数据就返回一套数据结构!
|
||||||
* 所有入参都没有必填项!
|
* 所有入参都没有必填项!
|
||||||
* 如果为了查询,请写一个新的接口!
|
* 如果为了查询,请写一个新的接口!
|
||||||
*
|
|
||||||
* @param request
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public MockConfigResponse genMockConfig(MockConfigRequest request) {
|
public MockConfigResponse genMockConfig(MockConfigRequest request) {
|
||||||
MockConfigResponse returnRsp;
|
MockConfigResponse returnRsp;
|
||||||
|
@ -153,40 +150,28 @@ public class MockConfigService {
|
||||||
|
|
||||||
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
||||||
if (configList.isEmpty()) {
|
if (configList.isEmpty()) {
|
||||||
long createTimeStmp = System.currentTimeMillis();
|
long createTimeLong = System.currentTimeMillis();
|
||||||
|
|
||||||
MockConfig config = new MockConfig();
|
MockConfig config = new MockConfig();
|
||||||
config.setProjectId(request.getProjectId());
|
config.setProjectId(request.getProjectId());
|
||||||
config.setId(UUID.randomUUID().toString());
|
config.setId(UUID.randomUUID().toString());
|
||||||
config.setCreateUserId(SessionUtils.getUserId());
|
config.setCreateUserId(SessionUtils.getUserId());
|
||||||
config.setCreateTime(createTimeStmp);
|
config.setCreateTime(createTimeLong);
|
||||||
config.setUpdateTime(createTimeStmp);
|
config.setUpdateTime(createTimeLong);
|
||||||
if (request.getApiId() != null) {
|
if (request.getApiId() != null) {
|
||||||
config.setApiId(request.getApiId());
|
config.setApiId(request.getApiId());
|
||||||
mockConfigMapper.insert(config);
|
mockConfigMapper.insert(config);
|
||||||
}
|
}
|
||||||
returnRsp = new MockConfigResponse(config, new ArrayList<>());
|
returnRsp = new MockConfigResponse(config, new ArrayList<>());
|
||||||
} else {
|
} else {
|
||||||
MockConfig config = configList.get(0);
|
returnRsp = this.assemblyMockConingResponse(configList);
|
||||||
MockExpectConfigExample expectConfigExample = new MockExpectConfigExample();
|
|
||||||
expectConfigExample.createCriteria().andMockConfigIdEqualTo(config.getId());
|
|
||||||
expectConfigExample.setOrderByClause("update_time DESC");
|
|
||||||
|
|
||||||
List<MockExpectConfigResponse> expectConfigResponseList = new ArrayList<>();
|
|
||||||
|
|
||||||
List<MockExpectConfigWithBLOBs> expectConfigList = mockExpectConfigMapper.selectByExampleWithBLOBs(expectConfigExample);
|
|
||||||
for (MockExpectConfigWithBLOBs expectConfig : expectConfigList) {
|
|
||||||
MockExpectConfigResponse response = new MockExpectConfigResponse(expectConfig);
|
|
||||||
expectConfigResponseList.add(response);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
returnRsp = new MockConfigResponse(config, expectConfigResponseList);
|
|
||||||
}
|
|
||||||
|
|
||||||
return returnRsp;
|
return returnRsp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void sendMockNotice(MockExpectConfigWithBLOBs mockExpectConfigWithBLOBs, String defaultContext, String event) {
|
public void sendMockNotice(MockExpectConfigWithBLOBs mockExpectConfigWithBLOBs, String defaultContext, String event) {
|
||||||
|
if (SessionUtils.getUserId() != null) {
|
||||||
String context = SessionUtils.getUserId().concat(defaultContext).concat(":").concat(mockExpectConfigWithBLOBs.getName());
|
String context = SessionUtils.getUserId().concat(defaultContext).concat(":").concat(mockExpectConfigWithBLOBs.getName());
|
||||||
Map<String, Object> paramMap = new HashMap<>();
|
Map<String, Object> paramMap = new HashMap<>();
|
||||||
MockConfig mockConfig = mockConfigMapper.selectByPrimaryKey(mockExpectConfigWithBLOBs.getMockConfigId());
|
MockConfig mockConfig = mockConfigMapper.selectByPrimaryKey(mockExpectConfigWithBLOBs.getMockConfigId());
|
||||||
|
@ -194,6 +179,7 @@ public class MockConfigService {
|
||||||
NoticeModel noticeModel = NoticeModel.builder().operator(SessionUtils.getUserId()).context(context).testId(mockExpectConfigWithBLOBs.getId()).subject("接口定义通知").paramMap(paramMap).excludeSelf(true).event(event).build();
|
NoticeModel noticeModel = NoticeModel.builder().operator(SessionUtils.getUserId()).context(context).testId(mockExpectConfigWithBLOBs.getId()).subject("接口定义通知").paramMap(paramMap).excludeSelf(true).event(event).build();
|
||||||
noticeSendService.send(NoticeConstants.TaskType.API_DEFINITION_TASK, noticeModel);
|
noticeSendService.send(NoticeConstants.TaskType.API_DEFINITION_TASK, noticeModel);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void getParamMap(Map<String, Object> paramMap, String userId, MockExpectConfigWithBLOBs mockExpectConfigWithBLOBs, MockConfig mockConfig) {
|
private void getParamMap(Map<String, Object> paramMap, String userId, MockExpectConfigWithBLOBs mockExpectConfigWithBLOBs, MockConfig mockConfig) {
|
||||||
paramMap.put("operator", userId);
|
paramMap.put("operator", userId);
|
||||||
|
@ -216,9 +202,9 @@ public class MockConfigService {
|
||||||
public MockExpectConfig updateMockExpectConfigStatus(MockExpectConfigRequest request) {
|
public MockExpectConfig updateMockExpectConfigStatus(MockExpectConfigRequest request) {
|
||||||
if (StringUtils.isNotEmpty(request.getId()) && StringUtils.isNotEmpty(request.getStatus())) {
|
if (StringUtils.isNotEmpty(request.getId()) && StringUtils.isNotEmpty(request.getStatus())) {
|
||||||
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
|
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
|
||||||
long timeStmp = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
model.setId(request.getId());
|
model.setId(request.getId());
|
||||||
model.setUpdateTime(timeStmp);
|
model.setUpdateTime(timestamp);
|
||||||
model.setStatus(request.getStatus());
|
model.setStatus(request.getStatus());
|
||||||
mockExpectConfigMapper.updateByPrimaryKeySelective(model);
|
mockExpectConfigMapper.updateByPrimaryKeySelective(model);
|
||||||
sendMockNotice(model, "更新了mock", NoticeConstants.Event.MOCK_UPDATE);
|
sendMockNotice(model, "更新了mock", NoticeConstants.Event.MOCK_UPDATE);
|
||||||
|
@ -238,7 +224,7 @@ public class MockConfigService {
|
||||||
if (request.getName() != null) {
|
if (request.getName() != null) {
|
||||||
this.checkNameIsExists(request);
|
this.checkNameIsExists(request);
|
||||||
}
|
}
|
||||||
long timeStmp = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
|
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
|
||||||
if (isSave) {
|
if (isSave) {
|
||||||
String expectNum = this.getMockExpectId(request.getMockConfigId());
|
String expectNum = this.getMockExpectId(request.getMockConfigId());
|
||||||
|
@ -247,7 +233,7 @@ public class MockConfigService {
|
||||||
model.setId(request.getId());
|
model.setId(request.getId());
|
||||||
|
|
||||||
model.setMockConfigId(request.getMockConfigId());
|
model.setMockConfigId(request.getMockConfigId());
|
||||||
model.setUpdateTime(timeStmp);
|
model.setUpdateTime(timestamp);
|
||||||
model.setStatus(request.getStatus());
|
model.setStatus(request.getStatus());
|
||||||
if (request.getTags() != null) {
|
if (request.getTags() != null) {
|
||||||
model.setTags(JSON.toJSONString(request.getTags()));
|
model.setTags(JSON.toJSONString(request.getTags()));
|
||||||
|
@ -260,7 +246,7 @@ public class MockConfigService {
|
||||||
model.setResponse(JSON.toJSONString(request.getResponse()));
|
model.setResponse(JSON.toJSONString(request.getResponse()));
|
||||||
}
|
}
|
||||||
if (isSave) {
|
if (isSave) {
|
||||||
model.setCreateTime(timeStmp);
|
model.setCreateTime(timestamp);
|
||||||
model.setCreateUserId(SessionUtils.getUserId());
|
model.setCreateUserId(SessionUtils.getUserId());
|
||||||
model.setStatus("true");
|
model.setStatus("true");
|
||||||
mockExpectConfigMapper.insert(model);
|
mockExpectConfigMapper.insert(model);
|
||||||
|
@ -321,7 +307,7 @@ public class MockConfigService {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
||||||
MockExpectConfigResponse resultModel = null;
|
MockExpectConfigResponse resultModel;
|
||||||
if (requestObj.has("params")) {
|
if (requestObj.has("params")) {
|
||||||
resultModel = this.getEmptyRequestMockExpectByParams(requestHeaderMap, model);
|
resultModel = this.getEmptyRequestMockExpectByParams(requestHeaderMap, model);
|
||||||
} else {
|
} else {
|
||||||
|
@ -340,13 +326,10 @@ public class MockConfigService {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
JSONObject mockExpectRequestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
JSONObject mockExpectRequestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
||||||
boolean isMatch;
|
boolean isMatch = false;
|
||||||
if (mockExpectRequestObj.has("params")) {
|
if (mockExpectRequestObj.has("params")) {
|
||||||
isMatch = this.isRequestMockExpectMatchingByParams(requestHeaderMap, mockExpectRequestObj, requestMockParams);
|
isMatch = this.isRequestMockExpectMatchingByParams(requestHeaderMap, mockExpectRequestObj, requestMockParams);
|
||||||
} else {
|
|
||||||
isMatch = this.isRequestMockExpectMatching(mockExpectRequestObj, requestMockParams);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isMatch) {
|
if (isMatch) {
|
||||||
returnModel = model;
|
returnModel = model;
|
||||||
break;
|
break;
|
||||||
|
@ -379,7 +362,6 @@ public class MockConfigService {
|
||||||
|
|
||||||
if (expectParamsObj.has("body")) {
|
if (expectParamsObj.has("body")) {
|
||||||
JSONObject expectBodyObject = expectParamsObj.optJSONObject("body");
|
JSONObject expectBodyObject = expectParamsObj.optJSONObject("body");
|
||||||
JSONArray jsonArray = requestMockParams.getBodyParams();
|
|
||||||
String type = expectBodyObject.optString(PropertyConstant.TYPE);
|
String type = expectBodyObject.optString(PropertyConstant.TYPE);
|
||||||
String paramsFilterType = "And";
|
String paramsFilterType = "And";
|
||||||
if (expectBodyObject.has("paramsFilterType")) {
|
if (expectBodyObject.has("paramsFilterType")) {
|
||||||
|
@ -389,11 +371,28 @@ public class MockConfigService {
|
||||||
JSONArray kvsArr = expectBodyObject.optJSONArray("kvs");
|
JSONArray kvsArr = expectBodyObject.optJSONArray("kvs");
|
||||||
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr);
|
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr);
|
||||||
if (CollectionUtils.isNotEmpty(mockConfigRequestParams)) {
|
if (CollectionUtils.isNotEmpty(mockConfigRequestParams)) {
|
||||||
if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) {
|
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getQueryParamsObj(), mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (StringUtils.equalsAnyIgnoreCase(type, "Raw") && expectBodyObject.has("raw")) {
|
||||||
|
String rawExpect = expectBodyObject.optString("raw");
|
||||||
|
if (StringUtils.isEmpty(requestMockParams.getRaw()) || !StringUtils.contains(requestMockParams.getRaw(), rawExpect)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
JSONArray jsonArray = null;
|
||||||
|
if (StringUtils.equalsIgnoreCase(type, "xml") && requestMockParams.getXmlToJsonParam() != null) {
|
||||||
|
jsonArray = new JSONArray();
|
||||||
|
jsonArray.put(requestMockParams.getXmlToJsonParam());
|
||||||
|
} else if (StringUtils.equalsIgnoreCase(type, "json") && requestMockParams.getJsonParam() != null) {
|
||||||
|
if (requestMockParams.getJsonParam() instanceof JSONArray) {
|
||||||
|
jsonArray = (JSONArray) requestMockParams.getJsonParam();
|
||||||
|
} else if (requestMockParams.getJsonParam() instanceof JSONObject) {
|
||||||
|
jsonArray = new JSONArray();
|
||||||
|
jsonArray.put(requestMockParams.getJsonParam());
|
||||||
|
}
|
||||||
|
}
|
||||||
Object mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
|
Object mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
|
||||||
if (mockExpectJsonArray instanceof JSONObject) {
|
if (mockExpectJsonArray instanceof JSONObject) {
|
||||||
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONObject) mockExpectJsonArray)) {
|
if (!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONObject) mockExpectJsonArray)) {
|
||||||
|
@ -427,69 +426,11 @@ public class MockConfigService {
|
||||||
if (expectParamsObj.has("rest")) {
|
if (expectParamsObj.has("rest")) {
|
||||||
JSONArray restArray = expectParamsObj.optJSONArray("rest");
|
JSONArray restArray = expectParamsObj.optJSONArray("rest");
|
||||||
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(restArray);
|
List<MockConfigRequestParams> mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(restArray);
|
||||||
if (!MockApiUtils.checkParamsCompliance(requestMockParams.getRestParamsObj(), mockConfigRequestParams, StringUtils.equals(restFilterType, "And"))) {
|
return MockApiUtils.checkParamsCompliance(requestMockParams.getRestParamsObj(), mockConfigRequestParams, StringUtils.equals(restFilterType, "And"));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isRequestMockExpectMatching(JSONObject mockExpectRequestObj, RequestMockParams requestMockParams) {
|
|
||||||
boolean isJsonParam = mockExpectRequestObj.getBoolean("jsonParam");
|
|
||||||
JSONObject mockExpectJson = new JSONObject();
|
|
||||||
if (isJsonParam) {
|
|
||||||
String jsonParams = mockExpectRequestObj.optString("jsonData");
|
|
||||||
JSONValidator jsonValidator = JSONValidator.from(jsonParams);
|
|
||||||
if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) {
|
|
||||||
JSONArray mockExpectArr = JSONUtil.parseArray(jsonParams);
|
|
||||||
for (int expectIndex = 0; expectIndex < mockExpectArr.length(); expectIndex++) {
|
|
||||||
JSONObject itemObj = mockExpectArr.optJSONObject(expectIndex);
|
|
||||||
mockExpectJson = itemObj;
|
|
||||||
}
|
|
||||||
} else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) {
|
|
||||||
JSONObject mockExpectJsonItem = JSONUtil.parseObject(jsonParams);
|
|
||||||
mockExpectJson = mockExpectJsonItem;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
JSONArray jsonArray = mockExpectRequestObj.optJSONArray("variables");
|
|
||||||
for (int i = 0; i < jsonArray.length(); i++) {
|
|
||||||
JSONObject object = jsonArray.optJSONObject(i);
|
|
||||||
String name = StringUtils.EMPTY;
|
|
||||||
String value = StringUtils.EMPTY;
|
|
||||||
if (object.has("name")) {
|
|
||||||
name = String.valueOf(object.get("name")).trim();
|
|
||||||
}
|
|
||||||
if (object.has("value")) {
|
|
||||||
value = String.valueOf(object.get("value")).trim();
|
|
||||||
}
|
|
||||||
if (StringUtils.isNotEmpty(name)) {
|
|
||||||
mockExpectJson.put(name, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean matchRest = false;
|
|
||||||
boolean matchQuery = false;
|
|
||||||
boolean matchBody = false;
|
|
||||||
|
|
||||||
if (requestMockParams.getQueryParamsObj() != null) {
|
|
||||||
matchQuery = JsonStructUtils.checkJsonObjCompliance(requestMockParams.getQueryParamsObj(), mockExpectJson);
|
|
||||||
}
|
|
||||||
if (requestMockParams.getRestParamsObj() != null) {
|
|
||||||
matchRest = JsonStructUtils.checkJsonObjCompliance(requestMockParams.getRestParamsObj(), mockExpectJson);
|
|
||||||
}
|
|
||||||
if (requestMockParams.getBodyParams() != null) {
|
|
||||||
for (int i = 0; i < requestMockParams.getBodyParams().length(); i++) {
|
|
||||||
JSONObject reqJsonObj = requestMockParams.getBodyParams().optJSONObject(i);
|
|
||||||
matchBody = JsonStructUtils.checkJsonObjCompliance(reqJsonObj, mockExpectJson);
|
|
||||||
if (matchBody) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matchRest || matchQuery || matchBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
private MockExpectConfigResponse getEmptyRequestMockExpectByParams(Map<String, String> requestHeaderMap, MockExpectConfigResponse model) {
|
private MockExpectConfigResponse getEmptyRequestMockExpectByParams(Map<String, String> requestHeaderMap, MockExpectConfigResponse model) {
|
||||||
JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
JSONObject requestObj = JSONUtil.parseObject(JSON.toJSONString(model.getRequest()));
|
||||||
if (requestObj.has("params")) {
|
if (requestObj.has("params")) {
|
||||||
|
@ -632,14 +573,14 @@ public class MockConfigService {
|
||||||
int httpCodeNum = 500;
|
int httpCodeNum = 500;
|
||||||
try {
|
try {
|
||||||
httpCodeNum = Integer.parseInt(responseJsonObj.optString("httpCode"));
|
httpCodeNum = Integer.parseInt(responseJsonObj.optString("httpCode"));
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
response.setStatus(httpCodeNum);
|
response.setStatus(httpCodeNum);
|
||||||
}
|
}
|
||||||
if (responseJsonObj.has("delayed")) {
|
if (responseJsonObj.has("delayed")) {
|
||||||
try {
|
try {
|
||||||
sleepTime = Long.parseLong(String.valueOf(responseJsonObj.get("delayed")));
|
sleepTime = Long.parseLong(String.valueOf(responseJsonObj.get("delayed")));
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -662,14 +603,14 @@ public class MockConfigService {
|
||||||
if (responseObj.has("delayed")) {
|
if (responseObj.has("delayed")) {
|
||||||
try {
|
try {
|
||||||
sleepTime = Long.parseLong(String.valueOf(responseObj.get("delayed")));
|
sleepTime = Long.parseLong(String.valueOf(responseObj.get("delayed")));
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sleepTime > 0) {
|
if (sleepTime > 0) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(sleepTime);
|
Thread.sleep(sleepTime);
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -695,7 +636,7 @@ public class MockConfigService {
|
||||||
if (mockExpectConfigWithBLOBs != null && StringUtils.isNotEmpty(mockExpectConfigWithBLOBs.getRequest())) {
|
if (mockExpectConfigWithBLOBs != null && StringUtils.isNotEmpty(mockExpectConfigWithBLOBs.getRequest())) {
|
||||||
try {
|
try {
|
||||||
FileUtils.deleteBodyFiles(mockExpectConfigWithBLOBs.getId());
|
FileUtils.deleteBodyFiles(mockExpectConfigWithBLOBs.getId());
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -708,8 +649,8 @@ public class MockConfigService {
|
||||||
for (MockConfig mockConfig : mockConfigList) {
|
for (MockConfig mockConfig : mockConfigList) {
|
||||||
example.clear();
|
example.clear();
|
||||||
example.createCriteria().andMockConfigIdEqualTo(mockConfig.getId());
|
example.createCriteria().andMockConfigIdEqualTo(mockConfig.getId());
|
||||||
List<MockExpectConfigWithBLOBs> deleteBolobs = mockExpectConfigMapper.selectByExampleWithBLOBs(example);
|
List<MockExpectConfigWithBLOBs> deleteBlobs = mockExpectConfigMapper.selectByExampleWithBLOBs(example);
|
||||||
for (MockExpectConfigWithBLOBs model : deleteBolobs) {
|
for (MockExpectConfigWithBLOBs model : deleteBlobs) {
|
||||||
this.deleteMockExpectFiles(model);
|
this.deleteMockExpectFiles(model);
|
||||||
}
|
}
|
||||||
mockExpectConfigMapper.deleteByExample(example);
|
mockExpectConfigMapper.deleteByExample(example);
|
||||||
|
@ -771,11 +712,11 @@ public class MockConfigService {
|
||||||
for (int index = 0; index < headArr.length(); index++) {
|
for (int index = 0; index < headArr.length(); index++) {
|
||||||
|
|
||||||
JSONObject headObj = headArr.optJSONObject(index);
|
JSONObject headObj = headArr.optJSONObject(index);
|
||||||
if (headObj.has("name") && !queryParamList.contains(headObj.has("name"))) {
|
if (headObj.has("name") && !queryParamList.contains(headObj.optString("name"))) {
|
||||||
queryParamList.add(String.valueOf(headObj.get("name")));
|
queryParamList.add(String.valueOf(headObj.get("name")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (requestObj.has("rest")) {
|
if (requestObj.has("rest")) {
|
||||||
|
@ -783,11 +724,11 @@ public class MockConfigService {
|
||||||
JSONArray headArr = requestObj.optJSONArray("rest");
|
JSONArray headArr = requestObj.optJSONArray("rest");
|
||||||
for (int index = 0; index < headArr.length(); index++) {
|
for (int index = 0; index < headArr.length(); index++) {
|
||||||
JSONObject headObj = headArr.optJSONObject(index);
|
JSONObject headObj = headArr.optJSONObject(index);
|
||||||
if (headObj.has("name") && !restParamList.contains(headObj.has("name"))) {
|
if (headObj.has("name") && !restParamList.contains(headObj.optString("name"))) {
|
||||||
restParamList.add(String.valueOf(headObj.get("name")));
|
restParamList.add(String.valueOf(headObj.get("name")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//请求体参数类型
|
//请求体参数类型
|
||||||
|
@ -802,14 +743,14 @@ public class MockConfigService {
|
||||||
JSONArray kvsArr = bodyObj.optJSONArray("kvs");
|
JSONArray kvsArr = bodyObj.optJSONArray("kvs");
|
||||||
for (int i = 0; i < kvsArr.length(); i++) {
|
for (int i = 0; i < kvsArr.length(); i++) {
|
||||||
JSONObject kv = kvsArr.optJSONObject(i);
|
JSONObject kv = kvsArr.optJSONObject(i);
|
||||||
if (kv.has("name") && !formDataList.contains(kv.has("name"))) {
|
if (kv.has("name") && !formDataList.contains(kv.optString("name"))) {
|
||||||
formDataList.add(String.valueOf(kv.get("name")));
|
formDataList.add(String.valueOf(kv.get("name")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -851,7 +792,7 @@ public class MockConfigService {
|
||||||
JSONObject returnObj = null;
|
JSONObject returnObj = null;
|
||||||
try {
|
try {
|
||||||
returnObj = JSONUtil.parseObject(request);
|
returnObj = JSONUtil.parseObject(request);
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
return returnObj;
|
return returnObj;
|
||||||
}
|
}
|
||||||
|
@ -868,7 +809,7 @@ public class MockConfigService {
|
||||||
MockConfigExample.Criteria criteria = example.createCriteria();
|
MockConfigExample.Criteria criteria = example.createCriteria();
|
||||||
criteria.andApiIdEqualTo(id);
|
criteria.andApiIdEqualTo(id);
|
||||||
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
||||||
return this.assemblyMockConfingResponse(configList);
|
return this.assemblyMockConingResponse(configList);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String checkReturnWithMockExpectByBodyParam(String method, Map<String, String> requestHeaderMap, Project project, HttpServletRequest request, HttpServletResponse response) {
|
public String checkReturnWithMockExpectByBodyParam(String method, Map<String, String> requestHeaderMap, Project project, HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
@ -876,23 +817,23 @@ public class MockConfigService {
|
||||||
boolean matchApi = false;
|
boolean matchApi = false;
|
||||||
String url = request.getRequestURL().toString();
|
String url = request.getRequestURL().toString();
|
||||||
if (project != null) {
|
if (project != null) {
|
||||||
|
RequestMockParams requestMockParams = MockApiUtils.genRequestMockParamsFromHttpRequest(request, true);
|
||||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
List<ApiDefinitionWithBLOBs> qualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
||||||
Object paramJson = MockApiUtils.getPostParamMap(request);
|
for (ApiDefinitionWithBLOBs api : qualifiedApiList) {
|
||||||
JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request);
|
|
||||||
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
|
|
||||||
if (StringUtils.isEmpty(returnStr)) {
|
if (StringUtils.isEmpty(returnStr)) {
|
||||||
RequestMockParams mockParams = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, true);
|
//补足rest参数
|
||||||
|
MockApiUtils.complementRestParam(urlSuffix, api.getPath(), requestMockParams);
|
||||||
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
|
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
|
||||||
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), mockParams);
|
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), requestMockParams);
|
||||||
if (finalExpectConfig != null) {
|
if (finalExpectConfig != null) {
|
||||||
returnStr = this.updateHttpServletResponse(project.getId(), finalExpectConfig, url, requestHeaderMap, mockParams, response);
|
returnStr = this.updateHttpServletResponse(project.getId(), finalExpectConfig, url, requestHeaderMap, requestMockParams, response);
|
||||||
} else {
|
} else {
|
||||||
returnStr = this.getApiDefinitionResponse(api, response);
|
returnStr = this.getApiDefinitionResponse(api, response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (CollectionUtils.isNotEmpty(aualifiedApiList)) {
|
if (CollectionUtils.isNotEmpty(qualifiedApiList)) {
|
||||||
matchApi = true;
|
matchApi = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -908,29 +849,27 @@ public class MockConfigService {
|
||||||
String returnStr = StringUtils.EMPTY;
|
String returnStr = StringUtils.EMPTY;
|
||||||
boolean matchApi = false;
|
boolean matchApi = false;
|
||||||
String url = request.getRequestURL().toString();
|
String url = request.getRequestURL().toString();
|
||||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
|
|
||||||
if (project != null) {
|
if (project != null) {
|
||||||
|
RequestMockParams requestMockParams = MockApiUtils.genRequestMockParamsFromHttpRequest(request, false);
|
||||||
|
|
||||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||||
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
List<ApiDefinitionWithBLOBs> qualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
||||||
|
/*
|
||||||
|
GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况:
|
||||||
|
api1: /api/{name} 参数 name = "ABC"
|
||||||
|
api2: /api/{testParam} 参数 testParam = "ABC"
|
||||||
|
|
||||||
/**
|
匹配预期Mock的逻辑为: 循环apiId进行筛选,直到筛选到预期Mock。如果筛选不到,则取Api的响应模版来进行返回
|
||||||
* GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况:
|
|
||||||
* api1: /api/{name} 参数 name = "ABC"
|
|
||||||
* api2: /api/{testParam} 参数 testParam = "ABC"
|
|
||||||
*
|
|
||||||
* 匹配预期Mock的逻辑为: 循环apiId进行筛选,直到筛选到预期Mock。如果筛选不到,则取Api的响应模版来进行返回
|
|
||||||
*/
|
*/
|
||||||
Object paramJson = MockApiUtils.getPostParamMap(request);
|
for (ApiDefinitionWithBLOBs api : qualifiedApiList) {
|
||||||
JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request);
|
|
||||||
|
|
||||||
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
|
|
||||||
if (StringUtils.isEmpty(returnStr)) {
|
if (StringUtils.isEmpty(returnStr)) {
|
||||||
RequestMockParams paramMap = MockApiUtils.getParams(urlSuffix, api.getPath(), parameterObject, paramJson, false);
|
//补足rest参数
|
||||||
|
MockApiUtils.complementRestParam(urlSuffix, api.getPath(), requestMockParams);
|
||||||
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
|
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
|
||||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||||
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), paramMap);
|
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(requestHeaderMap, mockConfigData.getMockExpectConfigList(), requestMockParams);
|
||||||
if (finalExpectConfig != null) {
|
if (finalExpectConfig != null) {
|
||||||
returnStr = this.updateHttpServletResponse(project.getId(), finalExpectConfig, url, requestHeaderMap, paramMap, response);
|
returnStr = this.updateHttpServletResponse(project.getId(), finalExpectConfig, url, requestHeaderMap, requestMockParams, response);
|
||||||
} else {
|
} else {
|
||||||
returnStr = this.getApiDefinitionResponse(api, response);
|
returnStr = this.getApiDefinitionResponse(api, response);
|
||||||
}
|
}
|
||||||
|
@ -938,7 +877,7 @@ public class MockConfigService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CollectionUtils.isNotEmpty(aualifiedApiList)) {
|
if (CollectionUtils.isNotEmpty(qualifiedApiList)) {
|
||||||
matchApi = true;
|
matchApi = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -991,7 +930,7 @@ public class MockConfigService {
|
||||||
for (TcpTreeTableDataStruct dataStruct : list) {
|
for (TcpTreeTableDataStruct dataStruct : list) {
|
||||||
List<String> nameList = dataStruct.getNameDeep();
|
List<String> nameList = dataStruct.getNameDeep();
|
||||||
for (String name : nameList) {
|
for (String name : nameList) {
|
||||||
if (!returnList.contains(nameList)) {
|
if (!returnList.contains(name)) {
|
||||||
returnList.add(name);
|
returnList.add(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1075,22 +1014,19 @@ public class MockConfigService {
|
||||||
if (isMatch) {
|
if (isMatch) {
|
||||||
JSONObject responseObj = JSONUtil.parseObject(responseStr);
|
JSONObject responseObj = JSONUtil.parseObject(responseStr);
|
||||||
if (responseObj.has("body")) {
|
if (responseObj.has("body")) {
|
||||||
if (isRaw) {
|
|
||||||
MockExpectConfigDTO dto = new MockExpectConfigDTO();
|
MockExpectConfigDTO dto = new MockExpectConfigDTO();
|
||||||
dto.setMockExpectConfig(expectConfig);
|
dto.setMockExpectConfig(expectConfig);
|
||||||
dto.setProjectId(projectId);
|
dto.setProjectId(projectId);
|
||||||
|
if (isRaw) {
|
||||||
rawResult.add(dto);
|
rawResult.add(dto);
|
||||||
} else {
|
} else {
|
||||||
MockExpectConfigDTO dto = new MockExpectConfigDTO();
|
|
||||||
dto.setMockExpectConfig(expectConfig);
|
|
||||||
dto.setProjectId(projectId);
|
|
||||||
structResult.add(dto);
|
structResult.add(dto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1112,7 +1048,7 @@ public class MockConfigService {
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||||
XMLUtil.setExpandEntityReferencesFalse(documentBuilderFactory);
|
XMLUtil.setExpandEntityReferencesFalse(documentBuilderFactory);
|
||||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
||||||
builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8.name()))));
|
builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))));
|
||||||
isXml = true;
|
isXml = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -1128,7 +1064,7 @@ public class MockConfigService {
|
||||||
if (!StringUtils.equalsIgnoreCase("value", type)) {
|
if (!StringUtils.equalsIgnoreCase("value", type)) {
|
||||||
isJson = true;
|
isJson = true;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
return isJson;
|
return isJson;
|
||||||
}
|
}
|
||||||
|
@ -1163,7 +1099,6 @@ public class MockConfigService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMockExpectConfigs(MockConfig mockConfig, List<MockExpectConfigWithBLOBs> list, SqlSession sqlSession) {
|
private void updateMockExpectConfigs(MockConfig mockConfig, List<MockExpectConfigWithBLOBs> list, SqlSession sqlSession) {
|
||||||
int batchCount = 0;
|
|
||||||
for (MockExpectConfigWithBLOBs mockExpect : list) {
|
for (MockExpectConfigWithBLOBs mockExpect : list) {
|
||||||
MockExpectConfig expectInDb = this.findMockExpectConfigByMockConfigIdAndExpectNum(mockConfig.getId(), mockExpect.getExpectNum());
|
MockExpectConfig expectInDb = this.findMockExpectConfigByMockConfigIdAndExpectNum(mockConfig.getId(), mockExpect.getExpectNum());
|
||||||
if (expectInDb == null) {
|
if (expectInDb == null) {
|
||||||
|
@ -1181,10 +1116,8 @@ public class MockConfigService {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (batchCount % 300 == 0) {
|
|
||||||
sqlSession.flushStatements();
|
sqlSession.flushStatements();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private MockExpectConfig findMockExpectConfigByMockConfigIdAndExpectNum(String mockConfigId, String expectNum) {
|
private MockExpectConfig findMockExpectConfigByMockConfigIdAndExpectNum(String mockConfigId, String expectNum) {
|
||||||
MockExpectConfigExample example = new MockExpectConfigExample();
|
MockExpectConfigExample example = new MockExpectConfigExample();
|
||||||
|
@ -1208,7 +1141,6 @@ public class MockConfigService {
|
||||||
config.setApiId(apiId);
|
config.setApiId(apiId);
|
||||||
mockConfigMapper.insert(config);
|
mockConfigMapper.insert(config);
|
||||||
|
|
||||||
int batchCount = 0;
|
|
||||||
for (MockExpectConfigWithBLOBs mockExpect : list) {
|
for (MockExpectConfigWithBLOBs mockExpect : list) {
|
||||||
mockExpect.setId(UUID.randomUUID().toString());
|
mockExpect.setId(UUID.randomUUID().toString());
|
||||||
mockExpect.setMockConfigId(mockId);
|
mockExpect.setMockConfigId(mockId);
|
||||||
|
@ -1217,10 +1149,8 @@ public class MockConfigService {
|
||||||
mockExpect.setCreateUserId(SessionUtils.getUserId());
|
mockExpect.setCreateUserId(SessionUtils.getUserId());
|
||||||
mockExpectConfigMapper.insert(mockExpect);
|
mockExpectConfigMapper.insert(mockExpect);
|
||||||
}
|
}
|
||||||
if (batchCount % 300 == 0) {
|
|
||||||
sqlSession.flushStatements();
|
sqlSession.flushStatements();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private MockConfig selectMockConfigByApiId(String apiId) {
|
private MockConfig selectMockConfigByApiId(String apiId) {
|
||||||
|
@ -1330,12 +1260,10 @@ public class MockConfigService {
|
||||||
int num1 = Integer.parseInt(tcpMockPortArr[0]);
|
int num1 = Integer.parseInt(tcpMockPortArr[0]);
|
||||||
int num2 = Integer.parseInt(tcpMockPortArr[1]);
|
int num2 = Integer.parseInt(tcpMockPortArr[1]);
|
||||||
|
|
||||||
int startNum = num1 > num2 ? num2 : num1;
|
int startNum = Math.min(num1, num2);
|
||||||
int endNum = num1 < num2 ? num2 : num1;
|
int endNum = Math.max(num1, num2);
|
||||||
|
|
||||||
if (port < startNum || port > endNum) {
|
if (!(port < startNum || port > endNum)) {
|
||||||
inRange = false;
|
|
||||||
} else {
|
|
||||||
inRange = true;
|
inRange = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1344,7 +1272,7 @@ public class MockConfigService {
|
||||||
inRange = true;
|
inRange = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return inRange;
|
return inRange;
|
||||||
|
@ -1358,7 +1286,7 @@ public class MockConfigService {
|
||||||
JSONObject configObj = JSONUtil.parseObject(mockEnv.getConfig());
|
JSONObject configObj = JSONUtil.parseObject(mockEnv.getConfig());
|
||||||
if (configObj.has("tcpConfig")) {
|
if (configObj.has("tcpConfig")) {
|
||||||
JSONObject tcpConfigObj = configObj.optJSONObject("tcpConfig");
|
JSONObject tcpConfigObj = configObj.optJSONObject("tcpConfig");
|
||||||
int tcpPort = 0;
|
int tcpPort;
|
||||||
if (tcpConfigObj.has("port")) {
|
if (tcpConfigObj.has("port")) {
|
||||||
tcpPort = tcpConfigObj.optInt("port");
|
tcpPort = tcpConfigObj.optInt("port");
|
||||||
if (tcpPort == 0 || !TCPPool.isTcpOpen(tcpPort)) {
|
if (tcpPort == 0 || !TCPPool.isTcpOpen(tcpPort)) {
|
||||||
|
|
|
@ -2,41 +2,41 @@ import { get, post } from 'metersphere-frontend/src/plugins/request';
|
||||||
import { fileUpload } from '@/api/base-network';
|
import { fileUpload } from '@/api/base-network';
|
||||||
|
|
||||||
export function getMockApiParams(id) {
|
export function getMockApiParams(id) {
|
||||||
return get('/mock/config/get-api-params/' + id);
|
return get('/mock-config/get-api-params/' + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateMockExpectConfigStatus(mockParam) {
|
export function updateMockExpectConfigStatus(mockParam) {
|
||||||
return post('/mock/config/update/expect', mockParam);
|
return post('/mock-config/update/expect', mockParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mockExpectConfig(id) {
|
export function mockExpectConfig(id) {
|
||||||
return get('/mock/config/get-expect/' + id);
|
return get('/mock-config/get-expect/' + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function delMock(id) {
|
export function delMock(id) {
|
||||||
return get('/mock/config/delete/' + id);
|
return get('/mock-config/delete/' + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createMockConfig(mockParam) {
|
export function createMockConfig(mockParam) {
|
||||||
return post('/mock/config/gen', mockParam);
|
return post('/mock-config/gen', mockParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMockApiResponse(id) {
|
export function getMockApiResponse(id) {
|
||||||
return get('/mock/config/get-api-response/' + id);
|
return get('/mock-config/get-api-response/' + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTcpMockTestData(mockParam) {
|
export function getTcpMockTestData(mockParam) {
|
||||||
return post('/mock/config/get-tcp-test-data', mockParam);
|
return post('/mock-config/get-tcp-test-data', mockParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getMockTestData(mockParam) {
|
export function getMockTestData(mockParam) {
|
||||||
return post('/mock/config/test-data', mockParam);
|
return post('/mock-config/test-data', mockParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateMockExpectConfig(mockParam, file, files) {
|
export function updateMockExpectConfig(mockParam, file, files) {
|
||||||
return fileUpload('/mock/config/update/form', file, files, mockParam);
|
return fileUpload('/mock-config/update/form', file, files, mockParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTcpMockInfo(projectId) {
|
export function getTcpMockInfo(projectId) {
|
||||||
return get('/mock/config/get-details/' + projectId);
|
return get('/mock-config/get-details/' + projectId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ export default {
|
||||||
this.getExecResult();
|
this.getExecResult();
|
||||||
} else {
|
} else {
|
||||||
this.response = this.result;
|
this.response = this.result;
|
||||||
|
this.isActive = true;
|
||||||
}
|
}
|
||||||
if (this.apiActive) {
|
if (this.apiActive) {
|
||||||
this.isActive = false;
|
this.isActive = false;
|
||||||
|
@ -58,7 +59,7 @@ export default {
|
||||||
} else {
|
} else {
|
||||||
this.getExecResult();
|
this.getExecResult();
|
||||||
}
|
}
|
||||||
this.isActive = false;
|
this.isActive = true;
|
||||||
},
|
},
|
||||||
apiItem() {
|
apiItem() {
|
||||||
this.getExecResult();
|
this.getExecResult();
|
||||||
|
@ -98,7 +99,7 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
:deep(.el-card__body) {
|
:deep(.el-card__body) {
|
||||||
padding: 15px;
|
padding: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon.is-active {
|
.icon.is-active {
|
||||||
|
|
|
@ -285,7 +285,6 @@ import ApiCaseSimpleList from './components/list/ApiCaseSimpleList';
|
||||||
import ApiDocumentsPage from '@/business/definition/components/list/ApiDocumentsPage';
|
import ApiDocumentsPage from '@/business/definition/components/list/ApiDocumentsPage';
|
||||||
import MsTableButton from 'metersphere-frontend/src/components/MsTableButton';
|
import MsTableButton from 'metersphere-frontend/src/components/MsTableButton';
|
||||||
import MsTabButton from '@/business/commons/MsTabs';
|
import MsTabButton from '@/business/commons/MsTabs';
|
||||||
import MockConfig from '@/business/definition/components/mock/MockConfig';
|
|
||||||
import ApiSchedule from '@/business/definition/components/import/ApiSchedule';
|
import ApiSchedule from '@/business/definition/components/import/ApiSchedule';
|
||||||
import MsEditCompleteContainer from './components/EditCompleteContainer';
|
import MsEditCompleteContainer from './components/EditCompleteContainer';
|
||||||
import MsEnvironmentSelect from './components/case/MsEnvironmentSelect';
|
import MsEnvironmentSelect from './components/case/MsEnvironmentSelect';
|
||||||
|
@ -353,7 +352,6 @@ export default {
|
||||||
MsRunTestSqlPage,
|
MsRunTestSqlPage,
|
||||||
MsRunTestDubboPage,
|
MsRunTestDubboPage,
|
||||||
ApiDocumentsPage,
|
ApiDocumentsPage,
|
||||||
MockConfig,
|
|
||||||
MsEditCompleteContainer,
|
MsEditCompleteContainer,
|
||||||
MsEnvironmentSelect,
|
MsEnvironmentSelect,
|
||||||
MockEditDrawer,
|
MockEditDrawer,
|
||||||
|
|
|
@ -96,15 +96,15 @@
|
||||||
v-if="currentProtocol === 'DUBBO'" />
|
v-if="currentProtocol === 'DUBBO'" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-if="showMock && (currentProtocol === 'HTTP' || currentProtocol === 'TCP')">
|
<el-card v-if="showMock && (currentProtocol === 'HTTP' || currentProtocol === 'TCP')">
|
||||||
<mock-tab
|
<mock-tab
|
||||||
:base-mock-config-data="baseMockConfigData"
|
:base-mock-config-data="baseMockConfigData"
|
||||||
@redirectToTest="redirectToTest"
|
@redirectToTest="redirectToTest"
|
||||||
:version-name="currentApi.versionName"
|
:version-name="currentApi.versionName"
|
||||||
:form="currentApi"
|
:form="currentApi"
|
||||||
:is-tcp="currentProtocol === 'TCP'" />
|
:is-tcp="currentProtocol === 'TCP'" />
|
||||||
</div>
|
</el-card>
|
||||||
<div v-if="showTestCaseList">
|
<el-card v-if="showTestCaseList">
|
||||||
<!--测试用例列表-->
|
<!--测试用例列表-->
|
||||||
<api-case-simple-list
|
<api-case-simple-list
|
||||||
:apiDefinitionId="currentApi.id"
|
:apiDefinitionId="currentApi.id"
|
||||||
|
@ -116,7 +116,7 @@
|
||||||
@refreshTable="refresh"
|
@refreshTable="refresh"
|
||||||
@showExecResult="showExecResult"
|
@showExecResult="showExecResult"
|
||||||
ref="trashCaseList" />
|
ref="trashCaseList" />
|
||||||
</div>
|
</el-card>
|
||||||
<!-- 加载用例 -->
|
<!-- 加载用例 -->
|
||||||
<ms-api-case-list :createCase="createCase" :currentApi="api" @reLoadCase="reLoadCase" ref="caseList" />
|
<ms-api-case-list :createCase="createCase" :currentApi="api" @reLoadCase="reLoadCase" ref="caseList" />
|
||||||
</ms-main-container>
|
</ms-main-container>
|
||||||
|
@ -132,7 +132,6 @@ import MsRunTestTcpPage from './runtest/RunTestTCPPage';
|
||||||
import MsRunTestSqlPage from './runtest/RunTestSQLPage';
|
import MsRunTestSqlPage from './runtest/RunTestSQLPage';
|
||||||
import MsRunTestDubboPage from './runtest/RunTestDubboPage';
|
import MsRunTestDubboPage from './runtest/RunTestDubboPage';
|
||||||
import MockTab from '@/business/definition/components/mock/MockTab';
|
import MockTab from '@/business/definition/components/mock/MockTab';
|
||||||
import TcpMockConfig from '@/business/definition/components/mock/TcpMockConfig';
|
|
||||||
import ApiCaseSimpleList from './list/ApiCaseSimpleList';
|
import ApiCaseSimpleList from './list/ApiCaseSimpleList';
|
||||||
import MsApiCaseList from './case/EditApiCase';
|
import MsApiCaseList from './case/EditApiCase';
|
||||||
import { getUUID } from 'metersphere-frontend/src/utils';
|
import { getUUID } from 'metersphere-frontend/src/utils';
|
||||||
|
@ -153,7 +152,6 @@ export default {
|
||||||
MsRunTestSqlPage,
|
MsRunTestSqlPage,
|
||||||
MsRunTestDubboPage,
|
MsRunTestDubboPage,
|
||||||
MockTab,
|
MockTab,
|
||||||
TcpMockConfig,
|
|
||||||
ApiCaseSimpleList,
|
ApiCaseSimpleList,
|
||||||
MsApiCaseList,
|
MsApiCaseList,
|
||||||
ApiBaseInfo,
|
ApiBaseInfo,
|
||||||
|
|
|
@ -713,7 +713,7 @@ export default {
|
||||||
if (!hideAlert) {
|
if (!hideAlert) {
|
||||||
this.$emit('refresh');
|
this.$emit('refresh');
|
||||||
}
|
}
|
||||||
this.$emit('refreshCaseList');
|
this.$emit('refreshCaseList',row.id);
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
this.isSave = false;
|
this.isSave = false;
|
||||||
|
|
|
@ -437,8 +437,20 @@ export default {
|
||||||
refresh() {
|
refresh() {
|
||||||
this.$emit('refresh');
|
this.$emit('refresh');
|
||||||
},
|
},
|
||||||
refreshCaseList() {
|
refreshCaseList(id) {
|
||||||
this.getApiTest(true, true);
|
return new Promise((resolve) => {
|
||||||
|
let commonUseEnvironment = store.useEnvironment;
|
||||||
|
this.environment = commonUseEnvironment ? commonUseEnvironment : '';
|
||||||
|
getCaseById(id).then((response) => {
|
||||||
|
let apiCase = response.data;
|
||||||
|
if (apiCase) {
|
||||||
|
this.formatCase(apiCase);
|
||||||
|
apiCase.active = true;
|
||||||
|
this.apiCaseList = [apiCase];
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
reLoadCase() {
|
reLoadCase() {
|
||||||
this.$emit('reLoadCase');
|
this.$emit('reLoadCase');
|
||||||
|
|
|
@ -1,72 +0,0 @@
|
||||||
<template>
|
|
||||||
<div role="tablist" aria-multiselectable="true">
|
|
||||||
<slot></slot>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
name: 'MsApiCollapse',
|
|
||||||
|
|
||||||
componentName: 'MsApiCollapse',
|
|
||||||
|
|
||||||
props: {
|
|
||||||
accordion: Boolean,
|
|
||||||
value: {
|
|
||||||
type: [Array, String, Number],
|
|
||||||
default() {
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
activeNames: [].concat(this.value),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
provide() {
|
|
||||||
return {
|
|
||||||
collapse: this,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
|
||||||
value(value) {
|
|
||||||
this.activeNames = [].concat(value);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
setActiveNames(activeNames, item) {
|
|
||||||
activeNames = [].concat(activeNames);
|
|
||||||
let value = this.accordion ? activeNames[0] : activeNames;
|
|
||||||
this.activeNames = activeNames;
|
|
||||||
this.$emit('input', value);
|
|
||||||
},
|
|
||||||
handleItemCollapseClick(item) {
|
|
||||||
if (this.accordion) {
|
|
||||||
this.setActiveNames((this.activeNames[0] || this.activeNames[0] === 0) && item.name, item);
|
|
||||||
} else {
|
|
||||||
let activeNames = this.activeNames.slice(0);
|
|
||||||
let index = activeNames.indexOf(item.name);
|
|
||||||
|
|
||||||
if (index > -1) {
|
|
||||||
activeNames.splice(index, 1);
|
|
||||||
} else {
|
|
||||||
activeNames.push(item.name);
|
|
||||||
}
|
|
||||||
this.setActiveNames(activeNames, item);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
handleItemClick(item) {
|
|
||||||
this.$emit('change', item.name);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
created() {
|
|
||||||
this.$on('item-click', this.handleItemClick);
|
|
||||||
this.$on('collapse-click', this.handleItemCollapseClick);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
|
@ -1,127 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="el-collapse-item" :class="{ 'is-active': isActive, 'is-disabled': disabled }">
|
|
||||||
<div
|
|
||||||
role="tab"
|
|
||||||
:aria-expanded="isActive"
|
|
||||||
:aria-controls="`el-collapse-content-${id}`"
|
|
||||||
:aria-describedby="`el-collapse-content-${id}`"
|
|
||||||
@click="handleHeaderClick">
|
|
||||||
<div
|
|
||||||
class="el-collapse-item__header"
|
|
||||||
role="button"
|
|
||||||
:id="`el-collapse-head-${id}`"
|
|
||||||
:tabindex="disabled ? undefined : 0"
|
|
||||||
@keyup.space.enter.stop="handleEnterClick"
|
|
||||||
:class="{
|
|
||||||
focusing: focusing,
|
|
||||||
'is-active': isActive,
|
|
||||||
}"
|
|
||||||
@focus="handleFocus"
|
|
||||||
@blur="focusing = false">
|
|
||||||
<div @click.stop="handleCollapseClick">
|
|
||||||
<i class="el-collapse-item__arrow el-icon-arrow-right" :class="{ 'is-active': isActive }"> </i>
|
|
||||||
</div>
|
|
||||||
<slot name="title">{{ title }}</slot>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<el-collapse-transition>
|
|
||||||
<div
|
|
||||||
class="el-collapse-item__wrap"
|
|
||||||
v-show="isActive"
|
|
||||||
role="tabpanel"
|
|
||||||
:aria-hidden="!isActive"
|
|
||||||
:aria-labelledby="`el-collapse-head-${id}`"
|
|
||||||
:id="`el-collapse-content-${id}`">
|
|
||||||
<div class="el-collapse-item__content">
|
|
||||||
<slot></slot>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</el-collapse-transition>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
<script>
|
|
||||||
import Emitter from 'element-ui/src/mixins/emitter';
|
|
||||||
import { generateId } from 'element-ui/src/utils/util';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'MsApiCollapseItem',
|
|
||||||
|
|
||||||
componentName: 'MsApiCollapseItem',
|
|
||||||
|
|
||||||
mixins: [Emitter],
|
|
||||||
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
contentWrapStyle: {
|
|
||||||
height: 'auto',
|
|
||||||
display: 'block',
|
|
||||||
},
|
|
||||||
contentHeight: 0,
|
|
||||||
focusing: false,
|
|
||||||
isClick: false,
|
|
||||||
id: generateId(),
|
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
inject: ['collapse'],
|
|
||||||
|
|
||||||
props: {
|
|
||||||
title: String,
|
|
||||||
name: {
|
|
||||||
type: [String, Number],
|
|
||||||
default() {
|
|
||||||
return this._uid;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
disabled: Boolean,
|
|
||||||
},
|
|
||||||
|
|
||||||
computed: {
|
|
||||||
isActive() {
|
|
||||||
return this.collapse.activeNames.indexOf(this.name) > -1;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
|
||||||
handleFocus() {
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!this.isClick) {
|
|
||||||
this.focusing = true;
|
|
||||||
} else {
|
|
||||||
this.isClick = false;
|
|
||||||
}
|
|
||||||
}, 50);
|
|
||||||
},
|
|
||||||
handleHeaderClick() {
|
|
||||||
if (this.disabled) return;
|
|
||||||
this.dispatch('MsApiCollapse', 'item-click', this);
|
|
||||||
this.focusing = false;
|
|
||||||
this.isClick = true;
|
|
||||||
},
|
|
||||||
handleCollapseClick() {
|
|
||||||
if (this.disabled) return;
|
|
||||||
this.dispatch('MsApiCollapse', 'collapse-click', this);
|
|
||||||
this.focusing = false;
|
|
||||||
this.isClick = true;
|
|
||||||
},
|
|
||||||
handleEnterClick() {
|
|
||||||
this.dispatch('MsApiCollapse', 'item-click', this);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.el-collapse-item__header {
|
|
||||||
padding-left: 7px;
|
|
||||||
border-right: 2px solid #409eff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.el-collapse-item__header.is-active {
|
|
||||||
background-color: #e9e9e9;
|
|
||||||
}
|
|
||||||
|
|
||||||
.el-collapse-item__content {
|
|
||||||
padding-bottom: 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -23,7 +23,9 @@
|
||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
<div style="width: 98%" v-if="body.type == 'Form Data' || body.type == 'WWW_FORM'">
|
<div style="width: 98%" v-if="body.type == 'Form Data' || body.type == 'WWW_FORM'">
|
||||||
<el-row v-if="body.type == 'Form Data' || body.type == 'WWW_FORM'">
|
<el-row v-if="body.type == 'Form Data' || body.type == 'WWW_FORM'">
|
||||||
<el-link class="ms-el-link" @click="batchAdd"> {{ $t('commons.batch_add') }}</el-link>
|
<el-link class="ms-el-link" @click="batchAdd">
|
||||||
|
{{ $t('commons.batch_add') }}
|
||||||
|
</el-link>
|
||||||
</el-row>
|
</el-row>
|
||||||
<mock-combination-condition
|
<mock-combination-condition
|
||||||
:filter-type-object="body"
|
:filter-type-object="body"
|
||||||
|
|
|
@ -117,6 +117,14 @@ export default {
|
||||||
title: this.$t('api_test.request.body') + this.$t('api_test.variable') + ' (Raw)',
|
title: this.$t('api_test.request.body') + this.$t('api_test.variable') + ' (Raw)',
|
||||||
value: this.getScript('bodyRaw'),
|
value: this.getScript('bodyRaw'),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: this.$t('api_test.request.body') + this.$t('api_test.variable') + ' (Json)',
|
||||||
|
value: this.getScript('body.json'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: this.$t('api_test.request.body') + this.$t('api_test.variable') + ' (Xml)',
|
||||||
|
value: this.getScript('body.xml'),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: 'Query ' + this.$t('api_test.definition.document.request_param'),
|
title: 'Query ' + this.$t('api_test.definition.document.request_param'),
|
||||||
value: this.getScript('query'),
|
value: this.getScript('query'),
|
||||||
|
@ -215,6 +223,20 @@ export default {
|
||||||
returnScript = 'var param=vars.get("bodyRaw")';
|
returnScript = 'var param=vars.get("bodyRaw")';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'body.json':
|
||||||
|
if (laguanges === 'python') {
|
||||||
|
returnScript = 'param=vars["body.json"]';
|
||||||
|
} else {
|
||||||
|
returnScript = 'var param=vars.get("body.json")';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'body.xml':
|
||||||
|
if (laguanges === 'python') {
|
||||||
|
returnScript = 'param=vars["body.xml"]';
|
||||||
|
} else {
|
||||||
|
returnScript = 'var param=vars.get("body.xml")';
|
||||||
|
}
|
||||||
|
break;
|
||||||
case 'query':
|
case 'query':
|
||||||
if (laguanges === 'python') {
|
if (laguanges === 'python') {
|
||||||
returnScript = 'param=vars["query.${param}"]';
|
returnScript = 'param=vars["query.${param}"]';
|
||||||
|
|
|
@ -1,354 +0,0 @@
|
||||||
<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
|
|
||||||
<div>
|
|
||||||
<el-row>
|
|
||||||
<el-col :span="spanCount">
|
|
||||||
<div style="border: 1px #dcdfe6 solid; height: 100%; border-radius: 4px; width: 100%" v-loading="isReloadData">
|
|
||||||
<el-tabs v-model="activeName" class="request-tabs"> </el-tabs>
|
|
||||||
</div>
|
|
||||||
</el-col>
|
|
||||||
</el-row>
|
|
||||||
<batch-add-parameter @batchSave="batchSave" ref="batchAddParameter" />
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { testDataGenerator } from '@/api/xpack';
|
|
||||||
import MsApiKeyValue from '@/business/definition/components/ApiKeyValue';
|
|
||||||
import MsApiAuthConfig from '@/business/definition/components/auth/ApiAuthConfig';
|
|
||||||
import ApiRequestMethodSelect from '@/business/definition/components/collapse/ApiRequestMethodSelect';
|
|
||||||
import { REQUEST_HEADERS } from 'metersphere-frontend/src/utils/constants';
|
|
||||||
import MsApiVariable from '@/business/definition/components/ApiVariable';
|
|
||||||
import MsApiAssertions from '@/business/definition/components/assertion/ApiAssertions';
|
|
||||||
import MsApiExtract from '@/business/definition/components/extract/ApiExtract';
|
|
||||||
import { Body, KeyValue } from '@/business/definition/model/ApiTestModel';
|
|
||||||
import { getUUID } from 'metersphere-frontend/src/utils';
|
|
||||||
import { hasLicense, hasPermission } from 'metersphere-frontend/src/utils/permission';
|
|
||||||
import BatchAddParameter from '@/business/definition/components/basis/BatchAddParameter';
|
|
||||||
import MsApiAdvancedConfig from '@/business/definition/components/request/http/ApiAdvancedConfig';
|
|
||||||
import MsJsr233Processor from '@/business/automation/scenario/component/Jsr233Processor';
|
|
||||||
import ApiDefinitionStepButton from '@/business/definition/components/request/components/ApiDefinitionStepButton';
|
|
||||||
import Convert from '@/business/commons/json-schema/convert/convert';
|
|
||||||
import MockApiBody from '@/business/definition/components/mock/Components/MockApiBody';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'MockRequestParam',
|
|
||||||
components: {
|
|
||||||
ApiDefinitionStepButton,
|
|
||||||
MsJsr233Processor,
|
|
||||||
MsApiAdvancedConfig,
|
|
||||||
BatchAddParameter,
|
|
||||||
MsApiVariable,
|
|
||||||
ApiRequestMethodSelect,
|
|
||||||
MsApiExtract,
|
|
||||||
MsApiAuthConfig,
|
|
||||||
MockApiBody,
|
|
||||||
MsApiKeyValue,
|
|
||||||
MsApiAssertions,
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
method: String,
|
|
||||||
request: {},
|
|
||||||
response: {},
|
|
||||||
definitionTest: {
|
|
||||||
type: Boolean,
|
|
||||||
default() {
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
showScript: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
referenced: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
isShowEnable: Boolean,
|
|
||||||
jsonPathList: Array,
|
|
||||||
isReadOnly: {
|
|
||||||
type: Boolean,
|
|
||||||
default: false,
|
|
||||||
},
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
let validateURL = (rule, value, callback) => {
|
|
||||||
try {
|
|
||||||
new URL(this.addProtocol(this.request.url));
|
|
||||||
} catch (e) {
|
|
||||||
callback(this.$t('api_test.request.url_invalid'));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
activeName: this.request.method === 'POST' ? 'body' : 'parameters',
|
|
||||||
rules: {
|
|
||||||
name: [
|
|
||||||
{
|
|
||||||
max: 300,
|
|
||||||
message: this.$t('commons.input_limit', [1, 300]),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
url: [
|
|
||||||
{
|
|
||||||
max: 500,
|
|
||||||
required: true,
|
|
||||||
message: this.$t('commons.input_limit', [1, 500]),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
{ validator: validateURL, trigger: 'blur' },
|
|
||||||
],
|
|
||||||
path: [
|
|
||||||
{
|
|
||||||
max: 500,
|
|
||||||
message: this.$t('commons.input_limit', [0, 500]),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
spanCount: 21,
|
|
||||||
headerSuggestions: REQUEST_HEADERS,
|
|
||||||
isReloadData: false,
|
|
||||||
isBodyShow: true,
|
|
||||||
dialogVisible: false,
|
|
||||||
hasOwnProperty: Object.prototype.hasOwnProperty,
|
|
||||||
propIsEnumerable: Object.prototype.propertyIsEnumerable,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
created() {
|
|
||||||
if (!this.referenced && this.showScript) {
|
|
||||||
this.spanCount = 21;
|
|
||||||
} else {
|
|
||||||
this.spanCount = 24;
|
|
||||||
}
|
|
||||||
this.init();
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
hasPermission,
|
|
||||||
hasLicense,
|
|
||||||
generate() {
|
|
||||||
if (this.request.body && (this.request.body.jsonSchema || this.request.body.raw)) {
|
|
||||||
if (!this.request.body.jsonSchema) {
|
|
||||||
const MsConvert = new Convert();
|
|
||||||
this.request.body.jsonSchema = MsConvert.format(JSON.parse(this.request.body.raw));
|
|
||||||
}
|
|
||||||
testDataGenerator(this.request.body.jsonSchema).then((response) => {
|
|
||||||
if (response.data) {
|
|
||||||
if (this.request.body.format !== 'JSON-SCHEMA') {
|
|
||||||
this.request.body.raw = response.data;
|
|
||||||
} else {
|
|
||||||
const MsConvert = new Convert();
|
|
||||||
let data = MsConvert.format(JSON.parse(response.data));
|
|
||||||
this.request.body.jsonSchema = this.deepAssign(this.request.body.jsonSchema, data);
|
|
||||||
}
|
|
||||||
this.reloadBody();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
remove(row) {
|
|
||||||
let index = this.request.hashTree.indexOf(row);
|
|
||||||
this.request.hashTree.splice(index, 1);
|
|
||||||
this.reload();
|
|
||||||
},
|
|
||||||
copyRow(row) {
|
|
||||||
let obj = JSON.parse(JSON.stringify(row));
|
|
||||||
obj.id = getUUID();
|
|
||||||
this.request.hashTree.push(obj);
|
|
||||||
this.reload();
|
|
||||||
},
|
|
||||||
reload() {
|
|
||||||
this.isReloadData = true;
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.isReloadData = false;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
init() {
|
|
||||||
if (
|
|
||||||
Object.prototype.toString
|
|
||||||
.call(this.request)
|
|
||||||
.match(/\[object (\w+)\]/)[1]
|
|
||||||
.toLowerCase() !== 'object'
|
|
||||||
) {
|
|
||||||
this.request = JSON.parse(this.request);
|
|
||||||
}
|
|
||||||
if (!this.request.body) {
|
|
||||||
this.request.body = new Body();
|
|
||||||
}
|
|
||||||
if (!this.request.headers) {
|
|
||||||
this.request.headers = [];
|
|
||||||
}
|
|
||||||
if (!this.request.body.kvs) {
|
|
||||||
this.request.body.kvs = [];
|
|
||||||
}
|
|
||||||
if (!this.request.rest) {
|
|
||||||
this.request.rest = [];
|
|
||||||
}
|
|
||||||
if (!this.request.arguments) {
|
|
||||||
this.request.arguments = [];
|
|
||||||
}
|
|
||||||
},
|
|
||||||
reloadBody() {
|
|
||||||
// 解决修改请求头后 body 显示错位
|
|
||||||
this.isBodyShow = false;
|
|
||||||
this.$nextTick(() => {
|
|
||||||
this.isBodyShow = true;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
batchAdd() {
|
|
||||||
this.$refs.batchAddParameter.open();
|
|
||||||
},
|
|
||||||
format(array, obj) {
|
|
||||||
if (array) {
|
|
||||||
let isAdd = true;
|
|
||||||
for (let i in array) {
|
|
||||||
let item = array[i];
|
|
||||||
if (item.name === obj.name) {
|
|
||||||
item.value = obj.value;
|
|
||||||
isAdd = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (isAdd) {
|
|
||||||
switch (this.activeName) {
|
|
||||||
case 'parameters':
|
|
||||||
this.request.arguments.unshift(obj);
|
|
||||||
break;
|
|
||||||
case 'rest':
|
|
||||||
this.request.rest.unshift(obj);
|
|
||||||
break;
|
|
||||||
case 'headers':
|
|
||||||
this.request.headers.unshift(obj);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
batchSave(data) {
|
|
||||||
if (data) {
|
|
||||||
let params = data.split('\n');
|
|
||||||
let keyValues = [];
|
|
||||||
params.forEach((item) => {
|
|
||||||
let line = item.split(/:|:/);
|
|
||||||
let required = false;
|
|
||||||
keyValues.unshift(
|
|
||||||
new KeyValue({
|
|
||||||
name: line[0],
|
|
||||||
required: required,
|
|
||||||
value: line[1],
|
|
||||||
description: line[2],
|
|
||||||
type: 'text',
|
|
||||||
valid: false,
|
|
||||||
file: false,
|
|
||||||
encode: true,
|
|
||||||
enable: true,
|
|
||||||
contentType: 'text/plain',
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
keyValues.forEach((item) => {
|
|
||||||
switch (this.activeName) {
|
|
||||||
case 'parameters':
|
|
||||||
this.format(this.request.arguments, item);
|
|
||||||
break;
|
|
||||||
case 'rest':
|
|
||||||
this.format(this.request.rest, item);
|
|
||||||
break;
|
|
||||||
case 'headers':
|
|
||||||
this.format(this.request.headers, item);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
isObj(x) {
|
|
||||||
let type = typeof x;
|
|
||||||
return x !== null && (type === 'object' || type === 'function');
|
|
||||||
},
|
|
||||||
|
|
||||||
toObject(val) {
|
|
||||||
if (val === null || val === undefined) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Object(val);
|
|
||||||
},
|
|
||||||
|
|
||||||
assignKey(to, from, key) {
|
|
||||||
let val = from[key];
|
|
||||||
|
|
||||||
if (val === undefined || val === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!this.hasOwnProperty.call(to, key) || !this.isObj(val)) {
|
|
||||||
to[key] = val;
|
|
||||||
} else {
|
|
||||||
to[key] = this.assign(Object(to[key]), from[key]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
assign(to, from) {
|
|
||||||
if (to === from) {
|
|
||||||
return to;
|
|
||||||
}
|
|
||||||
from = Object(from);
|
|
||||||
for (let key in from) {
|
|
||||||
if (this.hasOwnProperty.call(from, key)) {
|
|
||||||
this.assignKey(to, from, key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.getOwnPropertySymbols) {
|
|
||||||
let symbols = Object.getOwnPropertySymbols(from);
|
|
||||||
|
|
||||||
for (let i = 0; i < symbols.length; i++) {
|
|
||||||
if (this.propIsEnumerable.call(from, symbols[i])) {
|
|
||||||
this.assignKey(to, from, symbols[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return to;
|
|
||||||
},
|
|
||||||
|
|
||||||
deepAssign(target) {
|
|
||||||
target = this.toObject(target);
|
|
||||||
for (let s = 1; s < arguments.length; s++) {
|
|
||||||
this.assign(target, arguments[s]);
|
|
||||||
}
|
|
||||||
return target;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.ms-query {
|
|
||||||
background: #783887;
|
|
||||||
color: white;
|
|
||||||
height: 18px;
|
|
||||||
border-radius: 42%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.ms-header {
|
|
||||||
background: #783887;
|
|
||||||
color: white;
|
|
||||||
height: 18px;
|
|
||||||
border-radius: 42%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.request-tabs {
|
|
||||||
margin: 10px;
|
|
||||||
min-height: 200px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.ms-el-link {
|
|
||||||
float: right;
|
|
||||||
margin-right: 45px;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -1,370 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="card-container">
|
|
||||||
<el-card class="card-content" v-loading="mockConfigData.loading">
|
|
||||||
<p class="tip">期望列表</p>
|
|
||||||
<div class="card">
|
|
||||||
<el-input
|
|
||||||
:placeholder="$t('commons.search_by_name')"
|
|
||||||
class="search-input"
|
|
||||||
size="small"
|
|
||||||
:clearable="serchInputClearable"
|
|
||||||
v-model="tableSearch" />
|
|
||||||
<el-table
|
|
||||||
ref="table"
|
|
||||||
border
|
|
||||||
:data="
|
|
||||||
mockConfigData.mockExpectConfigList.filter(
|
|
||||||
(data) => !tableSearch || data.name.toLowerCase().includes(tableSearch.toLowerCase())
|
|
||||||
)
|
|
||||||
"
|
|
||||||
@row-click="clickRow"
|
|
||||||
row-key="id"
|
|
||||||
class="test-content"
|
|
||||||
:height="screenHeight">
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.name')" min-width="160px" prop="name"></el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.tag')" min-width="200px" prop="tags">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<ms-tag
|
|
||||||
v-for="(itemName, index) in scope.row.tags"
|
|
||||||
:key="index"
|
|
||||||
type="success"
|
|
||||||
effect="plain"
|
|
||||||
:show-tooltip="true"
|
|
||||||
:content="itemName"
|
|
||||||
style="margin-left: 0px; margin-right: 2px" />
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column
|
|
||||||
:label="$t('api_test.mock.table.creator')"
|
|
||||||
min-width="160px"
|
|
||||||
prop="createUserId"></el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.status')" min-width="80px" prop="status">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<div>
|
|
||||||
<el-switch v-model="scope.row.status" class="captcha-img" @change="changeStatus(scope.row)"></el-switch>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.update_time')" min-width="160px" prop="updateTime">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<span>{{ scope.row.updateTime | datetimeFormat }}</span>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column fixed="right" min-width="100" align="center" :label="$t('commons.operating')">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<div>
|
|
||||||
<ms-table-operator-button
|
|
||||||
:tip="$t('commons.copy')"
|
|
||||||
icon="el-icon-copy-document"
|
|
||||||
@exec="copyExpect(scope.row)" />
|
|
||||||
<ms-table-operator-button
|
|
||||||
:tip="$t('commons.delete')"
|
|
||||||
icon="el-icon-delete"
|
|
||||||
@exec="removeExpect(scope.row)" />
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
</el-table>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 期望详情 -->
|
|
||||||
<p class="tip">{{ $t('api_test.mock.expect_detail') }}</p>
|
|
||||||
<el-form :model="mockExpectConfig" :rules="rule" ref="mockExpectForm" label-width="80px" label-position="right">
|
|
||||||
<div class="card">
|
|
||||||
<div class="base-info">
|
|
||||||
<el-row>
|
|
||||||
<el-col>{{ $t('api_test.mock.base_info') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<el-col :span="12">
|
|
||||||
<el-form-item :label="$t('commons.name')" prop="name">
|
|
||||||
<el-input class="ms-http-input" size="small" v-model="mockExpectConfig.name" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
<el-col :span="12">
|
|
||||||
<el-form-item :label="$t('commons.tag')" prop="tag">
|
|
||||||
<ms-input-tag :currentScenario="mockExpectConfig" v-if="showHeadTable" ref="tag" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
</el-row>
|
|
||||||
|
|
||||||
<el-row>
|
|
||||||
<el-col>{{ $t('api_test.mock.req_param') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<el-col style="margin: 10px">
|
|
||||||
<el-switch v-model="mockExpectConfig.request.jsonParam"> </el-switch>
|
|
||||||
JSON
|
|
||||||
</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<div v-if="mockExpectConfig.request.jsonParam">
|
|
||||||
<ms-code-edit
|
|
||||||
height="400px"
|
|
||||||
:mode="'json'"
|
|
||||||
ref="codeEdit"
|
|
||||||
:data.sync="mockExpectConfig.request.jsonData"
|
|
||||||
style="margin-top: 10px" />
|
|
||||||
</div>
|
|
||||||
<div v-else>
|
|
||||||
<mock-row-variables
|
|
||||||
:show-copy="false"
|
|
||||||
v-if="showHeadTable"
|
|
||||||
:header-suggestions="apiParams"
|
|
||||||
:items="mockExpectConfig.request.variables"
|
|
||||||
ref="reqHttpHead" />
|
|
||||||
</div>
|
|
||||||
</el-row>
|
|
||||||
|
|
||||||
<el-row style="margin-top: 10px">
|
|
||||||
<el-col>{{ $t('api_test.mock.rsp_param') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<el-col :span="12">
|
|
||||||
<el-form-item label="HTTP Code" label-width="100px" prop="response.httpCode">
|
|
||||||
<el-input class="ms-http-input" size="small" v-model="mockExpectConfig.response.httpCode" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
<el-col :span="6">
|
|
||||||
<el-form-item label="延时 (ms)" prop="response.delayed">
|
|
||||||
<el-input-number v-model="mockExpectConfig.response.delayed" :min="0">
|
|
||||||
<template slot="append">ms</template>
|
|
||||||
</el-input-number>
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<span style="margin: 10px; font-size: 13px"> HTTP头: </span>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<mock-row-variables
|
|
||||||
v-if="showHeadTable"
|
|
||||||
:show-copy="false"
|
|
||||||
:header-suggestions="headerSuggestions"
|
|
||||||
:items="mockExpectConfig.response.httpHeads"
|
|
||||||
ref="rspHttpHead" />
|
|
||||||
</el-row>
|
|
||||||
<el-row style="margin-top: 10px">
|
|
||||||
<el-form-item label="Body:" label-width="50px">
|
|
||||||
<ms-code-edit
|
|
||||||
height="200px"
|
|
||||||
:mode="'txt'"
|
|
||||||
ref="codeEdit"
|
|
||||||
:data.sync="mockExpectConfig.response.body"
|
|
||||||
style="margin-top: 10px" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<div style="float: right; margin-right: 20px">
|
|
||||||
<el-button type="primary" size="small" @click="saveMockExpectConfig" title="ctrl + s"
|
|
||||||
>{{ $t('commons.add') }}
|
|
||||||
</el-button>
|
|
||||||
<el-button type="primary" size="small" @click="cleanMockExpectConfig"
|
|
||||||
>{{ $t('commons.clear') }}
|
|
||||||
</el-button>
|
|
||||||
<el-button
|
|
||||||
type="primary"
|
|
||||||
size="small"
|
|
||||||
v-if="mockExpectConfig.id != '' && mockExpectConfig.id != null"
|
|
||||||
@click="updateMockExpectConfig"
|
|
||||||
>{{ $t('commons.update') }}
|
|
||||||
</el-button>
|
|
||||||
</div>
|
|
||||||
</el-row>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</el-form>
|
|
||||||
</el-card>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { createMockConfig, delMock, getMockApiParams, updateMockExpectConfig } from '@/api/api-mock';
|
|
||||||
import MsTableOperatorButton from 'metersphere-frontend/src/components/MsTableOperatorButton';
|
|
||||||
import MockRowVariables from '@/business/definition/components/mock/MockRowVariables';
|
|
||||||
import MsInputTag from '@/business/automation/scenario/MsInputTag';
|
|
||||||
import MsCodeEdit from '@/business/definition/components/MsCodeEdit';
|
|
||||||
import MsApiVariableAdvance from 'metersphere-frontend/src/components/environment/commons/ApiVariableAdvance';
|
|
||||||
import MsTag from 'metersphere-frontend/src/components/MsTag';
|
|
||||||
import { REQUEST_HEADERS } from 'metersphere-frontend/src/utils/constants';
|
|
||||||
import { operationConfirm } from 'metersphere-frontend/src/utils';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'MockConfig',
|
|
||||||
components: {
|
|
||||||
MockRowVariables,
|
|
||||||
MsTableOperatorButton,
|
|
||||||
MsInputTag,
|
|
||||||
MsCodeEdit,
|
|
||||||
MsApiVariableAdvance,
|
|
||||||
MsTag,
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
screenHeight: 300,
|
|
||||||
tableSearch: '',
|
|
||||||
showHeadTable: true,
|
|
||||||
serchInputClearable: true,
|
|
||||||
mockConfigData: {},
|
|
||||||
apiParams: [],
|
|
||||||
headerSuggestions: REQUEST_HEADERS,
|
|
||||||
mockExpectConfig: {
|
|
||||||
id: '',
|
|
||||||
name: '',
|
|
||||||
mockConfigId: '',
|
|
||||||
request: {
|
|
||||||
jsonParam: false,
|
|
||||||
variables: [],
|
|
||||||
jsonData: '{}',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
rule: {
|
|
||||||
name: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('test_track.case.input_name'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
max: 100,
|
|
||||||
message: this.$t('test_track.length_less_than') + '100',
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
response: {
|
|
||||||
httpCode: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('api_test.mock.rule.input_code'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
delayed: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('test_track.case.input_name'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
props: { baseMockConfigData: {} },
|
|
||||||
created() {
|
|
||||||
this.mockConfigData = this.baseMockConfigData;
|
|
||||||
this.searchApiParams(this.mockConfigData.mockConfig.apiId);
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
searchApiParams(apiId) {
|
|
||||||
getMockApiParams(apiId).then((response) => {
|
|
||||||
this.apiParams = response.data;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
removeExpect(row) {
|
|
||||||
operationConfirm(this, this.$t('api_test.mock.delete_mock_expect'), () => {
|
|
||||||
let mockInfoId = row.mockConfigId;
|
|
||||||
delMock(row.id).then((response) => {
|
|
||||||
this.cleanMockExpectConfig();
|
|
||||||
this.refreshMockInfo(mockInfoId);
|
|
||||||
this.$message({
|
|
||||||
type: 'success',
|
|
||||||
message: this.$t('commons.delete_success'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
saveMockExpectConfig() {
|
|
||||||
let mockConfigId = this.mockConfigData.mockConfig.id;
|
|
||||||
this.mockExpectConfig.mockConfigId = mockConfigId;
|
|
||||||
this.mockExpectConfig.id = '';
|
|
||||||
let formCheckResult = this.checkMockExpectForm('mockExpectForm', true);
|
|
||||||
},
|
|
||||||
cleanMockExpectConfig() {
|
|
||||||
this.showHeadTable = false;
|
|
||||||
this.mockExpectConfig = {
|
|
||||||
id: '',
|
|
||||||
name: '',
|
|
||||||
mockConfigId: '',
|
|
||||||
request: {
|
|
||||||
jsonParam: false,
|
|
||||||
variables: [],
|
|
||||||
jsonData: '{}',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.$nextTick(function () {
|
|
||||||
this.showHeadTable = true;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
updateMockExpectConfig() {
|
|
||||||
this.checkMockExpectForm('mockExpectForm');
|
|
||||||
},
|
|
||||||
uploadMockExpectConfig(clearForm) {
|
|
||||||
let param = this.mockExpectConfig;
|
|
||||||
updateMockExpectConfig(param).then((response) => {
|
|
||||||
let returnData = response.data;
|
|
||||||
this.mockExpectConfig.id = returnData.id;
|
|
||||||
this.refreshMockInfo(param.mockConfigId);
|
|
||||||
if (clearForm) {
|
|
||||||
this.cleanMockExpectConfig();
|
|
||||||
}
|
|
||||||
this.$message({
|
|
||||||
type: 'success',
|
|
||||||
message: this.$t('commons.save_success'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
refreshMockInfo(mockConfigId) {
|
|
||||||
let mockParam = {};
|
|
||||||
mockParam.id = mockConfigId;
|
|
||||||
createMockConfig(mockParam).then((response) => {
|
|
||||||
this.mockConfigData = response.data;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
checkMockExpectForm(formName, clearForm) {
|
|
||||||
this.$refs[formName].validate((valid) => {
|
|
||||||
if (valid) {
|
|
||||||
this.uploadMockExpectConfig(clearForm);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.search-input {
|
|
||||||
float: right;
|
|
||||||
width: 300px;
|
|
||||||
margin-right: 10px;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .el-form-item {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .el-form-item :deep(.el-form-item__content) {
|
|
||||||
width: 80%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .ms-http-select {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -135,7 +135,7 @@ export default {
|
||||||
tableSearch: '',
|
tableSearch: '',
|
||||||
apiParams: {},
|
apiParams: {},
|
||||||
pageSize: 10,
|
pageSize: 10,
|
||||||
screenHeight: document.documentElement.clientHeight - 250,
|
screenHeight: 'calc(100vh - 205px)',
|
||||||
operators: [
|
operators: [
|
||||||
{
|
{
|
||||||
tip: this.$t('api_test.automation.execute'),
|
tip: this.$t('api_test.automation.execute'),
|
||||||
|
|
|
@ -1,392 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="card-container">
|
|
||||||
<el-card class="card-content" v-loading="mockConfigData.loading">
|
|
||||||
<p class="tip">期望列表</p>
|
|
||||||
<div class="card">
|
|
||||||
<el-input
|
|
||||||
:placeholder="$t('commons.search_by_name')"
|
|
||||||
class="search-input"
|
|
||||||
size="small"
|
|
||||||
:clearable="serchInputClearable"
|
|
||||||
v-model="tableSearch" />
|
|
||||||
<el-table
|
|
||||||
ref="table"
|
|
||||||
border
|
|
||||||
:data="
|
|
||||||
mockConfigData.mockExpectConfigList.filter(
|
|
||||||
(data) => !tableSearch || data.name.toLowerCase().includes(tableSearch.toLowerCase())
|
|
||||||
)
|
|
||||||
"
|
|
||||||
@row-click="clickRow"
|
|
||||||
row-key="id"
|
|
||||||
class="test-content"
|
|
||||||
:height="screenHeight">
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.name')" min-width="160px" prop="name"></el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.tag')" min-width="200px" prop="tags">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<ms-tag
|
|
||||||
v-for="(itemName, index) in scope.row.tags"
|
|
||||||
:key="index"
|
|
||||||
type="success"
|
|
||||||
effect="plain"
|
|
||||||
:show-tooltip="true"
|
|
||||||
:content="itemName"
|
|
||||||
style="margin-left: 0px; margin-right: 2px" />
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column
|
|
||||||
:label="$t('api_test.mock.table.creator')"
|
|
||||||
min-width="160px"
|
|
||||||
prop="createUserId"></el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.status')" min-width="80px" prop="status">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<div>
|
|
||||||
<el-switch v-model="scope.row.status" class="captcha-img" @change="changeStatus(scope.row)"></el-switch>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column :label="$t('api_test.mock.table.update_time')" min-width="160px" prop="updateTime">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<span>{{ scope.row.updateTime | datetimeFormat }}</span>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column fixed="right" min-width="100" align="center" :label="$t('commons.operating')">
|
|
||||||
<template v-slot:default="scope">
|
|
||||||
<div>
|
|
||||||
<ms-table-operator-button
|
|
||||||
:tip="$t('commons.copy')"
|
|
||||||
icon="el-icon-copy-document"
|
|
||||||
@exec="copyExpect(scope.row)" />
|
|
||||||
<ms-table-operator-button
|
|
||||||
:tip="$t('commons.delete')"
|
|
||||||
icon="el-icon-delete"
|
|
||||||
@exec="removeExpect(scope.row)" />
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
</el-table>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 期望详情 -->
|
|
||||||
<p class="tip">{{ $t('api_test.mock.expect_detail') }}</p>
|
|
||||||
<el-form :model="mockExpectConfig" :rules="rule" ref="mockExpectForm" label-width="80px" label-position="right">
|
|
||||||
<div class="card">
|
|
||||||
<div class="base-info">
|
|
||||||
<el-row>
|
|
||||||
<el-col>{{ $t('api_test.mock.base_info') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<el-col :span="12">
|
|
||||||
<el-form-item :label="$t('commons.name')" prop="name">
|
|
||||||
<el-input class="ms-http-input" size="small" v-model="mockExpectConfig.name" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
<el-col :span="12">
|
|
||||||
<el-form-item :label="$t('commons.tag')" prop="tag">
|
|
||||||
<ms-input-tag :currentScenario="mockExpectConfig" v-if="showHeadTable" ref="tag" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-col>
|
|
||||||
</el-row>
|
|
||||||
|
|
||||||
<el-row>
|
|
||||||
<el-col>{{ $t('api_test.mock.req_param') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<tcp-params :request="mockExpectConfig.request" style="margin: 10px 10px" ref="tcpParam"></tcp-params>
|
|
||||||
</el-row>
|
|
||||||
<el-row style="margin-top: 10px">
|
|
||||||
<el-col>{{ $t('api_test.mock.rsp_param') }}</el-col>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<el-form-item label="延时 (ms)" prop="response.delayed">
|
|
||||||
<el-input-number v-model="mockExpectConfig.response.delayed" :min="0">
|
|
||||||
<template slot="append">ms</template>
|
|
||||||
</el-input-number>
|
|
||||||
</el-form-item>
|
|
||||||
</el-row>
|
|
||||||
<el-row style="margin-top: 10px">
|
|
||||||
<el-form-item label="Body:" label-width="50px">
|
|
||||||
<ms-code-edit
|
|
||||||
height="200px"
|
|
||||||
:mode="'txt'"
|
|
||||||
ref="codeEdit"
|
|
||||||
:data.sync="mockExpectConfig.response.body"
|
|
||||||
style="margin-top: 10px" />
|
|
||||||
</el-form-item>
|
|
||||||
</el-row>
|
|
||||||
<el-row>
|
|
||||||
<div style="float: right; margin-right: 20px">
|
|
||||||
<el-button type="primary" size="small" @click="saveMockExpectConfig" title="ctrl + s"
|
|
||||||
>{{ $t('commons.add') }}
|
|
||||||
</el-button>
|
|
||||||
<el-button type="primary" size="small" @click="cleanMockExpectConfig"
|
|
||||||
>{{ $t('commons.clear') }}
|
|
||||||
</el-button>
|
|
||||||
<el-button
|
|
||||||
type="primary"
|
|
||||||
size="small"
|
|
||||||
v-if="mockExpectConfig.id != '' && mockExpectConfig.id != null"
|
|
||||||
@click="updateMockExpectConfig"
|
|
||||||
>{{ $t('commons.update') }}
|
|
||||||
</el-button>
|
|
||||||
</div>
|
|
||||||
</el-row>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</el-form>
|
|
||||||
</el-card>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import { createMockConfig, delMock, mockExpectConfig, updateMockExpectConfig } from '@/api/api-mock';
|
|
||||||
import MsTableOperatorButton from 'metersphere-frontend/src/components/MsTableOperatorButton';
|
|
||||||
import MockRowVariables from '@/business/definition/components/mock/MockRowVariables';
|
|
||||||
import MsInputTag from '@/business/automation/scenario/MsInputTag';
|
|
||||||
import MsCodeEdit from '@/business/definition/components/MsCodeEdit';
|
|
||||||
import MsApiVariableAdvance from 'metersphere-frontend/src/components/environment/commons/ApiVariableAdvance';
|
|
||||||
import MsTag from 'metersphere-frontend/src/components/MsTag';
|
|
||||||
import { REQUEST_HEADERS } from 'metersphere-frontend/src/utils/constants';
|
|
||||||
import TcpParams from '@/business/definition/components/request/tcp/TcpParams';
|
|
||||||
import { operationConfirm } from 'metersphere-frontend/src/utils';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: 'TcpMockConfig',
|
|
||||||
components: {
|
|
||||||
MockRowVariables,
|
|
||||||
MsTableOperatorButton,
|
|
||||||
MsInputTag,
|
|
||||||
MsCodeEdit,
|
|
||||||
MsApiVariableAdvance,
|
|
||||||
TcpParams,
|
|
||||||
MsTag,
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
screenHeight: 300,
|
|
||||||
tableSearch: '',
|
|
||||||
showHeadTable: true,
|
|
||||||
serchInputClearable: true,
|
|
||||||
mockConfigData: {},
|
|
||||||
apiParams: [],
|
|
||||||
headerSuggestions: REQUEST_HEADERS,
|
|
||||||
mockExpectConfig: {
|
|
||||||
id: '',
|
|
||||||
name: '',
|
|
||||||
mockConfigId: '',
|
|
||||||
request: {
|
|
||||||
reportType: 'raw',
|
|
||||||
xmlDataStruct: [],
|
|
||||||
jsonDataStruct: '',
|
|
||||||
rawDataStruct: '',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
rule: {
|
|
||||||
name: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('test_track.case.input_name'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
max: 100,
|
|
||||||
message: this.$t('test_track.length_less_than') + '100',
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
response: {
|
|
||||||
httpCode: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('api_test.mock.rule.input_code'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
delayed: [
|
|
||||||
{
|
|
||||||
required: true,
|
|
||||||
message: this.$t('test_track.case.input_name'),
|
|
||||||
trigger: 'blur',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
baseMockConfigData: {},
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
created() {
|
|
||||||
this.mockConfigData = this.baseMockConfigData;
|
|
||||||
// this.searchApiParams(this.mockConfigData.mockConfig.apiId);
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
copyExpect(row) {
|
|
||||||
mockExpectConfig(row.id).then((response) => {
|
|
||||||
let data = response.data;
|
|
||||||
this.showHeadTable = false;
|
|
||||||
this.mockExpectConfig = data;
|
|
||||||
this.mockExpectConfig.id = '';
|
|
||||||
this.mockExpectConfig.name = this.mockExpectConfig.name + '_copy';
|
|
||||||
if (this.mockExpectConfig.request == null) {
|
|
||||||
this.mockExpectConfig.request = {
|
|
||||||
jsonParam: false,
|
|
||||||
variables: [],
|
|
||||||
jsonData: '{}',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (this.mockExpectConfig.response == null) {
|
|
||||||
this.mockExpectConfig.response = {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
this.$nextTick(function () {
|
|
||||||
this.showHeadTable = true;
|
|
||||||
this.saveMockExpectConfig();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
removeExpect(row) {
|
|
||||||
operationConfirm(this, this.$t('api_test.mock.delete_mock_expect'), () => {
|
|
||||||
let mockInfoId = row.mockConfigId;
|
|
||||||
delMock(row.id).then((response) => {
|
|
||||||
this.cleanMockExpectConfig();
|
|
||||||
this.refreshMockInfo(mockInfoId);
|
|
||||||
this.$message({
|
|
||||||
type: 'success',
|
|
||||||
message: this.$t('commons.delete_success'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
saveMockExpectConfig() {
|
|
||||||
let mockConfigId = this.mockConfigData.mockConfig.id;
|
|
||||||
this.mockExpectConfig.mockConfigId = mockConfigId;
|
|
||||||
this.mockExpectConfig.id = '';
|
|
||||||
let formCheckResult = this.checkMockExpectForm('mockExpectForm', true);
|
|
||||||
},
|
|
||||||
cleanMockExpectConfig() {
|
|
||||||
this.showHeadTable = false;
|
|
||||||
this.mockExpectConfig = {
|
|
||||||
id: '',
|
|
||||||
name: '',
|
|
||||||
mockConfigId: '',
|
|
||||||
request: {
|
|
||||||
reportType: 'raw',
|
|
||||||
xmlDataStruct: [],
|
|
||||||
jsonDataStruct: '',
|
|
||||||
rawDataStruct: '',
|
|
||||||
},
|
|
||||||
response: {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.$nextTick(function () {
|
|
||||||
this.$refs.tcpParam.reload();
|
|
||||||
this.showHeadTable = true;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
updateMockExpectConfig() {
|
|
||||||
this.checkMockExpectForm('mockExpectForm');
|
|
||||||
},
|
|
||||||
uploadMockExpectConfig(clearForm) {
|
|
||||||
let param = this.mockExpectConfig;
|
|
||||||
updateMockExpectConfig(param).then((response) => {
|
|
||||||
let returnData = response.data;
|
|
||||||
this.mockExpectConfig.id = returnData.id;
|
|
||||||
this.refreshMockInfo(param.mockConfigId);
|
|
||||||
if (clearForm) {
|
|
||||||
this.cleanMockExpectConfig();
|
|
||||||
}
|
|
||||||
this.$message({
|
|
||||||
type: 'success',
|
|
||||||
message: this.$t('commons.save_success'),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
refreshMockInfo(mockConfigId) {
|
|
||||||
let mockParam = {};
|
|
||||||
mockParam.id = mockConfigId;
|
|
||||||
createMockConfig(mockParam).then((response) => {
|
|
||||||
this.mockConfigData = response.data;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
checkMockExpectForm(formName, clearForm) {
|
|
||||||
this.$refs[formName].validate((valid) => {
|
|
||||||
if (valid) {
|
|
||||||
this.uploadMockExpectConfig(clearForm);
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
changeStatus(row) {
|
|
||||||
let mockParam = {};
|
|
||||||
mockParam.id = row.id;
|
|
||||||
mockParam.status = row.status;
|
|
||||||
updateMockExpectConfig(mockParam).then((response) => {});
|
|
||||||
},
|
|
||||||
clickRow(row, column, event) {
|
|
||||||
this.cleanMockExpectConfig();
|
|
||||||
mockExpectConfig(row.id).then((response) => {
|
|
||||||
let data = response.data;
|
|
||||||
this.showHeadTable = false;
|
|
||||||
this.mockExpectConfig = data;
|
|
||||||
if (this.mockExpectConfig.request == null) {
|
|
||||||
this.mockExpectConfig.request = {
|
|
||||||
reportType: 'raw',
|
|
||||||
xmlDataStruct: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (this.mockExpectConfig.response == null) {
|
|
||||||
this.mockExpectConfig.response = {
|
|
||||||
httpCode: '',
|
|
||||||
delayed: '',
|
|
||||||
httpHeads: [],
|
|
||||||
body: '',
|
|
||||||
};
|
|
||||||
}
|
|
||||||
this.$nextTick(function () {
|
|
||||||
this.$refs.tcpParam.reload();
|
|
||||||
this.showHeadTable = true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.search-input {
|
|
||||||
float: right;
|
|
||||||
width: 300px;
|
|
||||||
margin-right: 10px;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .el-form-item {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .el-form-item :deep(.el-form-item__content) {
|
|
||||||
width: 80%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.base-info .ms-http-select {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
</style>
|
|
|
@ -198,7 +198,7 @@ public class TestPlanReportService {
|
||||||
if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanUiScenarioIdAndReportIdMap())) {
|
if (MapUtils.isNotEmpty(testPlanExecuteReportDTO.getTestPlanUiScenarioIdAndReportIdMap())) {
|
||||||
// 场景用例
|
// 场景用例
|
||||||
reportIds = new ArrayList<>(testPlanExecuteReportDTO.getTestPlanUiScenarioIdAndReportIdMap().values());
|
reportIds = new ArrayList<>(testPlanExecuteReportDTO.getTestPlanUiScenarioIdAndReportIdMap().values());
|
||||||
planReportCaseDTOS = planApiScenarioReportService.selectForPlanReport(reportIds);
|
planReportCaseDTOS = planUiScenarioReportService.selectForPlanReport(reportIds);
|
||||||
TestPlanStatusCalculator.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, ApiReportStatus.SUCCESS.name());
|
TestPlanStatusCalculator.buildStatusResultMap(planReportCaseDTOS, statusResultMap, report, ApiReportStatus.SUCCESS.name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue