feat(接口测试): ms请求mock接口时检查并添加ms-mock-api-resource-id请求头,用来定位具体的访问接口
ms请求mock接口时检查并添加ms-mock-api-resource-id请求头,用来定位具体的访问接口
This commit is contained in:
parent
68bca0d957
commit
47d3d6d679
|
@ -3,6 +3,7 @@ package io.metersphere.api.dto.definition.request.sampler;
|
|||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.mock.MockApiHeaders;
|
||||
import io.metersphere.api.parse.api.JMeterScriptUtil;
|
||||
import io.metersphere.api.dto.definition.request.ElementUtil;
|
||||
import io.metersphere.api.dto.definition.request.ParameterConfig;
|
||||
|
@ -163,6 +164,20 @@ public class MsHTTPSamplerProxy extends MsTestElement {
|
|||
}
|
||||
final HashTree httpSamplerTree = tree.add(sampler);
|
||||
// 注意顺序,放在config前面,会优先于环境的请求头生效
|
||||
if(httpConfig.isMock() && StringUtils.isNotEmpty(this.getId())){
|
||||
//如果选择的是mock环境,则自动添加一个apiHeader。
|
||||
AtomicBoolean headersHasMockApiId = new AtomicBoolean(false);
|
||||
if(CollectionUtils.isNotEmpty(this.headers)){
|
||||
this.headers.forEach(item -> {
|
||||
if(StringUtils.equals(item.getName(),MockApiHeaders.MOCK_API_RESOURCE_ID)){
|
||||
headersHasMockApiId.set(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
if(!headersHasMockApiId.get()){
|
||||
this.headers.add( new KeyValue(MockApiHeaders.MOCK_API_RESOURCE_ID,this.getId()));
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(this.headers)) {
|
||||
setHeader(httpSamplerTree, this.headers);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package io.metersphere.api.dto.mock;
|
||||
|
||||
/**
|
||||
* 存放mock服务中会用到的header的key
|
||||
*/
|
||||
public class MockApiHeaders {
|
||||
public static final String MOCK_API_RESOURCE_ID = "ms-mock-api-resource-id";
|
||||
}
|
|
@ -3,11 +3,7 @@ package io.metersphere.service;
|
|||
import io.metersphere.api.dto.ApiTestImportRequest;
|
||||
import io.metersphere.api.dto.automation.EsbDataStruct;
|
||||
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
|
||||
import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO;
|
||||
import io.metersphere.api.dto.mock.MockConfigRequestParams;
|
||||
import io.metersphere.api.dto.mock.MockExpectConfigDTO;
|
||||
import io.metersphere.api.dto.mock.MockParamSuggestions;
|
||||
import io.metersphere.api.dto.mock.RequestMockParams;
|
||||
import io.metersphere.api.dto.mock.*;
|
||||
import io.metersphere.api.dto.mock.config.MockConfigImportDTO;
|
||||
import io.metersphere.api.dto.mock.config.MockConfigRequest;
|
||||
import io.metersphere.api.dto.mock.config.MockExpectConfigRequest;
|
||||
|
@ -860,7 +856,7 @@ public class MockConfigService {
|
|||
String url = request.getRequestURL().toString();
|
||||
if (project != null) {
|
||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
|
||||
List<ApiDefinitionWithBLOBs> aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix,requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
||||
Object paramJson = MockApiUtils.getPostParamMap(request);
|
||||
JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request);
|
||||
for (ApiDefinitionWithBLOBs api : aualifiedApiList) {
|
||||
|
@ -894,7 +890,7 @@ public class MockConfigService {
|
|||
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
|
||||
if (project != null) {
|
||||
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
|
||||
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
|
||||
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix,requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID));
|
||||
|
||||
/**
|
||||
* GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况:
|
||||
|
|
|
@ -2207,12 +2207,17 @@ public class ApiDefinitionService {
|
|||
return extApiDefinitionMapper.selectEffectiveIdByProjectIdAndHaveNotCase(projectId);
|
||||
}
|
||||
|
||||
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String baseUrlSuffix) {
|
||||
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String baseUrlSuffix,String mockApiResourceId) {
|
||||
if (StringUtils.isEmpty(baseUrlSuffix)) {
|
||||
return new ArrayList<>();
|
||||
} else {
|
||||
String apiId = this.getApiIdFromMockApiResourceId(mockApiResourceId,projectId);
|
||||
ApiDefinitionExample example = new ApiDefinitionExample();
|
||||
example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId).andStatusNotEqualTo("Trash").andProtocolEqualTo("HTTP").andLatestEqualTo(true);
|
||||
ApiDefinitionExample.Criteria criteria = example.createCriteria();
|
||||
criteria.andMethodEqualTo(method).andProjectIdEqualTo(projectId).andStatusNotEqualTo("Trash").andProtocolEqualTo("HTTP").andLatestEqualTo(true);
|
||||
if(StringUtils.isNotBlank(apiId)){
|
||||
criteria.andIdEqualTo(apiId);
|
||||
}
|
||||
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
|
||||
List<String> apiIdList = new ArrayList<>();
|
||||
boolean urlSuffixEndEmpty = false;
|
||||
|
@ -2267,6 +2272,24 @@ public class ApiDefinitionService {
|
|||
}
|
||||
}
|
||||
|
||||
private String getApiIdFromMockApiResourceId(String mockApiResourceId,String projectId) {
|
||||
String returnId = null;
|
||||
ApiDefinition apiDefinition = this.get(mockApiResourceId);
|
||||
if(apiDefinition == null){
|
||||
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(mockApiResourceId);
|
||||
if(testCase != null){
|
||||
if(StringUtils.equals(testCase.getProjectId(),projectId) && !StringUtils.equals(testCase.getStatus(),"Trash")){
|
||||
returnId = testCase.getApiDefinitionId();
|
||||
}
|
||||
}
|
||||
}else {
|
||||
if(StringUtils.equals(apiDefinition.getProjectId(),projectId) && !StringUtils.equals(apiDefinition.getStatus(),"Trash") && apiDefinition.getLatest()){
|
||||
returnId = mockApiResourceId;
|
||||
}
|
||||
}
|
||||
return returnId;
|
||||
}
|
||||
|
||||
public String getLogDetails(String id) {
|
||||
ApiDefinitionWithBLOBs bloBs = apiDefinitionMapper.selectByPrimaryKey(id);
|
||||
if (bloBs != null) {
|
||||
|
|
|
@ -568,6 +568,12 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
|
|||
if (returnModel.getConfig() != null && project != null) {
|
||||
try {
|
||||
JSONObject configObj = parseObject(returnModel.getConfig());
|
||||
String socket = url;
|
||||
if (socket.startsWith("http://")) {
|
||||
socket = socket.substring(7);
|
||||
} else if (socket.startsWith("https://")) {
|
||||
socket = socket.substring(8);
|
||||
}
|
||||
if (configObj.has("httpConfig")) {
|
||||
JSONObject httpObj = configObj.getJSONObject("httpConfig");
|
||||
if (httpObj.has("isMock") && httpObj.getBoolean("isMock")) {
|
||||
|
@ -578,12 +584,7 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
|
|||
} else {
|
||||
for (int i = 0; i < conditions.length(); i++) {
|
||||
JSONObject obj = conditions.getJSONObject(i);
|
||||
String socket = url;
|
||||
if (socket.startsWith("http://")) {
|
||||
socket = socket.substring(7);
|
||||
} else if (socket.startsWith("https://")) {
|
||||
socket = socket.substring(8);
|
||||
}
|
||||
|
||||
if (!obj.has("socket") || !StringUtils.startsWith(String.valueOf(obj.get("socket")), socket)) {
|
||||
needUpdate = true;
|
||||
break;
|
||||
|
@ -615,8 +616,13 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
|
|||
needUpdate = true;
|
||||
}
|
||||
|
||||
String ipStr = socket;
|
||||
if (socket.contains(":")) {
|
||||
String[] urlArr = socket.split(":");
|
||||
ipStr = urlArr[0];
|
||||
}
|
||||
if (tcpConfigObj.has("server")) {
|
||||
if (!StringUtils.equals(tcpConfigObj.optString("server"), url)) {
|
||||
if(!StringUtils.equals(tcpConfigObj.optString("server"), ipStr)){
|
||||
needUpdate = true;
|
||||
}
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue