fix(接口定义): 修改tcp编码不生效的问题 (#15714)

--bug=1014739 --user=王孝刚 [接口测试]github#15582TCP 请求选择 GBK 编码进行调试,编码未生效
https://www.tapd.cn/55049933/s/1199479

Co-authored-by: wxg0103 <727495428@qq.com>
This commit is contained in:
MeterSphere Bot 2022-07-13 15:32:38 +08:00 committed by GitHub
parent b4474f6879
commit 09efdf1119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 32 deletions

View File

@ -38,6 +38,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.ConfigTestElement; import org.apache.jmeter.config.ConfigTestElement;
import org.apache.jmeter.modifiers.UserParameters; 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.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; 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.HashTree;
import org.apache.jorphan.collections.ListedHashTree; import org.apache.jorphan.collections.ListedHashTree;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -299,6 +299,10 @@ public class MsTCPSampler extends MsTestElement {
} else { } else {
tcpSampler.setClassname(this.getClassname()); tcpSampler.setClassname(this.getClassname());
} }
if (StringUtils.equals("TCPClientImpl", this.getClassname())) {
tcpSampler.setClassname(MsTCPClientImpl.class.getCanonicalName());
}
tcpSampler.setCharset(this.getConnectEncoding());
tcpSampler.setServer(this.getServer()); tcpSampler.setServer(this.getServer());
tcpSampler.setPort(this.getPort()); tcpSampler.setPort(this.getPort());
tcpSampler.setConnectTimeout(this.getCtimeout()); tcpSampler.setConnectTimeout(this.getCtimeout());
@ -322,22 +326,7 @@ public class MsTCPSampler extends MsTestElement {
} }
String value = this.getRequest(); 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.setRequestData(value);
tcpSampler.setProperty(ConfigTestElement.USERNAME, this.getUsername()); tcpSampler.setProperty(ConfigTestElement.USERNAME, this.getUsername());
tcpSampler.setProperty(ConfigTestElement.PASSWORD, this.getPassword()); tcpSampler.setProperty(ConfigTestElement.PASSWORD, this.getPassword());
return tcpSampler; return tcpSampler;
@ -358,19 +347,6 @@ public class MsTCPSampler extends MsTestElement {
String value = item.getValue(); String value = item.getValue();
if (StringUtils.isNotEmpty(value)) { if (StringUtils.isNotEmpty(value)) {
value = this.formatMockValue(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)); threadValues.add(new StringProperty(new Integer(new Random().nextInt(1000000)).toString(), value));
} }
}); });