fix: json的请求体mockjs报错问题
This commit is contained in:
parent
6bdb0e7386
commit
2e61cd1e88
|
@ -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());
|
||||
}
|
||||
// 请求体
|
||||
|
|
|
@ -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) {
|
||||
if (StringUtils.isNotBlank((String) value)) {
|
||||
value = ScriptEngineUtils.calculate((String) value);
|
||||
}
|
||||
jsonObject.put(key, value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue