refactor(接口测试): 重构接口请求体数据结构

This commit is contained in:
AgAngle 2023-12-01 14:33:12 +08:00 committed by Craftsman
parent c373662d19
commit b4d8149181
9 changed files with 62 additions and 56 deletions

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
/**
@ -8,10 +7,8 @@ import lombok.Data;
* @CreateTime: 2023-11-06 18:25
*/
@Data
@JsonTypeName("BINARY")
public class BinaryBody extends Body {
// todo 如果fileName能直接定义到文件就不需要filePath
private String filePath;
public class BinaryBody {
private String fileId;
private String fileName;
private String description;
}

View File

@ -1,7 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import lombok.Data;
/**
@ -9,15 +7,27 @@ import lombok.Data;
* @CreateTime: 2023-11-06 16:59
*/
@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "bodyType")
@JsonSubTypes({
@JsonSubTypes.Type(value = NoneBody.class),
@JsonSubTypes.Type(value = FormDataBody.class),
@JsonSubTypes.Type(value = WWWFormBody.class),
@JsonSubTypes.Type(value = JsonBody.class),
@JsonSubTypes.Type(value = XmlBody.class),
@JsonSubTypes.Type(value = RawBody.class),
@JsonSubTypes.Type(value = BinaryBody.class)
})
public abstract class Body {
public class Body {
/**
* 当前选择的请求体类型
* @see BodyType
* 同时持久化多个类型的请求体
*/
private String bodyType;
private NoneBody noneBody;
private FormDataBody formDataBody;
private WWWFormBody wwwFormBody;
private JsonBody jsonBody;
private XmlBody xmlBody;
private RawBody rawBody;
private BinaryBody binaryBody;
public enum BodyType {
BINARY,
FORM_DATA,
NONE,
RAW,
WWW_FORM,
XML
}
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
import java.util.List;
@ -10,7 +9,6 @@ import java.util.List;
* @CreateTime: 2023-11-06 16:59
*/
@Data
@JsonTypeName("FORM_DATA")
public class FormDataBody extends Body {
public class FormDataBody {
private List<FormDataKV> fromValues;
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
/**
@ -8,7 +7,18 @@ import lombok.Data;
* @CreateTime: 2023-11-06 18:25
*/
@Data
@JsonTypeName("JSON")
public class JsonBody extends Body {
private String value;
public class JsonBody {
/**
* 是否启用 json-schema
*/
private Boolean enableJsonSchema;
/**
* 没有启用 json-schema 时的 json 参数值
*/
private String jsonValue;
/**
* 启用 json-schema 时的参数对象
* todo json-schema 编辑器待调研暂时使用 Object 类型
*/
private Object jsonSchema;
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
/**
@ -8,6 +7,5 @@ import lombok.Data;
* @CreateTime: 2023-11-06 18:25
*/
@Data
@JsonTypeName("NONE")
public class NoneBody extends Body {
public class NoneBody {
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
/**
@ -8,7 +7,6 @@ import lombok.Data;
* @CreateTime: 2023-11-06 18:25
*/
@Data
@JsonTypeName("RAW")
public class RawBody extends Body {
public class RawBody {
private String value;
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
import java.util.List;
@ -10,7 +9,6 @@ import java.util.List;
* @CreateTime: 2023-11-06 16:59
*/
@Data
@JsonTypeName("WWW_FORM")
public class WWWFormBody extends Body {
public class WWWFormBody {
private List<FormDataKV> fromValues;
}

View File

@ -1,6 +1,5 @@
package io.metersphere.sdk.dto.api.request.http.body;
import com.fasterxml.jackson.annotation.JsonTypeName;
import lombok.Data;
/**
@ -8,7 +7,6 @@ import lombok.Data;
* @CreateTime: 2023-11-06 18:25
*/
@Data
@JsonTypeName("XML")
public class XmlBody extends Body {
public class XmlBody {
private String value;
}

View File

@ -1,5 +1,4 @@
package io.metersphere.api.controller;
import io.metersphere.sdk.dto.api.request.http.body.Body;
import io.metersphere.api.dto.definition.HttpResponse;
import io.metersphere.sdk.util.ApiDataUtils;
@ -37,7 +36,8 @@ public class MsHTTPElementTest {
MsHTTPElement msHTTPElement = getMsHttpElement();
List bodies = new ArrayList<>();
Body body = new Body();
body.setBodyType(Body.BodyType.FORM_DATA.name());
FormDataBody formDataBody = new FormDataBody();
FormDataKV formDataKV = new FormDataKV();
@ -52,38 +52,35 @@ public class MsHTTPElementTest {
formDataKV.setValue("value");
formDataKV.setKey("key");
formDataBody.setFromValues(List.of(formDataKV));
bodies.add(formDataBody);
body.setFormDataBody(formDataBody);
WWWFormBody wwwFormBody = new WWWFormBody();
wwwFormBody.setFromValues(List.of(formDataKV));
bodies.add(wwwFormBody);
body.setWwwFormBody(wwwFormBody);
JsonBody jsonBody = new JsonBody();
jsonBody.setValue("{}");
bodies.add(jsonBody);
jsonBody.setJsonSchema("{}");
body.setJsonBody(jsonBody);
bodies.add(new NoneBody());
body.setNoneBody(new NoneBody());
RawBody rawBody = new RawBody();
rawBody.setValue("A");
bodies.add(rawBody);
body.setRawBody(rawBody);
XmlBody xmlBody = new XmlBody();
xmlBody.setValue("<a/>");
bodies.add(xmlBody);
body.setXmlBody(xmlBody);
BinaryBody binaryBody = new BinaryBody();
binaryBody.setFilePath("/test/a.png");
binaryBody.setFileId("sdfsf2222");
binaryBody.setFileName("a.png");
bodies.add(binaryBody);
body.setBinaryBody(binaryBody);
for (Object body : bodies) {
msHTTPElement.setBody((Body) body);
String json = ApiDataUtils.toJSONString(msHTTPElement);
Assertions.assertNotNull(json);
Assertions.assertEquals(ApiDataUtils.parseObject(json, AbstractMsTestElement.class), msHTTPElement);
}
msHTTPElement.setBody(body);
String json = ApiDataUtils.toJSONString(msHTTPElement);
Assertions.assertNotNull(json);
Assertions.assertEquals(ApiDataUtils.parseObject(json, AbstractMsTestElement.class), msHTTPElement);
}
@Test
@ -297,7 +294,9 @@ public class MsHTTPElementTest {
formDataKV.setValue("value");
formDataKV.setKey("key");
formDataBody.setFromValues(List.of(formDataKV));
httpResponse.setBody(formDataBody);
Body body = new Body();
body.setBodyType(Body.BodyType.FORM_DATA.name());
httpResponse.setBody(body);
httpResponses.add(httpResponse);
return httpResponses;