fix(接口测试): 场景使用mock环境执行,url有误

--bug=1045933 --user=陈建星 [接口测试】接口场景中使用MOCK环境执行,请求URL 地址错误 https://www.tapd.cn/55049933/s/1584279
This commit is contained in:
AgAngle 2024-09-24 10:50:16 +08:00 committed by Craftsman
parent 67b86d4487
commit 589dc65212
2 changed files with 9 additions and 9 deletions

View File

@ -584,7 +584,7 @@
order by `pos` desc limit 1;
</select>
<select id="getApiDefinitionExecuteInfo" resultType="io.metersphere.api.dto.ApiDefinitionExecuteInfo">
select id as resource_id, module_id, path, method
select id as resource_id, module_id, path, method, num
from api_definition
where protocol = 'HTTP'
and id in

View File

@ -80,7 +80,7 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
sampler.setMethod(msHTTPElement.getMethod());
// path 设置完整的url
sampler.setPath(getPath(msHTTPElement, httpConfig));
sampler.setPath(getPath(msHTTPElement, httpConfig, envConfig));
setHttpOtherConfig(msHTTPElement.getOtherConfig(), sampler);
@ -182,16 +182,20 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
return authManager;
}
private String getPath(MsHTTPElement msHTTPElement, HttpConfig httpConfig) {
private String getPath(MsHTTPElement msHTTPElement, HttpConfig httpConfig, EnvironmentInfoDTO envConfig) {
String url = msHTTPElement.getPath();
if (httpConfig != null) {
// 接口调试没有环境不取环境的配置
String protocol = httpConfig.getProtocol().toLowerCase();
String hostName = httpConfig.getHostname();
if (hostName.lastIndexOf("/") == hostName.length() - 1) {
if (StringUtils.endsWith(hostName, "/")) {
hostName = hostName.substring(0, hostName.length() - 1);
}
if (url.indexOf("/") == 0) {
// 如果是 mock 返回的url格式是 /mock-server/projectNum/apiNum
if (BooleanUtils.isTrue(envConfig.getMock()) && msHTTPElement.getNum() != null) {
hostName = StringUtils.join(hostName, "/", msHTTPElement.getNum());
}
if (StringUtils.startsWith(url, "/")) {
url = url.substring(1);
}
url = protocol + "://" + (hostName + "/" + url);
@ -360,10 +364,6 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
match = true;
}
if (match) {
// 如果是mock 返回的url格式是 /mock-server/projectNum/apiNum
if (BooleanUtils.isTrue(envConfig.getMock())) {
httpConfig.setHostname(StringUtils.join(httpConfig.getHostname(), "/", msHTTPElement.getNum()));
}
return httpConfig;
}
}