refactor(接口测试): 优化请求体参数
This commit is contained in:
parent
bffd717e93
commit
c8e70124db
|
@ -3,6 +3,8 @@ package io.metersphere.api.dto.request.http;
|
|||
import io.metersphere.api.dto.request.http.auth.HTTPAuth;
|
||||
import io.metersphere.api.dto.request.http.body.Body;
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.metersphere.sdk.constants.HttpMethodConstants;
|
||||
import io.metersphere.system.valid.EnumValue;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
@ -34,9 +36,11 @@ public class MsHTTPElement extends AbstractMsTestElement {
|
|||
private String path;
|
||||
/**
|
||||
* 请求方法
|
||||
* 取值参考:{@link HttpMethodConstants}
|
||||
*/
|
||||
@NotBlank
|
||||
@Size(max = 10)
|
||||
@EnumValue(enumClass = HttpMethodConstants.class)
|
||||
private String method;
|
||||
/**
|
||||
* 请求体
|
||||
|
|
|
@ -13,5 +13,5 @@ public class FormDataBody {
|
|||
/**
|
||||
* form-data 请求体的键值对列表
|
||||
*/
|
||||
private List<FormDataKV> fromValues;
|
||||
private List<FormDataKV> formValues;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.metersphere.api.dto.request.http.body;
|
|||
|
||||
import io.metersphere.api.dto.ApiFile;
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
@ -22,6 +23,11 @@ public class FormDataKV extends WWWFormKV {
|
|||
*/
|
||||
@Valid
|
||||
private List<ApiFile> files;
|
||||
/**
|
||||
* 参数的 contentType
|
||||
*/
|
||||
@Size(max = 100)
|
||||
private String contentType;
|
||||
|
||||
public boolean isFile() {
|
||||
return StringUtils.equalsIgnoreCase(getParamType(), BodyParamType.FILE.getValue());
|
||||
|
|
|
@ -13,5 +13,5 @@ import java.util.List;
|
|||
@Data
|
||||
public class WWWFormBody {
|
||||
@Valid
|
||||
private List<FormDataKV> fromValues;
|
||||
private List<WWWFormKV> formValues;
|
||||
}
|
||||
|
|
|
@ -34,11 +34,6 @@ public class WWWFormKV extends KeyValueEnableParam {
|
|||
* 最大长度
|
||||
*/
|
||||
private Integer maxLength;
|
||||
/**
|
||||
* 参数的 contentType
|
||||
*/
|
||||
@Size(max = 100)
|
||||
private String contentType;
|
||||
/**
|
||||
* 是否对参数进行编码
|
||||
* 默认 false
|
||||
|
|
|
@ -166,7 +166,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
|
|||
private void parseWWWFormBody(JsonSchemaItem item, Body body) {
|
||||
WWWFormBody wwwFormBody = new WWWFormBody();
|
||||
List<String> required = item.getRequired();
|
||||
List<FormDataKV> formDataKVS = new ArrayList<>();
|
||||
List<WWWFormKV> formDataKVS = new ArrayList<>();
|
||||
item.getProperties().forEach((key, value) -> {
|
||||
if (value != null && !StringUtils.equals(PropertyConstant.OBJECT, value.getType())) {
|
||||
FormDataKV formDataKV = new FormDataKV();
|
||||
|
@ -180,7 +180,7 @@ public class Swagger3Parser<T> implements ImportParser<ApiDefinitionImport> {
|
|||
formDataKVS.add(formDataKV);
|
||||
}
|
||||
});
|
||||
wwwFormBody.setFromValues(formDataKVS);
|
||||
wwwFormBody.setFormValues(formDataKVS);
|
||||
body.setWwwFormBody(wwwFormBody);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.metersphere.api.parser.jmeter.body;
|
|||
|
||||
|
||||
import io.metersphere.api.dto.ApiFile;
|
||||
import io.metersphere.api.dto.request.http.body.FormDataKV;
|
||||
import io.metersphere.api.dto.request.http.body.WWWFormKV;
|
||||
import io.metersphere.jmeter.mock.Mock;
|
||||
import io.metersphere.plugin.api.dto.ParameterConfig;
|
||||
|
@ -36,23 +37,23 @@ public abstract class MsBodyConverter<T> {
|
|||
|
||||
/**
|
||||
* 解析文本类型的 kv 参数
|
||||
* @param textFromValues
|
||||
* @param textFormValues
|
||||
* @return
|
||||
*/
|
||||
protected Arguments getArguments(List<? extends WWWFormKV> textFromValues) {
|
||||
protected Arguments getArguments(List<? extends WWWFormKV> textFormValues) {
|
||||
Arguments arguments = new Arguments();
|
||||
textFromValues.forEach(formDataKV -> {
|
||||
textFormValues.forEach(kv -> {
|
||||
// 处理 mock 函数
|
||||
String value = Mock.buildFunctionCallString(formDataKV.getValue());
|
||||
String value = Mock.buildFunctionCallString(kv.getValue());
|
||||
if (value == null) {
|
||||
value = StringUtils.EMPTY;
|
||||
}
|
||||
HTTPArgument httpArgument = new HTTPArgument(formDataKV.getKey(), value);
|
||||
httpArgument.setAlwaysEncoded(formDataKV.getEncode());
|
||||
if (StringUtils.isNotBlank(formDataKV.getContentType())) {
|
||||
HTTPArgument httpArgument = new HTTPArgument(kv.getKey(), value);
|
||||
httpArgument.setAlwaysEncoded(kv.getEncode());
|
||||
arguments.addArgument(httpArgument);
|
||||
if (kv instanceof FormDataKV formDataKV && formDataKV.getContentType() != null) {
|
||||
httpArgument.setContentType(formDataKV.getContentType());
|
||||
}
|
||||
arguments.addArgument(httpArgument);
|
||||
});
|
||||
return arguments;
|
||||
}
|
||||
|
|
|
@ -22,29 +22,29 @@ public class MsFormDataBodyConverter extends MsBodyConverter<FormDataBody> {
|
|||
|
||||
@Override
|
||||
public void parse(HTTPSamplerProxy sampler, FormDataBody body, ParameterConfig config) {
|
||||
List<FormDataKV> fromValues = body.getFromValues();
|
||||
List<FormDataKV> validFromValues = fromValues.stream().filter(FormDataKV::isValid).collect(Collectors.toList());
|
||||
List<FormDataKV> fileFromValues = validFromValues.stream().filter(FormDataKV::isFile).collect(Collectors.toList());
|
||||
List<FormDataKV> textFromValues = validFromValues.stream().filter(kv -> !kv.isFile()).collect(Collectors.toList());
|
||||
List<FormDataKV> formValues = body.getFormValues();
|
||||
List<FormDataKV> validFormValues = formValues.stream().filter(FormDataKV::isValid).collect(Collectors.toList());
|
||||
List<FormDataKV> fileFormValues = validFormValues.stream().filter(FormDataKV::isFile).collect(Collectors.toList());
|
||||
List<FormDataKV> textFormValues = validFormValues.stream().filter(kv -> !kv.isFile()).collect(Collectors.toList());
|
||||
sampler.setDoMultipart(true);
|
||||
sampler.setHTTPFiles(getHttpFileArg(fileFromValues));
|
||||
sampler.setArguments(getArguments(textFromValues));
|
||||
sampler.setHTTPFiles(getHttpFileArg(fileFormValues));
|
||||
sampler.setArguments(getArguments(textFormValues));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析文件类型的参数
|
||||
*
|
||||
* @param fileFromValues
|
||||
* @param fileFormValues
|
||||
* @return
|
||||
*/
|
||||
private HTTPFileArg[] getHttpFileArg(List<FormDataKV> fileFromValues) {
|
||||
if (CollectionUtils.isEmpty(fileFromValues)) {
|
||||
private HTTPFileArg[] getHttpFileArg(List<FormDataKV> fileFormValues) {
|
||||
if (CollectionUtils.isEmpty(fileFormValues)) {
|
||||
return new HTTPFileArg[0];
|
||||
}
|
||||
List<HTTPFileArg> list = new ArrayList<>();
|
||||
if (CollectionUtils.isNotEmpty(fileFromValues)) {
|
||||
fileFromValues.forEach(formDataKV -> {
|
||||
if (CollectionUtils.isNotEmpty(fileFormValues)) {
|
||||
fileFormValues.forEach(formDataKV -> {
|
||||
String paramName = formDataKV.getKey();
|
||||
formDataKV.getFiles().forEach(file -> {
|
||||
HTTPFileArg fileArg = getHttpFileArg(file);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.metersphere.api.parser.jmeter.body;
|
||||
|
||||
import io.metersphere.api.dto.request.http.body.FormDataKV;
|
||||
import io.metersphere.api.dto.request.http.body.WWWFormBody;
|
||||
import io.metersphere.api.dto.request.http.body.WWWFormKV;
|
||||
import io.metersphere.plugin.api.dto.ParameterConfig;
|
||||
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
|
||||
|
||||
|
@ -15,8 +15,8 @@ import java.util.stream.Collectors;
|
|||
public class MsWWWFormBodyConverter extends MsBodyConverter<WWWFormBody> {
|
||||
@Override
|
||||
public void parse(HTTPSamplerProxy sampler, WWWFormBody body, ParameterConfig config) {
|
||||
List<FormDataKV> fromValues = body.getFromValues();
|
||||
List<FormDataKV> validFromValues = fromValues.stream().filter(FormDataKV::isValid).collect(Collectors.toList());
|
||||
sampler.setArguments(getArguments(validFromValues));
|
||||
List<WWWFormKV> formValues = body.getFormValues();
|
||||
List<WWWFormKV> validFormValues = formValues.stream().filter(WWWFormKV::isValid).collect(Collectors.toList());
|
||||
sampler.setArguments(getArguments(validFormValues));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -86,11 +86,11 @@ public class ApiStepParser extends StepParser {
|
|||
}
|
||||
if (StringUtils.equals(refBody.getBodyType(), Body.BodyType.FORM_DATA.name()) &&
|
||||
valueBody.getFormDataBody() != null && refBody.getFormDataBody() != null) {
|
||||
replaceKvParam(valueBody.getFormDataBody().getFromValues(), valueBody.getFormDataBody().getFromValues());
|
||||
replaceKvParam(valueBody.getFormDataBody().getFormValues(), valueBody.getFormDataBody().getFormValues());
|
||||
}
|
||||
if (StringUtils.equals(refBody.getBodyType(), Body.BodyType.WWW_FORM.name()) &&
|
||||
valueBody.getWwwFormBody() != null && refBody.getWwwFormBody() != null) {
|
||||
replaceKvParam(valueBody.getWwwFormBody().getFromValues(), valueBody.getWwwFormBody().getFromValues());
|
||||
replaceKvParam(valueBody.getWwwFormBody().getFormValues(), valueBody.getWwwFormBody().getFormValues());
|
||||
}
|
||||
// todo JsonSchema body
|
||||
}
|
||||
|
|
|
@ -517,11 +517,11 @@ public class ApiDefinitionImportUtilService {
|
|||
FormDataBody formDataBody = dbbody.getFormDataBody();
|
||||
FormDataBody importFormDataBody = importBody.getFormDataBody();
|
||||
if (ObjectUtils.isNotEmpty(formDataBody) || ObjectUtils.isNotEmpty(importFormDataBody)) {
|
||||
List<FormDataKV> fromValues = formDataBody.getFromValues();
|
||||
List<FormDataKV> importFromValues = importFormDataBody.getFromValues();
|
||||
if (CollectionUtils.isNotEmpty(fromValues) || CollectionUtils.isNotEmpty(importFromValues)) {
|
||||
List<String> dbFormKeys = fromValues.stream().map(FormDataKV::getKey).toList();
|
||||
List<String> importFormKeys = importFromValues.stream().map(FormDataKV::getKey).toList();
|
||||
List<FormDataKV> formValues = formDataBody.getFormValues();
|
||||
List<FormDataKV> importFormValues = importFormDataBody.getFormValues();
|
||||
if (CollectionUtils.isNotEmpty(formValues) || CollectionUtils.isNotEmpty(importFormValues)) {
|
||||
List<String> dbFormKeys = formValues.stream().map(FormDataKV::getKey).toList();
|
||||
List<String> importFormKeys = importFormValues.stream().map(FormDataKV::getKey).toList();
|
||||
if (paramsIsSame(dbFormKeys, importFormKeys)) {
|
||||
return false;
|
||||
}
|
||||
|
@ -531,11 +531,11 @@ public class ApiDefinitionImportUtilService {
|
|||
WWWFormBody wwwBody = dbbody.getWwwFormBody();
|
||||
WWWFormBody importWwwBody = importBody.getWwwFormBody();
|
||||
if (ObjectUtils.isNotEmpty(wwwBody) || ObjectUtils.isNotEmpty(importWwwBody)) {
|
||||
List<FormDataKV> wwwValues = wwwBody.getFromValues();
|
||||
List<FormDataKV> importWwwValues = importWwwBody.getFromValues();
|
||||
List<WWWFormKV> wwwValues = wwwBody.getFormValues();
|
||||
List<WWWFormKV> importWwwValues = importWwwBody.getFormValues();
|
||||
if (CollectionUtils.isNotEmpty(wwwValues) || CollectionUtils.isNotEmpty(importWwwValues)) {
|
||||
List<String> dbWwwKeys = wwwValues.stream().map(FormDataKV::getKey).toList();
|
||||
List<String> importWwwKeys = importWwwValues.stream().map(FormDataKV::getKey).toList();
|
||||
List<String> dbWwwKeys = wwwValues.stream().map(WWWFormKV::getKey).toList();
|
||||
List<String> importWwwKeys = importWwwValues.stream().map(WWWFormKV::getKey).toList();
|
||||
if (paramsIsSame(dbWwwKeys, importWwwKeys)) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -79,11 +79,12 @@ public class MsHTTPElementTest {
|
|||
bodyFile.setFileName("aaa");
|
||||
formDataFileKV.setFiles(List.of(bodyFile));
|
||||
formDataFileKV.setKey("fileKey");
|
||||
formDataBody.setFromValues(List.of(formDataKV));
|
||||
formDataBody.setFormValues(List.of(formDataKV));
|
||||
body.setFormDataBody(formDataBody);
|
||||
|
||||
WWWFormKV wwwFormKV = BeanUtils.copyBean(new WWWFormKV(), formDataKV);
|
||||
WWWFormBody wwwFormBody = new WWWFormBody();
|
||||
wwwFormBody.setFromValues(List.of(formDataKV));
|
||||
wwwFormBody.setFormValues(List.of(wwwFormKV));
|
||||
body.setWwwFormBody(wwwFormBody);
|
||||
|
||||
JsonBody jsonBody = new JsonBody();
|
||||
|
@ -411,7 +412,7 @@ public class MsHTTPElementTest {
|
|||
formDataKV.setRequired(true);
|
||||
formDataKV.setValue("value");
|
||||
formDataKV.setKey("key");
|
||||
formDataBody.setFromValues(List.of(formDataKV));
|
||||
formDataBody.setFormValues(List.of(formDataKV));
|
||||
Body body = new Body();
|
||||
body.setBodyType(Body.BodyType.FORM_DATA.name());
|
||||
httpResponse.setBody(body);
|
||||
|
|
|
@ -14,10 +14,6 @@ import java.util.List;
|
|||
public class HttpConfig implements Serializable {
|
||||
@Schema(description = "环境域名")
|
||||
private String url;
|
||||
@Schema(description = "接口测试")
|
||||
private Boolean apiTest = true;
|
||||
@Schema(description = "UI测试")
|
||||
private Boolean uiTest = false;
|
||||
@Schema(description = "启用条件 NONE/MODULE/PATH")
|
||||
private String type = "NONE";
|
||||
@Schema(description = "启用条件为PATH时,需要填写的路径/ key为equal时,value为路径,key为contain时,value为包含的路径")
|
||||
|
|
Loading…
Reference in New Issue