From 8a4315b5cc2112496f4bcfd3c4f0a01dc8d38bf1 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 10 Feb 2022 13:26:05 +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=8Dhttp=20get=E8=AF=B7=E6=B1=82JSON=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8F=82=E6=95=B0=E6=97=A0=E6=B3=95=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1010169 --user=赵勇 【接口测试】github#10327,自动化下接口的query参数选择“编码”,实际发出的请求并未转义 https://www.tapd.cn/55049933/s/1102562 --- .../request/sampler/MsHTTPSamplerProxy.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index c27da22931..734ed95ffd 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -447,7 +447,12 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setProperty("HTTPSampler.path", envPath); } if (CollectionUtils.isNotEmpty(this.getArguments())) { - String path = getPostQueryParameters(envPath); + String path = envPath; + if (StringUtils.equalsIgnoreCase(this.getMethod(), "GET")) { + getQueryParameters(sampler); + } else { + path = postQueryParameters(envPath); + } if (HTTPConstants.DELETE.equals(this.getMethod()) && !path.startsWith("${")) { if (!path.startsWith("/")) { path = "/" + path; @@ -476,7 +481,6 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { sampler.setProperty("HTTPSampler.port", this.getPort()); - } else { sampler.setPort(urlObject.getPort()); } @@ -488,7 +492,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setProperty("HTTPSampler.path", envPath); } if (CollectionUtils.isNotEmpty(this.getArguments())) { - sampler.setProperty("HTTPSampler.path", getPostQueryParameters(URLDecoder.decode(URLEncoder.encode(envPath, "UTF-8"), "UTF-8"))); + if (StringUtils.equalsIgnoreCase(this.getMethod(), "GET")) { + getQueryParameters(sampler); + } else { + sampler.setProperty("HTTPSampler.path", postQueryParameters(URLDecoder.decode(URLEncoder.encode(envPath, "UTF-8"), "UTF-8"))); + } } } } catch (Exception e) { @@ -629,7 +637,14 @@ public class MsHTTPSamplerProxy extends MsTestElement { return path; } - private String getPostQueryParameters(String path) { + private void getQueryParameters(HTTPSamplerProxy sampler) { + Arguments arguments = httpArguments(this.getArguments()); + if (arguments != null && !arguments.getArguments().isEmpty()) { + sampler.setArguments(arguments); + } + } + + private String postQueryParameters(String path) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(path); if (path.indexOf("?") != -1) { @@ -658,22 +673,22 @@ public class MsHTTPSamplerProxy extends MsTestElement { list.stream(). filter(KeyValue::isValid). filter(KeyValue::isEnable).forEach(keyValue -> { - try { - String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); - if (keyValue.getValue() == null) { - httpArgument.setValue(""); - } - httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); - if (StringUtils.isNotBlank(keyValue.getContentType())) { - httpArgument.setContentType(keyValue.getContentType()); - } - arguments.addArgument(httpArgument); - } catch (Exception e) { - - } + try { + String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); + if (keyValue.getValue() == null) { + httpArgument.setValue(""); } - ); + httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); + if (StringUtils.isNotBlank(keyValue.getContentType())) { + httpArgument.setContentType(keyValue.getContentType()); + } + arguments.addArgument(httpArgument); + } catch (Exception e) { + + } + } + ); return arguments; }