fix(接口测试): 修复接口定义导入jmx文件选择编码,ms不显示编码的缺陷 (#16573)

--bug=1015574 --user=王孝刚 【接口测试】github#16513,导入jmeter文件Parameter使用URL
Encode时, 导入后没有勾选上Encode https://www.tapd.cn/55049933/s/1217011

Co-authored-by: wxg0103 <727495428@qq.com>
This commit is contained in:
MeterSphere Bot 2022-08-02 16:43:37 +08:00 committed by GitHub
parent c0bb152525
commit 312a352774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 12 deletions

View File

@ -63,6 +63,7 @@ import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
@ -71,6 +72,7 @@ import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree;
@ -88,6 +90,7 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
* todo 存放单个请求下的Header 为了和平台对应
*/
private Map<Integer, List<Object>> headerMap = new HashMap<>();
private static final String ALWAYS_ENCODE = "HTTPArgument.always_encode";
@Override
public ScenarioImport parse(InputStream inputSource, ApiTestImportRequest request) {
@ -291,16 +294,18 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
samplerProxy.getBody().initKvs();
} else if (source.getDoMultipart()) {
samplerProxy.getBody().setType(Body.FORM_DATA);
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
samplerProxy.getBody().getKvs().add(keyValue);
});
} else if (StringUtils.isNotEmpty(bodyType) ||
(source.getMethod().equalsIgnoreCase("POST") &&
MapUtils.isNotEmpty(source.getArguments().getArgumentsAsMap()))) {
samplerProxy.getBody().setType(Body.WWW_FROM);
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
samplerProxy.getBody().getKvs().add(keyValue);
});
} else if (samplerProxy.getBody() != null && samplerProxy.getBody().getType().equals(Body.FORM_DATA)) {
@ -310,8 +315,9 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
});
} else {
List<KeyValue> keyValues = new LinkedList<>();
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
keyValues.add(keyValue);
});
if (CollectionUtils.isNotEmpty(keyValues)) {
@ -337,6 +343,20 @@ public class MsJmeterParser extends ApiImportAbstractParser<ScenarioImport> {
}
}
private void parseParams(JMeterProperty params, KeyValue keyValue) {
if (params == null || keyValue == null) {
return;
}
Object objValue = params.getObjectValue();
if (objValue instanceof HTTPArgument) {
HTTPArgument argument = (HTTPArgument) objValue;
boolean propertyAsBoolean = argument.getPropertyAsBoolean(ALWAYS_ENCODE);
keyValue.setUrlEncode(propertyAsBoolean);
keyValue.setName(argument.getName());
keyValue.setValue(argument.getValue());
}
}
private void convertTCPSampler(MsTCPSampler msTCPSampler, TCPSampler tcpSampler) {
msTCPSampler.setName(tcpSampler.getName());
msTCPSampler.setType("TCPSampler");

View File

@ -55,6 +55,7 @@ import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler;
@ -62,6 +63,7 @@ import org.apache.jmeter.protocol.tcp.sampler.TCPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.timers.ConstantTimer;
import org.apache.jorphan.collections.HashTree;
@ -79,6 +81,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
private String planName = "default";
private static final Integer GROUP_GLOBAL = 1;
private static final String ALWAYS_ENCODE = "HTTPArgument.always_encode";
@Override
public ApiDefinitionImport parse(InputStream inputSource, ApiTestImportRequest request) {
@ -742,19 +745,22 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
samplerProxy.getBody().initKvs();
} else if (StringUtils.isNotEmpty(bodyType) || ("POST".equalsIgnoreCase(source.getMethod()) && source.getArguments().getArgumentsAsMap().size() > 0)) {
samplerProxy.getBody().setType(Body.WWW_FROM);
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
samplerProxy.getBody().getKvs().add(keyValue);
});
} else if (samplerProxy.getBody() != null && samplerProxy.getBody().getType().equals(Body.FORM_DATA)) {
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
samplerProxy.getBody().getKvs().add(keyValue);
});
} else {
List<KeyValue> keyValues = new LinkedList<>();
source.getArguments().getArgumentsAsMap().forEach((k, v) -> {
KeyValue keyValue = new KeyValue(k, v);
source.getArguments().getArguments().forEach(params -> {
KeyValue keyValue = new KeyValue();
parseParams(params, keyValue);
keyValues.add(keyValue);
});
if (CollectionUtils.isNotEmpty(keyValues)) {
@ -775,6 +781,20 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
}
}
private void parseParams(JMeterProperty params, KeyValue keyValue) {
if (params == null || keyValue == null) {
return;
}
Object objValue = params.getObjectValue();
if (objValue instanceof HTTPArgument) {
HTTPArgument argument = (HTTPArgument) objValue;
boolean propertyAsBoolean = argument.getPropertyAsBoolean(ALWAYS_ENCODE);
keyValue.setUrlEncode(propertyAsBoolean);
keyValue.setName(argument.getName());
keyValue.setValue(argument.getValue());
}
}
private String getBodyType(List<KeyValue> headers) {
if (CollectionUtils.isNotEmpty(headers)) {
List<KeyValue> keyValues = headers.stream().filter(keyValue -> "Content-Type".equals(keyValue.getName()) && "application/x-www-form-urlencoded".equals(keyValue.getValue())).collect(Collectors.toList());