mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 20:19:16 +08:00
fix(接口测试): 修复导入postman文件,x-www-form-urlencoded格式的请求体没显示问题
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001018159 --user=郭雨琦
This commit is contained in:
parent
21594843d4
commit
875ef58d96
@ -1,6 +1,8 @@
|
|||||||
package io.metersphere.api.parse;
|
package io.metersphere.api.parse;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
|
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
|
||||||
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
||||||
import io.metersphere.api.dto.scenario.Body;
|
import io.metersphere.api.dto.scenario.Body;
|
||||||
@ -189,18 +191,18 @@ public abstract class HarScenarioAbstractParser<T> extends ApiImportAbstractPars
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void parseBody(Body body, PostmanRequest requestDesc) {
|
private void parseBody(Body body, PostmanRequest requestDesc) {
|
||||||
JSONObject postmanBody = requestDesc.getBody();
|
ObjectNode postmanBody = requestDesc.getBody();
|
||||||
if (postmanBody == null) {
|
if (postmanBody == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String bodyMode = postmanBody.optString("mode");
|
String bodyMode = postmanBody.get("mode").textValue();
|
||||||
if (StringUtils.isBlank(bodyMode)) {
|
if (StringUtils.isBlank(bodyMode)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
|
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
|
||||||
parseRawBody(body, postmanBody, bodyMode);
|
parseRawBody(body, postmanBody, bodyMode);
|
||||||
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
|
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
|
||||||
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.optString(bodyMode), PostmanKeyValue.class);
|
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.get(bodyMode).textValue(), PostmanKeyValue.class);
|
||||||
body.setKvs(parseKeyValue(postmanKeyValues));
|
body.setKvs(parseKeyValue(postmanKeyValues));
|
||||||
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
|
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
|
||||||
body.setType(Body.FORM_DATA);
|
body.setType(Body.FORM_DATA);
|
||||||
@ -218,15 +220,15 @@ public abstract class HarScenarioAbstractParser<T> extends ApiImportAbstractPars
|
|||||||
return XMLUtil.jsonToXmlStr(object);
|
return XMLUtil.jsonToXmlStr(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
|
private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) {
|
||||||
body.setRaw(postmanBody.optString(bodyMode));
|
body.setRaw(postmanBody.get(bodyMode).textValue());
|
||||||
body.setType(MsRequestBodyType.RAW.value());
|
body.setType(MsRequestBodyType.RAW.value());
|
||||||
JSONObject options = postmanBody.optJSONObject("options");
|
JsonNode options = postmanBody.get("options");
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value());
|
JsonNode raw = options.get(PostmanRequestBodyMode.RAW.value());
|
||||||
if (raw != null) {
|
if (raw != null) {
|
||||||
String bodyType = "";
|
String bodyType = "";
|
||||||
switch (raw.optString("language")) {
|
switch (raw.get("language").textValue()) {
|
||||||
case "json":
|
case "json":
|
||||||
bodyType = Body.JSON_STR;
|
bodyType = Body.JSON_STR;
|
||||||
break;
|
break;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package io.metersphere.api.parse;
|
package io.metersphere.api.parse;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
||||||
import io.metersphere.api.dto.definition.response.HttpResponse;
|
import io.metersphere.api.dto.definition.response.HttpResponse;
|
||||||
import io.metersphere.api.dto.scenario.Body;
|
import io.metersphere.api.dto.scenario.Body;
|
||||||
@ -30,7 +32,8 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
|
|||||||
}
|
}
|
||||||
requestDesc.getAuth(); // todo 认证方式等待优化
|
requestDesc.getAuth(); // todo 认证方式等待优化
|
||||||
PostmanUrl url = requestDesc.getUrl();
|
PostmanUrl url = requestDesc.getUrl();
|
||||||
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod(), Optional.ofNullable(requestDesc.getBody()).orElse(new JSONObject()).optString("jsonSchema"));
|
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod(),
|
||||||
|
requestDesc.getBody().get("jsonSchema") == null ? "" : requestDesc.getBody().get("jsonSchema").textValue());
|
||||||
request.setRest(parseKeyValue(requestDesc.getUrl().getVariable()));
|
request.setRest(parseKeyValue(requestDesc.getUrl().getVariable()));
|
||||||
if (StringUtils.isNotBlank(request.getPath())) {
|
if (StringUtils.isNotBlank(request.getPath())) {
|
||||||
String path = request.getPath().split("\\?")[0];
|
String path = request.getPath().split("\\?")[0];
|
||||||
@ -132,18 +135,20 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void parseBody(Body body, PostmanRequest requestDesc) {
|
private void parseBody(Body body, PostmanRequest requestDesc) {
|
||||||
JSONObject postmanBody = requestDesc.getBody();
|
ObjectNode postmanBody = requestDesc.getBody();
|
||||||
if (postmanBody == null) {
|
if (postmanBody == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String bodyMode = postmanBody.optString("mode");
|
String bodyMode = postmanBody.get("mode").textValue();
|
||||||
if (StringUtils.isBlank(bodyMode)) {
|
if (StringUtils.isBlank(bodyMode)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
|
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
|
||||||
parseRawBody(body, postmanBody, bodyMode);
|
parseRawBody(body, postmanBody, bodyMode);
|
||||||
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
|
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
|
||||||
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(parseVariable(postmanBody.optString(bodyMode)), PostmanKeyValue.class);
|
String s1 = postmanBody.get(bodyMode).toString();
|
||||||
|
String s = parseVariable(s1);
|
||||||
|
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(s, PostmanKeyValue.class);
|
||||||
body.setKvs(parseKeyValue(postmanKeyValues));
|
body.setKvs(parseKeyValue(postmanKeyValues));
|
||||||
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
|
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
|
||||||
body.setType(Body.FORM_DATA);
|
body.setType(Body.FORM_DATA);
|
||||||
@ -156,15 +161,15 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
|
private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) {
|
||||||
body.setRaw(parseVariable(postmanBody.optString(bodyMode)));
|
body.setRaw(parseVariable(postmanBody.get(bodyMode).textValue()));
|
||||||
body.setType(MsRequestBodyType.RAW.value());
|
body.setType(MsRequestBodyType.RAW.value());
|
||||||
JSONObject options = postmanBody.optJSONObject("options");
|
JsonNode options = postmanBody.get("options");
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
JSONObject raw = options.optJSONObject(PostmanRequestBodyMode.RAW.value());
|
JsonNode raw = options.get(PostmanRequestBodyMode.RAW.value());
|
||||||
if (raw != null) {
|
if (raw != null) {
|
||||||
String bodyType = "";
|
String bodyType = "";
|
||||||
switch (raw.optString("language")) {
|
switch (raw.get("language").textValue()) {
|
||||||
case "json":
|
case "json":
|
||||||
bodyType = Body.JSON_STR;
|
bodyType = Body.JSON_STR;
|
||||||
break;
|
break;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package io.metersphere.api.parse.postman;
|
package io.metersphere.api.parse.postman;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -12,8 +13,8 @@ public class PostmanRequest {
|
|||||||
private String method;
|
private String method;
|
||||||
private String schema;
|
private String schema;
|
||||||
private List<PostmanKeyValue> header;
|
private List<PostmanKeyValue> header;
|
||||||
private JSONObject body;
|
private ObjectNode body;
|
||||||
private JSONObject auth;
|
private ObjectNode auth;
|
||||||
private PostmanUrl url;
|
private PostmanUrl url;
|
||||||
private String description;
|
private String description;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user