parent
56fe5fe5e8
commit
297f70ec6b
|
@ -2,7 +2,9 @@ package io.metersphere.api.controller;
|
|||
|
||||
import io.metersphere.api.service.ApiDefinitionService;
|
||||
import io.metersphere.api.service.MockConfigService;
|
||||
import io.metersphere.base.domain.Project;
|
||||
import io.metersphere.controller.handler.annotation.NoResultHolder;
|
||||
import io.metersphere.service.ProjectService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
@ -21,53 +23,60 @@ public class MockApiController {
|
|||
@Resource
|
||||
private MockConfigService mockConfigService;
|
||||
@Resource
|
||||
private ApiDefinitionService apiDefinitionService;
|
||||
private ProjectService projectService;
|
||||
|
||||
@PostMapping("/{projectId}/**")
|
||||
@PostMapping("/{projectSystemId}/**")
|
||||
@NoResultHolder
|
||||
public String postRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("POST", projectId, request, response);
|
||||
public String postRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("POST", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/**")
|
||||
@GetMapping("/{projectSystemId}/**")
|
||||
@NoResultHolder
|
||||
public String getRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("GET", projectId, request, response);
|
||||
public String getRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("GET", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@PutMapping("/{projectId}/**")
|
||||
@PutMapping("/{projectSystemId}/**")
|
||||
@NoResultHolder
|
||||
public String putRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PUT", projectId, request, response);
|
||||
public String putRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PUT", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@PatchMapping("/{projectId}/**")
|
||||
@PatchMapping("/{projectSystemId}/**")
|
||||
@NoResultHolder
|
||||
public String patchRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PATCH", projectId, request, response);
|
||||
public String patchRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByBodyParam("PATCH", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{projectId}/**")
|
||||
@DeleteMapping("/{projectSystemId}/**")
|
||||
@NoResultHolder
|
||||
public String deleteRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("DELETE", projectId, request, response);
|
||||
public String deleteRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("DELETE", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{projectId}/**", method = RequestMethod.OPTIONS)
|
||||
@RequestMapping(value = "/{projectSystemId}/**", method = RequestMethod.OPTIONS)
|
||||
@NoResultHolder
|
||||
public String optionsRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("OPTIONS", projectId, request, response);
|
||||
public String optionsRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
String returnStr = mockConfigService.checkReturnWithMockExpectByUrlParam("OPTIONS", project, request, response);
|
||||
return returnStr;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{projectId}/**", method = RequestMethod.HEAD)
|
||||
@RequestMapping(value = "/{projectSystemId}/**", method = RequestMethod.HEAD)
|
||||
@NoResultHolder
|
||||
public void headRequest(@PathVariable String projectId, HttpServletRequest request, HttpServletResponse response) {
|
||||
mockConfigService.checkReturnWithMockExpectByUrlParam("HEAD", projectId, request, response);
|
||||
public void headRequest(@PathVariable String projectSystemId, HttpServletRequest request, HttpServletResponse response) {
|
||||
Project project = projectService.findBySystemId(projectSystemId);
|
||||
mockConfigService.checkReturnWithMockExpectByUrlParam("HEAD", project, request, response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -243,17 +243,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
} else {
|
||||
if (!isCustomizeReq() || isRefEnvironment) {
|
||||
if (isNeedAddMockUrl(url)) {
|
||||
//1.9 增加对Mock环境的判断
|
||||
if (this.isMockEnvironment()) {
|
||||
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
|
||||
} else {
|
||||
if (httpConfig.isMock()) {
|
||||
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
|
||||
} else {
|
||||
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
|
||||
}
|
||||
|
||||
}
|
||||
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
|
||||
}
|
||||
URL urlObject = new URL(url);
|
||||
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
|
||||
|
|
|
@ -19,6 +19,7 @@ import io.metersphere.log.utils.ReflexObjectUtil;
|
|||
import io.metersphere.log.vo.DetailColumn;
|
||||
import io.metersphere.log.vo.OperatingLogDetails;
|
||||
import io.metersphere.log.vo.system.SystemReference;
|
||||
import io.metersphere.service.ProjectService;
|
||||
import io.metersphere.service.SystemParameterService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -151,17 +152,19 @@ public class ApiTestEnvironmentService {
|
|||
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
|
||||
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(apiName);
|
||||
List<ApiTestEnvironmentWithBLOBs> list = this.selectByExampleWithBLOBs(example);
|
||||
|
||||
String projectNumber = this.getSystemIdByProjectId(projectId);
|
||||
if (list.isEmpty()) {
|
||||
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, apiName, baseUrl);
|
||||
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId,projectNumber, protocal, apiName, baseUrl);
|
||||
this.add(returnModel);
|
||||
} else {
|
||||
returnModel = list.get(0);
|
||||
returnModel = this.checkMockEvnIsRightful(returnModel, protocal, projectId, apiName, baseUrl);
|
||||
returnModel = this.checkMockEvnIsRightful(returnModel, protocal, projectId,projectNumber, apiName, baseUrl);
|
||||
}
|
||||
return returnModel;
|
||||
}
|
||||
|
||||
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId, String name, String url) {
|
||||
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId,String projectNumber, String name, String url) {
|
||||
boolean needUpdate = false;
|
||||
if (returnModel.getConfig() != null) {
|
||||
try {
|
||||
|
@ -182,10 +185,18 @@ public class ApiTestEnvironmentService {
|
|||
} else if (socket.startsWith("https://")) {
|
||||
socket = socket.substring(8);
|
||||
}
|
||||
if (!obj.containsKey("socket") || !StringUtils.equals(socket, String.valueOf(obj.get("socket")))) {
|
||||
if (!obj.containsKey("socket") || !StringUtils.startsWith(String.valueOf(obj.get("socket")),socket)) {
|
||||
needUpdate = true;
|
||||
break;
|
||||
} else if (!obj.containsKey("protocol") || !StringUtils.equals(protocal, String.valueOf(obj.get("protocol")))) {
|
||||
needUpdate = true;
|
||||
break;
|
||||
}
|
||||
|
||||
String projectSocket = String.valueOf(obj.get("socket"));
|
||||
if(!StringUtils.contains(projectSocket,"/mock/"+projectNumber)){
|
||||
needUpdate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -193,19 +204,20 @@ public class ApiTestEnvironmentService {
|
|||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
needUpdate = true;
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (needUpdate) {
|
||||
String id = returnModel.getId();
|
||||
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, name, url);
|
||||
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId,projectNumber, protocal, name, url);
|
||||
returnModel.setId(id);
|
||||
apiTestEnvironmentMapper.updateByPrimaryKeyWithBLOBs(returnModel);
|
||||
}
|
||||
return returnModel;
|
||||
}
|
||||
|
||||
private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId, String protocal, String name, String url) {
|
||||
private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId,String projectNumber, String protocal, String name, String url) {
|
||||
String socket = "";
|
||||
if (url.startsWith("http://")) {
|
||||
url = url.substring(7);
|
||||
|
@ -253,7 +265,7 @@ public class ApiTestEnvironmentService {
|
|||
JSONObject httpItem = new JSONObject();
|
||||
httpItem.put("id", UUID.randomUUID().toString());
|
||||
httpItem.put("type", "NONE");
|
||||
httpItem.put("socket", socket);
|
||||
httpItem.put("socket", socket+"/mock/"+projectNumber);
|
||||
httpItem.put("protocol", protocal);
|
||||
JSONArray protocolVariablesArr = new JSONArray();
|
||||
Map<String, Object> protocolMap = new HashMap<>();
|
||||
|
@ -310,12 +322,21 @@ public class ApiTestEnvironmentService {
|
|||
} else if (baseUrl.startsWith("https:")) {
|
||||
protocal = "https";
|
||||
}
|
||||
|
||||
model = this.checkMockEvnIsRightful(model, protocal, model.getProjectId(), model.getName(), baseUrl);
|
||||
String projectNumber = this.getSystemIdByProjectId(model.getProjectId());
|
||||
model = this.checkMockEvnIsRightful(model, protocal, model.getProjectId(),projectNumber, model.getName(), baseUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getSystemIdByProjectId(String projectId){
|
||||
ProjectService projectService = CommonBeanFactory.getBean(ProjectService.class);
|
||||
if(projectService != null){
|
||||
return projectService.getSystemIdByProjectId(projectId);
|
||||
}else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public String getLogDetails(String id) {
|
||||
ApiTestEnvironmentWithBLOBs bloBs = apiTestEnvironmentMapper.selectByPrimaryKey(id);
|
||||
if (bloBs != null) {
|
||||
|
|
|
@ -711,56 +711,66 @@ public class MockConfigService {
|
|||
return this.assemblyMockConfingResponse(configList);
|
||||
}
|
||||
|
||||
public String checkReturnWithMockExpectByBodyParam(String method, String projectId, HttpServletRequest
|
||||
public String checkReturnWithMockExpectByBodyParam(String method, Project project, HttpServletRequest
|
||||
request, HttpServletResponse response) {
|
||||
String returnStr = "";
|
||||
String urlSuffix = this.getUrlSuffix(projectId, request);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(projectId, method, urlSuffix, urlSuffix);
|
||||
JSONObject 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);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
|
||||
if(project!=null){
|
||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, urlSuffix);
|
||||
JSONObject paramMap = this.getPostParamMap(request);
|
||||
|
||||
List<String> apiIdList = aualifiedApiList.stream().map(ApiDefinitionWithBLOBs::getId).collect(Collectors.toList());
|
||||
MockConfigResponse mockConfigData = this.findByApiIdList(apiIdList);
|
||||
|
||||
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
|
||||
public String checkReturnWithMockExpectByUrlParam(String method, Project project, 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) {
|
||||
JSONObject paramMap = this.getGetParamMap(urlSuffix, api, request);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
|
||||
if(project != null){
|
||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, null, urlSuffix);
|
||||
|
||||
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;
|
||||
/**
|
||||
* GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况:
|
||||
* api1: /api/{name} 参数 name = "ABC"
|
||||
* api2: /api/{testParam} 参数 testParam = "ABC"
|
||||
*
|
||||
* 匹配预期Mock的逻辑为: 循环apiId进行筛选,直到筛选到预期Mock。如果筛选不到,则取Api的响应模版来进行返回
|
||||
*/
|
||||
|
||||
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
|
||||
JSONObject paramMap = this.getGetParamMap(urlSuffix, api, request);
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -15,4 +15,7 @@ public interface ExtProjectMapper {
|
|||
int removeIssuePlatform(@Param("platform") String platform, @Param("orgId") String orgId);
|
||||
|
||||
List<ProjectDTO> getSwitchProject(@Param("proRequest") ProjectRequest request);
|
||||
|
||||
String getSystemIdByProjectId(String projectId);
|
||||
|
||||
}
|
||||
|
|
|
@ -93,6 +93,10 @@
|
|||
</if>
|
||||
</where>
|
||||
</select>
|
||||
<select id="getSystemIdByProjectId" resultType="java.lang.String">
|
||||
SELECT system_id FROM project WHERE id = #{0}
|
||||
</select>
|
||||
|
||||
|
||||
<update id="removeIssuePlatform">
|
||||
update project
|
||||
|
|
|
@ -418,4 +418,19 @@ public class ProjectService {
|
|||
public Integer checkSourceRole(String workspaceId, String userId, String roleId) {
|
||||
return extOrganizationMapper.checkSourceRole(workspaceId, userId, roleId);
|
||||
}
|
||||
|
||||
public String getSystemIdByProjectId(String projectId) {
|
||||
return extProjectMapper.getSystemIdByProjectId(projectId);
|
||||
}
|
||||
|
||||
public Project findBySystemId(String systemId) {
|
||||
ProjectExample example = new ProjectExample();
|
||||
example.createCriteria().andSystemIdEqualTo(systemId);
|
||||
List<Project> returnList = projectMapper.selectByExample(example);
|
||||
if(CollectionUtils.isEmpty(returnList)){
|
||||
return null;
|
||||
}else {
|
||||
return returnList.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -194,7 +194,7 @@
|
|||
computed: {
|
||||
getUrlPrefix() {
|
||||
if (this.httpForm.path == null) {
|
||||
return this.mockBaseUrl + "/mock/" + this.projectId;
|
||||
return this.mockBaseUrl;
|
||||
} else {
|
||||
let path = this.httpForm.path;
|
||||
let prefix = "";
|
||||
|
@ -227,7 +227,7 @@
|
|||
}
|
||||
}
|
||||
|
||||
return this.mockBaseUrl + "/mock/" + this.projectId + path + prefix;
|
||||
return this.mockBaseUrl + path + prefix;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue