diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 28df4de7e0..4f7226b702 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -14,6 +14,7 @@ import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.HttpConfig; import io.metersphere.api.dto.scenario.HttpConfigCondition; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.dto.scenario.environment.CommonConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.ssl.KeyStoreConfig; import io.metersphere.api.dto.ssl.KeyStoreFile; @@ -191,8 +192,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setMethod(this.getMethod()); sampler.setContentEncoding("UTF-8"); - sampler.setConnectTimeout(this.getConnectTimeout() == null ? "6000" : this.getConnectTimeout()); - sampler.setResponseTimeout(this.getResponseTimeout() == null ? "6000" : this.getResponseTimeout()); sampler.setFollowRedirects(this.isFollowRedirects()); sampler.setUseKeepAlive(true); sampler.setDoMultipart(this.isDoMultipartPost()); @@ -204,8 +203,13 @@ public class MsHTTPSamplerProxy extends MsTestElement { compatible(config); + this.initConnectAndResponseTimeout(config); + sampler.setConnectTimeout(this.getConnectTimeout() == null ? "60000" : this.getConnectTimeout()); + sampler.setResponseTimeout(this.getResponseTimeout() == null ? "60000" : this.getResponseTimeout()); + HttpConfig httpConfig = getHttpConfig(config); + setSamplerPath(config, httpConfig, sampler); // 请求体 @@ -271,6 +275,28 @@ public class MsHTTPSamplerProxy extends MsTestElement { } + private void initConnectAndResponseTimeout(ParameterConfig config) { + if (config.isEffective(this.getProjectId())) { + String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid(); + if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) { + this.setEnvironmentId(useEvnId); + } + CommonConfig commonConfig = config.getConfig().get(this.getProjectId()).getCommonConfig(); + if(commonConfig != null){ + if(this.getConnectTimeout() == null || StringUtils.equals(this.getConnectTimeout(),"60000")){ + if(commonConfig.getRequestTimeout() != 0){ + this.setConnectTimeout(String.valueOf(commonConfig.getRequestTimeout())); + } + } + if(this.getResponseTimeout() == null || StringUtils.equals(this.getResponseTimeout(),"60000")){ + if(commonConfig.getResponseTimeout() != 0){ + this.setResponseTimeout(String.valueOf(commonConfig.getResponseTimeout())); + } + } + } + } + } + private EnvironmentConfig getEnvironmentConfig(ParameterConfig config) { return config.getConfig().get(this.getProjectId()); } diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/environment/CommonConfig.java b/backend/src/main/java/io/metersphere/api/dto/scenario/environment/CommonConfig.java index 6670bcfccc..9e8a748d2a 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/environment/CommonConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/environment/CommonConfig.java @@ -10,4 +10,6 @@ public class CommonConfig { private List variables; private boolean enableHost; private List hosts; + private int requestTimeout; + private int responseTimeout; } diff --git a/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java b/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java index 28af94c2c9..1e9b83ddae 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java +++ b/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java @@ -18,23 +18,25 @@ public class TCPPool { public static String createTcp(int port){ String returnString = ""; - TCPServer tcpServer = null; - if(serverSockedMap.containsKey(port)){ - tcpServer = serverSockedMap.get(port); - }else { - tcpServer = new TCPServer(port); - serverSockedMap.put(port,tcpServer); - } - try { - if(!tcpServer.isSocketOpen()){ - Thread t = new Thread(tcpServer); - t.start(); + if(port > 0){ + TCPServer tcpServer = null; + if(serverSockedMap.containsKey(port)){ + tcpServer = serverSockedMap.get(port); + }else { + tcpServer = new TCPServer(port); + serverSockedMap.put(port,tcpServer); + } + try { + if(!tcpServer.isSocketOpen()){ + Thread t = new Thread(tcpServer); + t.start(); + } + returnString = "OK"; + }catch (Exception e){ + returnString = e.getMessage(); + e.printStackTrace(); + MSException.throwException(e.getMessage()); } - returnString = "OK"; - }catch (Exception e){ - returnString = e.getMessage(); - e.printStackTrace(); - MSException.throwException(e.getMessage()); } return returnString; diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index 0c5fc362f0..f85105bd9d 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -35,6 +35,7 @@ import io.metersphere.track.service.TestPlanProjectService; import io.metersphere.track.service.TestPlanService; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -89,6 +90,9 @@ public class ProjectService { @Resource private ScheduleService scheduleService; + @Value("${tcp.mock.port}") + private String tcpMockPorts; + public Project addProject(Project project) { if (StringUtils.isBlank(project.getName())) { MSException.throwException(Translator.get("project_name_is_null")); @@ -273,6 +277,10 @@ public class ProjectService { lastTcpNum = oldData.getMockTcpPort().intValue(); } + if(project.getMockTcpPort().intValue() > 0){ + this.checkMockTcpPort(project.getMockTcpPort().intValue()); + } + this.checkProjectTcpPort(project); project.setCreateTime(null); @@ -294,6 +302,39 @@ public class ProjectService { } } + private void checkMockTcpPort(int port) { + if(StringUtils.isNotEmpty(this.tcpMockPorts)){ + try { + if(this.tcpMockPorts.contains("-")){ + String [] tcpMockPortArr = this.tcpMockPorts.split("-"); + int num1 = Integer.parseInt(tcpMockPortArr[0]); + int num2 = Integer.parseInt(tcpMockPortArr[1]); + + int startNum = num1 > num2 ? num2 : num1; + int endNum = num1 < num2 ? num2 : num1; + + if(port < startNum || port > endNum){ + MSException.throwException("Tcp port is not in ["+this.tcpMockPorts+"]"); + } + }else { + int tcpPortConfigNum = Integer.parseInt(this.tcpMockPorts); + if(port != tcpPortConfigNum){ + MSException.throwException("Tcp port is not equals ["+this.tcpMockPorts+"]"); + } + } + }catch (Exception e){ + String errorMsg = e.getMessage(); + if(!errorMsg.startsWith("Tcp")){ + MSException.throwException("Tcp port config is error!"); + }else { + MSException.throwException(errorMsg); + } + } + }else { + MSException.throwException("Tcp port config is error!"); + } + } + private void checkProjectTcpPort(Project project) { //判断端口是否重复 if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) { diff --git a/frontend/src/business/components/api/test/components/environment/EnvironmentCommonConfig.vue b/frontend/src/business/components/api/test/components/environment/EnvironmentCommonConfig.vue index 861377e721..2300a7868a 100644 --- a/frontend/src/business/components/api/test/components/environment/EnvironmentCommonConfig.vue +++ b/frontend/src/business/components/api/test/components/environment/EnvironmentCommonConfig.vue @@ -1,65 +1,89 @@