From 48c69444c7130f3a9a93636e04bbd2c13d2b2652 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 9 Dec 2020 12:25:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89):=20?= =?UTF-8?q?=E9=83=A8=E5=88=86=E7=BC=BA=E9=99=B7=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/request/MsTestElement.java | 2 +- .../request/sampler/MsHTTPSamplerProxy.java | 36 +++++++++++++++---- .../automation/scenario/ApiScenarioList.vue | 20 ++++++----- .../automation/scenario/EditApiScenario.vue | 2 +- .../automation/scenario/ImportApiScenario.vue | 1 + .../api/definition/ApiDefinition.vue | 4 +-- .../api/definition/components/ApiCaseList.vue | 3 ++ .../api/definition/components/ApiList.vue | 11 ++++-- frontend/src/i18n/en-US.js | 1 + frontend/src/i18n/zh-CN.js | 1 + frontend/src/i18n/zh-TW.js | 1 + 11 files changed, 60 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java index 3c359e917c..71bcb99f86 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java @@ -102,7 +102,7 @@ public class MsTestElement { public HashTree generateHashTree() { HashTree jmeterTestPlanHashTree = new ListedHashTree(); - this.toHashTree(jmeterTestPlanHashTree, this.hashTree, null); + this.toHashTree(jmeterTestPlanHashTree, this.hashTree, new ParameterConfig()); return jmeterTestPlanHashTree; } 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 2833c5cacf..5106f1209d 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 @@ -28,7 +28,11 @@ import org.apache.jorphan.collections.HashTree; import java.net.URL; import java.net.URLDecoder; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Data @EqualsAndHashCode(callSuper = true) @@ -112,8 +116,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { envPath += this.getPath(); } if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) { - sampler.setPath(getRestParameters(URLDecoder.decode(envPath, "UTF-8"))); - } else { + envPath = getRestParameters(URLDecoder.decode(envPath, "UTF-8")); + sampler.setPath(envPath); + } + if (CollectionUtils.isNotEmpty(this.getArguments())) { sampler.setPath(getPostQueryParameters(URLDecoder.decode(envPath, "UTF-8"))); } } else { @@ -128,19 +134,22 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) { sampler.setPath(getRestParameters(URLDecoder.decode(urlObject.getPath(), "UTF-8"))); - } else { + } + if (CollectionUtils.isNotEmpty(this.getArguments())) { sampler.setPath(getPostQueryParameters(URLDecoder.decode(urlObject.getPath(), "UTF-8"))); } } } catch (Exception e) { LogUtil.error(e); } - + // REST参数 + if (CollectionUtils.isNotEmpty(this.getArguments())) { + sampler.setArguments(httpArguments(this.getRest())); + } // 请求参数 if (CollectionUtils.isNotEmpty(this.getArguments())) { sampler.setArguments(httpArguments(this.getArguments())); } - // 请求体 if (!StringUtils.equals(this.getMethod(), "GET")) { List bodyParams = this.body.getBodyParams(sampler, this.getId()); @@ -168,10 +177,22 @@ public class MsHTTPSamplerProxy extends MsTestElement { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(path); stringBuffer.append("/"); + Map keyValueMap = new HashMap<>(); this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue -> - stringBuffer.append(keyValue.getValue()).append("/") + keyValueMap.put(keyValue.getName(), keyValue.getValue()) ); - return stringBuffer.substring(0, stringBuffer.length() - 1); + + Pattern p = Pattern.compile("(\\{)([\\w]+)(\\})"); + Matcher m = p.matcher(path); + StringBuffer sb = new StringBuffer(); + while (m.find()) { + String group = m.group(2); + //替换并且把替换好的值放到sb中 + m.appendReplacement(sb, keyValueMap.get(group)); + } + //把符合的数据追加到sb尾 + m.appendTail(sb); + return sb.toString(); } private String getPostQueryParameters(String path) { @@ -214,4 +235,5 @@ public class MsHTTPSamplerProxy extends MsTestElement { private boolean isRest() { return this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).toArray().length > 0; } + } diff --git a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue index faccabd1b5..cbf6fcdbe3 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue @@ -1,6 +1,6 @@