From df9417cb08f39ae774bc4cf3b3271fa27b6317f8 Mon Sep 17 00:00:00 2001 From: MeterSphere Bot <78466014+metersphere-bot@users.noreply.github.com> Date: Wed, 13 Jul 2022 18:42:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9tcp=E7=BC=96=E7=A0=81=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98=20(#15715)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1014739 --user=王孝刚 [接口测试]github#15582TCP 请求选择 GBK 编码进行调试,编码未生效 https://www.tapd.cn/55049933/s/1199479 Co-authored-by: wxg0103 <727495428@qq.com> --- .../request/sampler/MsTCPSampler.java | 40 ++++--------------- 1 file changed, 8 insertions(+), 32 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 88c78fcdf2..1bd688475d 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 @@ -38,6 +38,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.modifiers.UserParameters; +import org.apache.jmeter.protocol.tcp.sampler.MsTCPClientImpl; import org.apache.jmeter.protocol.tcp.sampler.TCPSampler; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; @@ -46,7 +47,6 @@ 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.List; import java.util.Random; @@ -159,7 +159,7 @@ public class MsTCPSampler extends MsTestElement { } if (config.getConfig() == null) { // 单独接口执行 - if(StringUtils.isNotEmpty(config.getProjectId())) { + if (StringUtils.isNotEmpty(config.getProjectId())) { this.setProjectId(config.getProjectId()); } config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(this.getEnvironmentId()) ? this.getEnvironmentId() : useEnvironment, this.getProjectId(), this.isMockEnvironment())); @@ -294,11 +294,15 @@ public class MsTCPSampler extends MsTestElement { ElementUtil.setBaseParams(tcpSampler, this.getParent(), config, this.getId(), this.getIndex()); tcpSampler.setProperty(TestElement.TEST_CLASS, TCPSampler.class.getName()); tcpSampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TCPSamplerGui")); - if(StringUtils.isEmpty(this.getClassname())){ + if (StringUtils.isEmpty(this.getClassname())) { tcpSampler.setClassname("TCPClientImpl"); - }else{ + } else { tcpSampler.setClassname(this.getClassname()); } + if (StringUtils.equals("TCPClientImpl", this.getClassname())) { + tcpSampler.setClassname(MsTCPClientImpl.class.getCanonicalName()); + } + tcpSampler.setCharset(this.getConnectEncoding()); tcpSampler.setServer(this.getServer()); tcpSampler.setPort(this.getPort()); tcpSampler.setConnectTimeout(this.getCtimeout()); @@ -322,22 +326,7 @@ public class MsTCPSampler extends MsTestElement { } String value = this.getRequest(); - if (StringUtils.isNotEmpty(this.getConnectEncoding())) { - if (StringUtils.equalsIgnoreCase("utf-8", this.getConnectEncoding())) { - try { - value = new String(value.getBytes(), StandardCharsets.UTF_8); - } catch (Exception e) { - } - } 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; @@ -358,19 +347,6 @@ public class MsTCPSampler extends MsTestElement { String value = item.getValue(); if (StringUtils.isNotEmpty(value)) { value = this.formatMockValue(value); - if (StringUtils.isNotEmpty(this.getConnectEncoding())) { - if (StringUtils.equalsIgnoreCase("utf-8", this.getConnectEncoding())) { - try { - value = new String(value.getBytes(), StandardCharsets.UTF_8); - } catch (Exception e) { - } - } 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)); } });