parent
bb4f482a4c
commit
c841a9396f
|
@ -1,20 +1,13 @@
|
|||
package io.metersphere.api.controller;
|
||||
|
||||
import io.metersphere.api.dto.mockconfig.response.MockConfigResponse;
|
||||
import io.metersphere.api.dto.mockconfig.response.MockExpectConfigResponse;
|
||||
import io.metersphere.api.service.ApiDefinitionService;
|
||||
import io.metersphere.api.service.MockConfigService;
|
||||
import io.metersphere.controller.handler.annotation.NoResultHolder;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.messaging.rsocket.RSocketRequester;
|
||||
import org.springframework.messaging.rsocket.annotation.ConnectMapping;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author song.tianyang
|
||||
|
@ -30,166 +23,51 @@ public class MockApiController {
|
|||
@Resource
|
||||
private ApiDefinitionService apiDefinitionService;
|
||||
|
||||
@PostMapping("/{apiId}/**")
|
||||
@PostMapping("/{projectId}/**")
|
||||
@NoResultHolder
|
||||
public String postRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, String> paramMap = mockConfigService.getPostParamMap(request);
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String postRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("POST", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@GetMapping("/{apiId}/**")
|
||||
@GetMapping("/{projectId}/**")
|
||||
@NoResultHolder
|
||||
public String getRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, String> paramMap = mockConfigService.getGetParamMap(request, apiId);
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String getRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("GET", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@PutMapping("/{apiId}/**")
|
||||
@PutMapping("/{projectId}/**")
|
||||
@NoResultHolder
|
||||
public String putRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, String> paramMap = mockConfigService.getPostParamMap(request);
|
||||
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String putRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PUT", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@PatchMapping("/{apiId}/**")
|
||||
@PatchMapping("/{projectId}/**")
|
||||
@NoResultHolder
|
||||
public String patchRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, String> paramMap = mockConfigService.getPostParamMap(request);
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String patchRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PATCH", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{apiId}/**")
|
||||
@DeleteMapping("/{projectId}/**")
|
||||
@NoResultHolder
|
||||
public String deleteRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
Map<String, String> paramMap = mockConfigService.getGetParamMap(request, apiId);
|
||||
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String deleteRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("DELETE", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{apiId}/**", method = RequestMethod.OPTIONS)
|
||||
@RequestMapping(value = "/{projectId}/**", method = RequestMethod.OPTIONS)
|
||||
@NoResultHolder
|
||||
public String optionsRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
|
||||
Map<String, String> paramMapPost = mockConfigService.getPostParamMap(request);
|
||||
Map<String, String> paramMapGet = mockConfigService.getGetParamMap(request, apiId);
|
||||
|
||||
Map<String, String> paramMap = new HashMap<>();
|
||||
paramMap.putAll(paramMapPost);
|
||||
paramMap.putAll(paramMapGet);
|
||||
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
|
||||
public String optionsRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("OPTIONS", projectId, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{apiId}/**", method = RequestMethod.HEAD)
|
||||
@RequestMapping(value = "/{projectId}/**", method = RequestMethod.HEAD)
|
||||
@NoResultHolder
|
||||
public void headRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Map<String, String> paramMap = mockConfigService.getGetParamMap(request, apiId);
|
||||
String returnStr = "";
|
||||
MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
response.setStatus(404);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @ConnectMapping("/{apiId}/**")
|
||||
// public String conntRequest(@PathVariable String apiId, HttpServletRequest request, HttpServletResponse response) {
|
||||
// Enumeration<String> paramNameItor = request.getParameterNames();
|
||||
//
|
||||
// Map<String, String> paramMap = new HashMap<>();
|
||||
// while (paramNameItor.hasMoreElements()) {
|
||||
// String key = paramNameItor.nextElement();
|
||||
// String value = request.getParameter(key);
|
||||
// paramMap.put(key, value);
|
||||
// }
|
||||
//
|
||||
// String returnStr = "";
|
||||
// MockConfigResponse mockConfigData = mockConfigService.findByApiId(apiId);
|
||||
// if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
// MockExpectConfigResponse finalExpectConfig = mockConfigService.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
// response.setStatus(404);
|
||||
// if (finalExpectConfig != null) {
|
||||
// returnStr = mockConfigService.updateHttpServletResponse(finalExpectConfig, response);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return returnStr;
|
||||
// }
|
||||
|
||||
private static final Map<String, RSocketRequester> REQUESTER_MAP = new HashMap<>();
|
||||
|
||||
@ConnectMapping("/{apiId}/**")
|
||||
void onConnect(RSocketRequester rSocketRequester, @Payload String apiId) {
|
||||
System.out.println("ooooooo");
|
||||
rSocketRequester.rsocket()
|
||||
.onClose()
|
||||
.subscribe(null, null,
|
||||
() -> REQUESTER_MAP.remove(apiId, rSocketRequester));
|
||||
REQUESTER_MAP.put(apiId, rSocketRequester);
|
||||
public void headRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
mockConfigService.checkReturnWithMockExpectByUrlParam("HEAD", projectId, request, response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -229,7 +229,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
sampler.setDomain(httpConfig.getDomain());
|
||||
//1.9 增加对Mock环境的判断
|
||||
if (this.isMockEnvironment()) {
|
||||
url = url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getId();
|
||||
url = url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
|
||||
} else {
|
||||
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
|
||||
}
|
||||
|
|
|
@ -949,4 +949,70 @@ public class ApiDefinitionService {
|
|||
return extApiDefinitionMapper.selectEffectiveIdByProjectId(projectId);
|
||||
}
|
||||
|
||||
// public List<ApiDefinition> selectByProjectIdAndMethodAndUrl(String projectId, String method,String url) {
|
||||
// ApiDefinitionExample example = new ApiDefinitionExample();
|
||||
// ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId);
|
||||
// if(StringUtils.isNotEmpty(url)){
|
||||
// criteria.andPathEqualTo(url);
|
||||
// }
|
||||
// return apiDefinitionMapper.selectByExample(example);
|
||||
// }
|
||||
|
||||
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String url, String urlSuffix) {
|
||||
|
||||
if (StringUtils.isEmpty(urlSuffix)) {
|
||||
return new ArrayList<>();
|
||||
} else {
|
||||
if (StringUtils.equalsAnyIgnoreCase(method, "GET", "DELETE")) {
|
||||
ApiDefinitionExample example = new ApiDefinitionExample();
|
||||
ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId);
|
||||
if (StringUtils.isNotEmpty(url)) {
|
||||
criteria.andPathEqualTo(url);
|
||||
}
|
||||
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
|
||||
|
||||
List<String> apiIdList = new ArrayList<>();
|
||||
String[] urlParams = urlSuffix.split("/");
|
||||
for (ApiDefinition api : apiList) {
|
||||
String path = api.getPath();
|
||||
if (path.startsWith("/")) {
|
||||
path = path.substring(1);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(path)) {
|
||||
String[] pathArr = path.split("/");
|
||||
if (pathArr.length == urlParams.length) {
|
||||
boolean isFetch = true;
|
||||
for (int i = 0; i < pathArr.length; i++) {
|
||||
String pathItem = pathArr[i];
|
||||
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
|
||||
if (!StringUtils.equals(pathArr[i], urlParams[i])) {
|
||||
isFetch = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (isFetch) {
|
||||
apiIdList.add(api.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (apiIdList.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
} else {
|
||||
example.clear();
|
||||
example.createCriteria().andIdIn(apiIdList);
|
||||
return apiDefinitionMapper.selectByExampleWithBLOBs(example);
|
||||
}
|
||||
} else {
|
||||
if (!url.startsWith("/")) {
|
||||
url = "/" + url;
|
||||
}
|
||||
ApiDefinitionExample example = new ApiDefinitionExample();
|
||||
ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId).andPathEqualTo(url);
|
||||
return apiDefinitionMapper.selectByExampleWithBLOBs(example);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,11 +120,11 @@ public class ApiTestEnvironmentService {
|
|||
}
|
||||
|
||||
JSONObject commonConfigObj = new JSONObject();
|
||||
JSONArray variablesArr = new JSONArray();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("enable", true);
|
||||
variablesArr.add(map);
|
||||
commonConfigObj.put("variables", variablesArr);
|
||||
JSONArray commonVariablesArr = new JSONArray();
|
||||
Map<String, Object> commonMap = new HashMap<>();
|
||||
commonMap.put("enable", true);
|
||||
commonVariablesArr.add(commonMap);
|
||||
commonConfigObj.put("variables", commonVariablesArr);
|
||||
commonConfigObj.put("enableHost", false);
|
||||
commonConfigObj.put("hosts", new String[]{});
|
||||
|
||||
|
@ -138,7 +138,11 @@ public class ApiTestEnvironmentService {
|
|||
// }
|
||||
httpConfig.put("socket", null);
|
||||
httpConfig.put("domain", null);
|
||||
httpConfig.put("headers", new JSONArray(variablesArr));
|
||||
JSONArray httpVariablesArr = new JSONArray();
|
||||
Map<String, Object> httpMap = new HashMap<>();
|
||||
httpMap.put("enable", true);
|
||||
httpVariablesArr.add(httpMap);
|
||||
httpConfig.put("headers", new JSONArray(httpVariablesArr));
|
||||
httpConfig.put("protocol", null);
|
||||
httpConfig.put("port", null);
|
||||
JSONArray httpItemArr = new JSONArray();
|
||||
|
@ -147,7 +151,11 @@ public class ApiTestEnvironmentService {
|
|||
httpItem.put("type", "NONE");
|
||||
httpItem.put("socket", socket);
|
||||
httpItem.put("protocol", protocol);
|
||||
httpItem.put("headers", new JSONArray(variablesArr));
|
||||
JSONArray protocolVariablesArr = new JSONArray();
|
||||
Map<String, Object> protocolMap = new HashMap<>();
|
||||
protocolMap.put("enable", true);
|
||||
protocolVariablesArr.add(protocolMap);
|
||||
httpItem.put("headers", new JSONArray(protocolVariablesArr));
|
||||
httpItem.put("domain", ipStr);
|
||||
if (StringUtils.isNotEmpty(portStr)) {
|
||||
httpItem.put("port", portStr);
|
||||
|
|
|
@ -7,7 +7,6 @@ import io.metersphere.api.dto.mockconfig.MockExpectConfigRequest;
|
|||
import io.metersphere.api.dto.mockconfig.response.MockConfigResponse;
|
||||
import io.metersphere.api.dto.mockconfig.response.MockExpectConfigResponse;
|
||||
import io.metersphere.base.domain.*;
|
||||
import io.metersphere.base.mapper.ApiDefinitionMapper;
|
||||
import io.metersphere.base.mapper.MockConfigMapper;
|
||||
import io.metersphere.base.mapper.MockExpectConfigMapper;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
|
@ -21,6 +20,7 @@ import javax.annotation.Resource;
|
|||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -31,18 +31,45 @@ public class MockConfigService {
|
|||
@Resource
|
||||
private MockExpectConfigMapper mockExpectConfigMapper;
|
||||
@Resource
|
||||
private ApiDefinitionMapper apiDefinitionMapper;
|
||||
private ApiDefinitionService apiDefinitionService;
|
||||
|
||||
public MockConfigResponse findByApiId(String apiId) {
|
||||
MockConfigRequest request = new MockConfigRequest();
|
||||
request.setApiId(apiId);
|
||||
return this.genMockConfig(request);
|
||||
public MockConfigResponse findByApiIdList(List<String> apiIdList) {
|
||||
if (apiIdList.isEmpty()) {
|
||||
return new MockConfigResponse(null, new ArrayList<>());
|
||||
}
|
||||
MockConfigExample example = new MockConfigExample();
|
||||
MockConfigExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andApiIdIn(apiIdList);
|
||||
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
||||
return this.assemblyMockConfingResponse(configList);
|
||||
}
|
||||
|
||||
private MockConfigResponse assemblyMockConfingResponse(List<MockConfig> configList) {
|
||||
if (!configList.isEmpty()) {
|
||||
MockConfig config = configList.get(0);
|
||||
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);
|
||||
|
||||
}
|
||||
MockConfigResponse returnRsp = new MockConfigResponse(config, expectConfigResponseList);
|
||||
return returnRsp;
|
||||
} else {
|
||||
return new MockConfigResponse(null, new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
public MockConfigResponse genMockConfig(MockConfigRequest request) {
|
||||
MockConfigResponse returnRsp = null;
|
||||
|
||||
|
||||
MockConfigExample example = new MockConfigExample();
|
||||
MockConfigExample.Criteria criteria = example.createCriteria();
|
||||
if (request.getId() != null) {
|
||||
|
@ -90,13 +117,16 @@ public class MockConfigService {
|
|||
}
|
||||
|
||||
public MockExpectConfig updateMockExpectConfig(MockExpectConfigRequest request) {
|
||||
//检查名称是否存在
|
||||
this.checkNameIsExists(request);
|
||||
boolean isSave = false;
|
||||
if (StringUtils.isEmpty(request.getId())) {
|
||||
isSave = true;
|
||||
request.setId(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
//检查名称是否存在
|
||||
if (request.getName() != null) {
|
||||
this.checkNameIsExists(request);
|
||||
}
|
||||
long timeStmp = System.currentTimeMillis();
|
||||
MockExpectConfigWithBLOBs model = new MockExpectConfigWithBLOBs();
|
||||
model.setId(request.getId());
|
||||
|
@ -126,7 +156,7 @@ public class MockConfigService {
|
|||
|
||||
private void checkNameIsExists(MockExpectConfigRequest request) {
|
||||
MockExpectConfigExample example = new MockExpectConfigExample();
|
||||
example.createCriteria().andMockConfigIdEqualTo(request.getMockConfigId()).andNameEqualTo(request.getName().trim());
|
||||
example.createCriteria().andMockConfigIdEqualTo(request.getMockConfigId()).andNameEqualTo(request.getName().trim()).andIdNotEqualTo(request.getId());
|
||||
long count = mockExpectConfigMapper.countByExample(example);
|
||||
if (count > 0) {
|
||||
MSException.throwException(Translator.get("expect_name_exists") + ":" + request.getName());
|
||||
|
@ -171,7 +201,6 @@ public class MockConfigService {
|
|||
}
|
||||
|
||||
boolean notMatching = false;
|
||||
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
String key = entry.getKey().trim();
|
||||
String value = entry.getValue();
|
||||
|
@ -179,10 +208,13 @@ public class MockConfigService {
|
|||
value = value.trim();
|
||||
}
|
||||
|
||||
if (!reqParamMap.containsKey(key) || !StringUtils.equals(value, reqParamMap.get(key))) {
|
||||
notMatching = true;
|
||||
break;
|
||||
if (reqParamMap.containsKey(key)) {
|
||||
if (!StringUtils.equals(value, reqParamMap.get(key))) {
|
||||
notMatching = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (!notMatching) {
|
||||
returnModel = model;
|
||||
|
@ -233,6 +265,102 @@ public class MockConfigService {
|
|||
return returnStr;
|
||||
}
|
||||
|
||||
public String updateHttpServletResponse(List<ApiDefinitionWithBLOBs> apis, HttpServletResponse response) {
|
||||
String returnStr = "";
|
||||
try {
|
||||
for (ApiDefinitionWithBLOBs api : apis) {
|
||||
if (api.getResponse() != null) {
|
||||
JSONObject respObj = JSONObject.parseObject(api.getResponse());
|
||||
if (respObj.containsKey("headers")) {
|
||||
JSONArray headersArr = respObj.getJSONArray("headers");
|
||||
for (int i = 0; i < headersArr.size(); i++) {
|
||||
JSONObject obj = headersArr.getJSONObject(i);
|
||||
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) {
|
||||
response.setHeader(obj.getString("name"), obj.getString("value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (respObj.containsKey("statusCode")) {
|
||||
JSONArray statusCodeArr = respObj.getJSONArray("statusCode");
|
||||
for (int i = 0; i < statusCodeArr.size(); i++) {
|
||||
JSONObject obj = statusCodeArr.getJSONObject(i);
|
||||
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) {
|
||||
response.setHeader(obj.getString("name"), obj.getString("value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (respObj.containsKey("body")) {
|
||||
JSONObject bodyObj = respObj.getJSONObject("body");
|
||||
if (bodyObj.containsKey("type")) {
|
||||
String type = bodyObj.getString("type");
|
||||
if (StringUtils.equals(type, "JSON")) {
|
||||
//判断是否是JsonSchema
|
||||
boolean isJsonSchema = false;
|
||||
if (bodyObj.containsKey("format")) {
|
||||
String foramtValue = String.valueOf(bodyObj.get("format"));
|
||||
if (StringUtils.equals("JSON-SCHEMA", foramtValue)) {
|
||||
isJsonSchema = true;
|
||||
}
|
||||
}
|
||||
if (isJsonSchema) {
|
||||
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
|
||||
returnStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
|
||||
}
|
||||
} else {
|
||||
if (bodyObj.containsKey("raw")) {
|
||||
returnStr = bodyObj.getString("raw");
|
||||
}
|
||||
}
|
||||
} else if (StringUtils.equalsAny(type, "XML", "Raw")) {
|
||||
if (bodyObj.containsKey("raw")) {
|
||||
String raw = bodyObj.getString("raw");
|
||||
returnStr = raw;
|
||||
}
|
||||
} else if (StringUtils.equalsAny(type, "Form Data", "WWW_FORM")) {
|
||||
Map<String, String> paramMap = new LinkedHashMap<>();
|
||||
if (bodyObj.containsKey("kvs")) {
|
||||
JSONArray bodyParamArr = new JSONArray();
|
||||
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
|
||||
for (int i = 0; i < kvsArr.size(); i++) {
|
||||
JSONObject kv = kvsArr.getJSONObject(i);
|
||||
if (kv.containsKey("name")) {
|
||||
paramMap.put(kv.getString("name"), kv.getString("value"));
|
||||
}
|
||||
}
|
||||
}
|
||||
returnStr = JSONObject.toJSONString(paramMap);
|
||||
} else if (StringUtils.equals(type, "BINARY")) {
|
||||
Map<String, String> paramMap = new LinkedHashMap<>();
|
||||
if (bodyObj.containsKey("binary")) {
|
||||
JSONArray kvsArr = bodyObj.getJSONArray("kvs");
|
||||
for (int i = 0; i < kvsArr.size(); i++) {
|
||||
JSONObject kv = kvsArr.getJSONObject(i);
|
||||
if (kv.containsKey("description") && kv.containsKey("files")) {
|
||||
String name = kv.getString("description");
|
||||
JSONArray fileArr = kv.getJSONArray("files");
|
||||
String value = "";
|
||||
for (int j = 0; j < fileArr.size(); j++) {
|
||||
JSONObject fileObj = fileArr.getJSONObject(j);
|
||||
if (fileObj.containsKey("name")) {
|
||||
value += fileObj.getString("name") + " ;";
|
||||
}
|
||||
}
|
||||
paramMap.put(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
returnStr = JSONObject.toJSONString(paramMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
public MockExpectConfigWithBLOBs findMockExpectConfigById(String id) {
|
||||
return mockExpectConfigMapper.selectByPrimaryKey(id);
|
||||
}
|
||||
|
@ -241,13 +369,8 @@ public class MockConfigService {
|
|||
mockExpectConfigMapper.deleteByPrimaryKey(id);
|
||||
}
|
||||
|
||||
public Map<String, String> getGetParamMap(HttpServletRequest request, String apiId) {
|
||||
String urlPrefix = "/mock/" + apiId + "/";
|
||||
String requestUri = request.getRequestURI();
|
||||
String[] urlParamArr = requestUri.split(urlPrefix);
|
||||
String urlParams = urlParamArr[urlParamArr.length - 1];
|
||||
|
||||
Map<String, String> paramMap = this.getSendRestParamMapByIdAndUrl(apiId, urlParams);
|
||||
public Map<String, String> getGetParamMap(String urlParams, ApiDefinitionWithBLOBs api) {
|
||||
Map<String, String> paramMap = this.getSendRestParamMapByIdAndUrl(api, urlParams);
|
||||
return paramMap;
|
||||
}
|
||||
|
||||
|
@ -263,8 +386,8 @@ public class MockConfigService {
|
|||
return paramMap;
|
||||
}
|
||||
|
||||
public Map<String, String> getSendRestParamMapByIdAndUrl(String apiId, String urlParams) {
|
||||
ApiDefinitionWithBLOBs api = apiDefinitionMapper.selectByPrimaryKey(apiId);
|
||||
public Map<String, String> getSendRestParamMapByIdAndUrl(ApiDefinitionWithBLOBs api, String urlParams) {
|
||||
// ApiDefinitionWithBLOBs api = apiDefinitionMapper.selectByPrimaryKey(apiId);
|
||||
Map<String, String> returnMap = new HashMap<>();
|
||||
if (api != null) {
|
||||
String path = api.getPath();
|
||||
|
@ -401,4 +524,72 @@ public class MockConfigService {
|
|||
}
|
||||
return returnObj;
|
||||
}
|
||||
|
||||
public String getUrlSuffix(String projectId, HttpServletRequest request) {
|
||||
String urlPrefix = "/mock/" + projectId + "/";
|
||||
String requestUri = request.getRequestURI();
|
||||
String[] urlParamArr = requestUri.split(urlPrefix);
|
||||
return urlParamArr[urlParamArr.length - 1];
|
||||
}
|
||||
|
||||
public MockConfigResponse findByApiId(String id) {
|
||||
MockConfigExample example = new MockConfigExample();
|
||||
MockConfigExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andApiIdEqualTo(id);
|
||||
List<MockConfig> configList = mockConfigMapper.selectByExample(example);
|
||||
return this.assemblyMockConfingResponse(configList);
|
||||
}
|
||||
|
||||
public String checkReturnWithMockExpectByBodyParam(String method, String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = "";
|
||||
String urlSuffix = this.getUrlSuffix(projectId, request);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(projectId, method, urlSuffix, urlSuffix);
|
||||
Map<String, String> paramMap = this.getPostParamMap(request);
|
||||
|
||||
List<String> apiIdList = aualifiedApiList.stream().map(ApiDefinitionWithBLOBs::getId).collect(Collectors.toList());
|
||||
MockConfigResponse mockConfigData = this.findByApiIdList(apiIdList);
|
||||
boolean isMatch = false;
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
if (finalExpectConfig != null) {
|
||||
isMatch = true;
|
||||
returnStr = this.updateHttpServletResponse(finalExpectConfig, response);
|
||||
}
|
||||
}
|
||||
if (!isMatch) {
|
||||
returnStr = this.updateHttpServletResponse(aualifiedApiList, response);
|
||||
}
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
public String checkReturnWithMockExpectByUrlParam(String get, String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = "";
|
||||
String urlSuffix = this.getUrlSuffix(projectId, request);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(projectId, "GET", null, urlSuffix);
|
||||
|
||||
/**
|
||||
* GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况:
|
||||
* api1: /api/{name} 参数 name = "ABC"
|
||||
* api2: /api/{testParam} 参数 testParam = "ABC"
|
||||
*
|
||||
* 匹配预期Mock的逻辑为: 循环apiId进行筛选,直到筛选到预期Mock。如果筛选不到,则取Api的响应模版来进行返回
|
||||
*/
|
||||
boolean isMatch = false;
|
||||
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
|
||||
Map<String, String> paramMap = this.getGetParamMap(urlSuffix, api);
|
||||
MockConfigResponse mockConfigData = this.findByApiId(api.getId());
|
||||
if (mockConfigData != null && mockConfigData.getMockExpectConfigList() != null) {
|
||||
MockExpectConfigResponse finalExpectConfig = this.findExpectConfig(mockConfigData.getMockExpectConfigList(), paramMap);
|
||||
if (finalExpectConfig != null) {
|
||||
returnStr = this.updateHttpServletResponse(finalExpectConfig, response);
|
||||
isMatch = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isMatch) {
|
||||
returnStr = this.updateHttpServletResponse(aualifiedApiList, response);
|
||||
}
|
||||
return returnStr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,8 +85,8 @@
|
|||
<el-row>
|
||||
<el-col :span="20">
|
||||
Mock地址:
|
||||
<el-link>
|
||||
{{ this.mockBaseUrl + "/mock/" + this.projectId + (this.httpForm.path == null ? "" : this.httpForm.path) }}
|
||||
<el-link :href="getUrlPrefix" target="_blank" style="color: black"
|
||||
type="primary">{{ this.getUrlPrefix }}
|
||||
</el-link>
|
||||
</el-col>
|
||||
<el-col :span="4">
|
||||
|
@ -184,6 +184,41 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
getUrlPrefix() {
|
||||
if (this.httpForm.path == null) {
|
||||
return this.mockBaseUrl + "/mock/" + this.projectId;
|
||||
} else {
|
||||
let path = this.httpForm.path;
|
||||
let protocol = this.httpForm.method;
|
||||
if (protocol === 'GET' || protocol === 'DELETE') {
|
||||
if (this.httpForm.request != null && this.httpForm.request.rest != null) {
|
||||
let pathUrlArr = path.split("/");
|
||||
let newPath = "";
|
||||
pathUrlArr.forEach(item => {
|
||||
if (item !== "") {
|
||||
let pathItem = item;
|
||||
if (item.indexOf("{") === 0 && item.indexOf("}") === (item.length - 1)) {
|
||||
let paramItem = item.substr(1, item.length - 2);
|
||||
for (let i = 0; i < this.httpForm.request.rest.length; i++) {
|
||||
let param = this.httpForm.request.rest[i];
|
||||
if (param.name === paramItem) {
|
||||
pathItem = param.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
newPath += "/" + pathItem;
|
||||
}
|
||||
});
|
||||
if (newPath !== "") {
|
||||
path = newPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.mockBaseUrl + "/mock/" + this.projectId + path;
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
runTest() {
|
||||
this.$refs['httpForm'].validate((valid) => {
|
||||
|
@ -193,7 +228,7 @@
|
|||
} else {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
});
|
||||
},
|
||||
getMaintainerOptions() {
|
||||
let workspaceId = localStorage.getItem(WORKSPACE_ID);
|
||||
|
@ -260,7 +295,14 @@
|
|||
this.mockEnvironment = response.data;
|
||||
let httpConfig = JSON.parse(this.mockEnvironment.config);
|
||||
if (httpConfig != null) {
|
||||
this.mockBaseUrl = httpConfig.httpConfig.socket;
|
||||
httpConfig = httpConfig.httpConfig;
|
||||
let httpType = httpConfig.defaultCondition;
|
||||
let conditions = httpConfig.conditions;
|
||||
conditions.forEach(condition => {
|
||||
if (condition.type === httpType) {
|
||||
this.mockBaseUrl = condition.protocol + "://" + condition.socket;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue