From 312a352774fbb3279e8e009446146a8ba94bb34b Mon Sep 17 00:00:00 2001 From: MeterSphere Bot <78466014+metersphere-bot@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:43:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=E5=AF=BC?= =?UTF-8?q?=E5=85=A5jmx=E6=96=87=E4=BB=B6=E9=80=89=E6=8B=A9=E7=BC=96?= =?UTF-8?q?=E7=A0=81=EF=BC=8Cms=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E7=BC=BA=E9=99=B7=20(#16573)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1015574 --user=王孝刚 【接口测试】github#16513,导入jmeter文件Parameter使用URL Encode时, 导入后没有勾选上Encode https://www.tapd.cn/55049933/s/1217011 Co-authored-by: wxg0103 <727495428@qq.com> --- .../dto/automation/parse/MsJmeterParser.java | 32 +++++++++++++++---- .../parse/JmeterDefinitionParser.java | 32 +++++++++++++++---- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java index 9cd7cac04e..577f13ede5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java @@ -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 { * todo 存放单个请求下的Header 为了和平台对应 */ private Map> 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 { 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 { }); } else { List 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 { } } + 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"); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/JmeterDefinitionParser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/JmeterDefinitionParser.java index 028e28b766..a02050aaa2 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/JmeterDefinitionParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/JmeterDefinitionParser.java @@ -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 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 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 headers) { if (CollectionUtils.isNotEmpty(headers)) { List keyValues = headers.stream().filter(keyValue -> "Content-Type".equals(keyValue.getName()) && "application/x-www-form-urlencoded".equals(keyValue.getValue())).collect(Collectors.toList());