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

--bug=1015574 --user=王孝刚 【接口测试】github#16513,导入jmeter文件Parameter使用URL
Encode时, 导入后没有勾选上Encode https://www.tapd.cn/55049933/s/1217011
This commit is contained in:
wxg0103 2022-08-02 10:17:46 +08:00 committed by f2c-ci-robot[bot]
parent 2f371fcefe
commit 57f04893a4
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

@ -54,6 +54,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;
@ -61,6 +62,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;
@ -78,6 +80,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) {
@ -728,19 +731,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)) {
@ -761,6 +767,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());