fix(接口测试): 场景执行添加自定义请求参数

This commit is contained in:
AgAngle 2024-03-20 17:42:01 +08:00 committed by Craftsman
parent 665e09df41
commit 9c868f8097
4 changed files with 43 additions and 9 deletions

View File

@ -557,7 +557,7 @@ CREATE TABLE IF NOT EXISTS api_test_case_blob(
CREATE TABLE IF NOT EXISTS api_file_resource( CREATE TABLE IF NOT EXISTS api_file_resource(
`resource_id` VARCHAR(50) NOT NULL COMMENT '资源ID(接口用例等)' , `resource_id` VARCHAR(50) NOT NULL COMMENT '资源ID(接口用例等)' ,
`file_id` VARCHAR(50) NOT NULL COMMENT '文件ID' , `file_id` VARCHAR(50) NOT NULL COMMENT '文件ID' ,
`file_name` VARCHAR(50) NOT NULL COMMENT '文件名称' , `file_name` VARCHAR(255) NOT NULL COMMENT '文件名称' ,
`resource_type` VARCHAR(50) NOT NULL COMMENT '资源类型(API_DEBUG,API,API_CASE,API_SCENARIO)' , `resource_type` VARCHAR(50) NOT NULL COMMENT '资源类型(API_DEBUG,API,API_CASE,API_SCENARIO)' ,
`create_time` BIGINT NOT NULL COMMENT '创建时间' , `create_time` BIGINT NOT NULL COMMENT '创建时间' ,
`project_id` VARCHAR(50) NOT NULL COMMENT '项目ID' , `project_id` VARCHAR(50) NOT NULL COMMENT '项目ID' ,

View File

@ -0,0 +1,20 @@
package io.metersphere.plugin.api.spi;
import lombok.Data;
/**
* 协议组件的抽象类
* @Author: jianxing
* @CreateTime: 2023-10-30 15:08
*/
@Data
public abstract class AbstractMsProtocolTestElement extends AbstractMsTestElement {
/**
* 是否是自定义请求
*/
private Boolean customizeRequest = false;
/**
* 自定义请求是否启用环境
*/
private Boolean customizeRequestEnvEnable = false;
}

View File

@ -2,7 +2,7 @@ package io.metersphere.api.dto.request.http;
import io.metersphere.api.dto.request.http.auth.HTTPAuthConfig; import io.metersphere.api.dto.request.http.auth.HTTPAuthConfig;
import io.metersphere.api.dto.request.http.body.Body; import io.metersphere.api.dto.request.http.body.Body;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsProtocolTestElement;
import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.system.valid.EnumValue; import io.metersphere.system.valid.EnumValue;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -22,7 +22,7 @@ import java.util.List;
*/ */
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class MsHTTPElement extends AbstractMsTestElement { public class MsHTTPElement extends AbstractMsProtocolTestElement {
/** /**
* 完整请求地址 * 完整请求地址
* 自定义请求时使用该字段 * 自定义请求时使用该字段

View File

@ -70,8 +70,8 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
} }
ApiParamConfig apiParamConfig = (ApiParamConfig) config; ApiParamConfig apiParamConfig = (ApiParamConfig) config;
HttpConfig httpConfig = getHttpConfig(msHTTPElement, apiParamConfig); EnvironmentInfoDTO envConfig = isEnvEnable(msHTTPElement) ? apiParamConfig.getEnvConfig(msHTTPElement.getProjectId()) : null;
EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig(msHTTPElement.getProjectId()); HttpConfig httpConfig = getHttpConfig(msHTTPElement, envConfig);
HTTPSamplerProxy sampler = new HTTPSamplerProxy(); HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setName(msHTTPElement.getName()); sampler.setName(msHTTPElement.getName());
@ -98,6 +98,7 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
// 处理请求头 // 处理请求头
HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig); HeaderManager httpHeader = getHttpHeader(msHTTPElement, apiParamConfig, httpConfig);
Optional.ofNullable(httpHeader).ifPresent(httpTree::add); Optional.ofNullable(httpHeader).ifPresent(httpTree::add);
//处理host //处理host
DNSCacheManager dnsCacheManager = getEnvDns(msHTTPElement.getName(), envConfig, httpConfig); DNSCacheManager dnsCacheManager = getEnvDns(msHTTPElement.getName(), envConfig, httpConfig);
Optional.ofNullable(dnsCacheManager).ifPresent(httpTree::add); Optional.ofNullable(dnsCacheManager).ifPresent(httpTree::add);
@ -111,6 +112,21 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
parseChild(httpTree, msHTTPElement, config); parseChild(httpTree, msHTTPElement, config);
} }
/**
* 判断是否启用环境
* 非自定义请求启用
* 自定义请求需要判断是否启用
* @param httpElement
* @return
*/
private boolean isEnvEnable(MsHTTPElement httpElement) {
if (BooleanUtils.isTrue(httpElement.getCustomizeRequest())) {
return BooleanUtils.isTrue(httpElement.getCustomizeRequestEnvEnable());
} else {
return true;
}
}
/** /**
* 设置超时时间等配置 * 设置超时时间等配置
@ -341,12 +357,10 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
* 获取环境 http 配置 * 获取环境 http 配置
* *
* @param msHTTPElement * @param msHTTPElement
* @param config * @param envConfig
* @return * @return
*/ */
private HttpConfig getHttpConfig(MsHTTPElement msHTTPElement, ApiParamConfig config) { private HttpConfig getHttpConfig(MsHTTPElement msHTTPElement, EnvironmentInfoDTO envConfig) {
ApiParamConfig apiParamConfig = config;
EnvironmentInfoDTO envConfig = apiParamConfig.getEnvConfig();
if (envConfig == null) { if (envConfig == null) {
return null; return null;
} }