From 2e61cd1e88f712b3235e5bfd9cc13ab67c992139 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 29 Apr 2021 18:24:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20json=E7=9A=84=E8=AF=B7=E6=B1=82=E4=BD=93?= =?UTF-8?q?mockjs=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/sampler/MsHTTPSamplerProxy.java | 5 ++- .../io/metersphere/api/dto/scenario/Body.java | 39 ++++++++++++------- 2 files changed, 28 insertions(+), 16 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 f22c15f713..eeedc79765 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 @@ -278,6 +278,9 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { url.replaceAll(this.getPort(), "10990"); } + if (url == null) { + MSException.throwException("请填写请求地址"); + } URL urlObject = new URL(url); sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { @@ -298,7 +301,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } } catch (Exception e) { - LogUtil.error(e); + LogUtil.error(e.getMessage(), e); MSException.throwException(e.getMessage()); } // 请求体 diff --git a/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java b/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java index 39bbf1960f..ebfa88aac5 100644 --- a/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java +++ b/backend/src/main/java/io/metersphere/api/dto/scenario/Body.java @@ -1,11 +1,9 @@ package io.metersphere.api.dto.scenario; import com.alibaba.fastjson.JSONObject; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import io.metersphere.api.dto.scenario.request.BodyFile; -import io.metersphere.commons.json.JSONSchemaGenerator; import io.metersphere.commons.utils.FileUtils; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.ScriptEngineUtils; import lombok.Data; import org.apache.commons.collections4.CollectionUtils; @@ -70,17 +68,7 @@ public class Body { sampler.setDoMultipart(true); } } else { - if (StringUtils.isNotEmpty(this.format) && this.getJsonSchema() != null) { - if("JSON-SCHEMA".equals(this.format)) { - this.raw = JSONSchemaGenerator.getJson(com.alibaba.fastjson.JSON.toJSONString(this.getJsonSchema())); - } else { // json 文本也支持 mock 参数 - JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(this.getRaw()); - jsonMockParse(jsonObject); - // 格式化 json - Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create(); - this.raw = gson.toJson(jsonObject); - } - } + parseJonBodyMock(); KeyValue keyValue = new KeyValue("", "JSON-SCHEMA", this.getRaw(), true, true); sampler.setPostBodyRaw(true); keyValue.setEnable(true); @@ -90,13 +78,34 @@ public class Body { return body; } + private void parseJonBodyMock() { + try { +// if (StringUtils.isNotBlank(this.format) && "JSON-SCHEMA".equals(this.format) ) { +// if(this.getJsonSchema() != null) { +// JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(this.getJsonSchema())); +// jsonMockParse(jsonObject); +// this.setJsonSchema(jsonObject); +// } +// } else + if (StringUtils.isNotBlank(this.type) && StringUtils.equals(this.type, "JSON")) { // json 文本也支持 mock 参数 + JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(this.getRaw()); + jsonMockParse(jsonObject); + this.raw = JSONObject.toJSONString(jsonObject); + } + } catch (Exception e) { + LogUtil.error(e.getMessage(), e); + } + } + private void jsonMockParse(JSONObject jsonObject) { for(String key : jsonObject.keySet()) { Object value = jsonObject.get(key); if(value instanceof JSONObject) { jsonMockParse((JSONObject) value); } else if(value instanceof String) { - value = ScriptEngineUtils.calculate((String) value); + if (StringUtils.isNotBlank((String) value)) { + value = ScriptEngineUtils.calculate((String) value); + } jsonObject.put(key, value); } }