From 214a6385fee2e1b4b42844aea0861350d4354423 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 4 Jun 2021 10:41:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20TCP=E8=AF=B7=E6=B1=82=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BA=A4=E4=BA=92=E5=92=8C=E5=B1=95=E7=A4=BA=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接口定义TCP请求的交互和展示优化,TCP设置增加编码设置,TCP表头的请求类型改为协议方法,ESB报文增加Mock参数 --- .../request/sampler/MsTCPSampler.java | 59 ++++++++++++++++++- .../api/service/EsbApiParamService.java | 2 - .../scenario/api/RelevanceApiList.vue | 9 ++- .../complete/EditCompleteTCPApi.vue | 12 +++- .../components/complete/TCPBasicApi.vue | 16 +---- .../definition/components/list/ApiList.vue | 15 ++++- .../request/http/ApiHttpRequestForm.vue | 1 - .../request/tcp/TcpBasisParameters.vue | 24 +++++++- frontend/src/business/components/xpack | 2 +- frontend/src/i18n/en-US.js | 2 + frontend/src/i18n/zh-CN.js | 2 + frontend/src/i18n/zh-TW.js | 2 + 12 files changed, 119 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index a3629274b9..de7b0c76c5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -20,6 +20,7 @@ import io.metersphere.commons.constants.DelimiterConstants; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.ScriptEngineUtils; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -35,10 +36,13 @@ import org.apache.jmeter.testelement.property.StringProperty; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Data @EqualsAndHashCode(callSuper = true) @@ -82,6 +86,9 @@ public class MsTCPSampler extends MsTestElement { private String protocol = "TCP"; @JSONField(ordinal = 39) private String projectId; + @JSONField(ordinal = 40) + private String connectEncoding; + /** * 新加两个参数,场景保存/修改时需要的参数。不会传递JMeter,只是用于最后的保留。 @@ -195,7 +202,21 @@ public class MsTCPSampler extends MsTestElement { tcpSampler.setCloseConnection(String.valueOf(this.isCloseConnection())); tcpSampler.setSoLinger(this.getSoLinger()); tcpSampler.setEolByte(this.getEolByte()); - tcpSampler.setRequestData(this.getRequest()); + + String value = this.getRequest(); + if(StringUtils.isNotEmpty(this.getConnectEncoding())){ + if(StringUtils.equalsIgnoreCase("utf-8",this.getConnectEncoding())){ + value = new String(value.getBytes(),StandardCharsets.UTF_8); + }else if(StringUtils.equalsIgnoreCase("gbk",this.getConnectEncoding())){ + try { + value = new String(value.getBytes(),"GBK"); + }catch (Exception e){ + } + + } + } + tcpSampler.setRequestData(value); + tcpSampler.setProperty(ConfigTestElement.USERNAME, this.getUsername()); tcpSampler.setProperty(ConfigTestElement.PASSWORD, this.getPassword()); return tcpSampler; @@ -213,7 +234,19 @@ public class MsTCPSampler extends MsTestElement { if (CollectionUtils.isNotEmpty(this.parameters)) { this.parameters.forEach(item -> { names.add(new StringProperty(new Integer(new Random().nextInt(1000000)).toString(), item.getName())); - threadValues.add(new StringProperty(new Integer(new Random().nextInt(1000000)).toString(), item.getValue())); + String value = item.getValue(); + value = this.formatMockValue(value); + if(StringUtils.isNotEmpty(this.getConnectEncoding())){ + if(StringUtils.equalsIgnoreCase("utf-8",this.getConnectEncoding())){ + value = new String(value.getBytes(),StandardCharsets.UTF_8); + }else if(StringUtils.equalsIgnoreCase("gbk",this.getConnectEncoding())){ + try { + value = new String(value.getBytes(),"GBK"); + }catch (Exception e){ + } + } + } + threadValues.add(new StringProperty(new Integer(new Random().nextInt(1000000)).toString(), value)); }); } userParameters.setNames(new CollectionProperty(UserParameters.NAMES, names)); @@ -222,7 +255,29 @@ public class MsTCPSampler extends MsTestElement { userParameters.setThreadLists(new CollectionProperty(UserParameters.THREAD_VALUES, collectionPropertyList)); tree.add(userParameters); } + private String formatMockValue(String value) { + String patten = ">@[^>@]+ 3){ + findStr = findStr.substring(1,findStr.length()-2); + String replaceStr = ScriptEngineUtils.calculate(findStr); + if(StringUtils.equals(findStr,replaceStr)){ + replaceStr = ""; + } + value = value.replace(">"+findStr+""+replaceStr+" list = esbApiParamsMapper.selectByExampleWithBLOBs(example); - if (list.isEmpty()) { String uuid = UUID.randomUUID().toString(); model = new EsbApiParamsWithBLOBs(); diff --git a/frontend/src/business/components/api/automation/scenario/api/RelevanceApiList.vue b/frontend/src/business/components/api/automation/scenario/api/RelevanceApiList.vue index e01ec31f1d..d0c18e4bf3 100644 --- a/frontend/src/business/components/api/automation/scenario/api/RelevanceApiList.vue +++ b/frontend/src/business/components/api/automation/scenario/api/RelevanceApiList.vue @@ -41,7 +41,7 @@ sortable="custom" column-key="method" :filters="methodFilters" - :label="$t('api_test.definition.api_type')" + :label="getApiRequestTypeName" width="120px">