feat(接口测试): MockServer增加响应体设置为JsonSchema的功能
This commit is contained in:
parent
8baf7f5c13
commit
55b8f0b943
|
@ -11,6 +11,7 @@ import io.metersphere.api.dto.request.ImportRequest;
|
|||
import io.metersphere.api.service.definition.ApiDefinitionLogService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
|
||||
import io.metersphere.api.service.definition.ApiDefinitionService;
|
||||
import io.metersphere.api.utils.JsonSchemaBuilder;
|
||||
import io.metersphere.project.service.FileModuleService;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.system.dto.OperationHistoryDTO;
|
||||
|
@ -271,6 +272,6 @@ public class ApiDefinitionController {
|
|||
@Operation(summary = "接口测试-接口管理-接口-json-schema-预览")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
|
||||
public String preview(@RequestBody TextNode jsonSchema) {
|
||||
return apiDefinitionService.preview(jsonSchema.asText());
|
||||
return JsonSchemaBuilder.preview(jsonSchema.asText());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package io.metersphere.api.dto.request.http.body;
|
||||
|
||||
import io.metersphere.api.dto.schema.JsonSchemaItem;
|
||||
import io.metersphere.api.utils.JsonSchemaBuilder;
|
||||
import io.metersphere.sdk.util.JSON;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.Data;
|
||||
|
||||
|
@ -32,4 +34,12 @@ public class JsonBody {
|
|||
* 默认为 false
|
||||
*/
|
||||
private Boolean enableTransition = false;
|
||||
|
||||
//判断是返回jsonValue还是计算JsonSchema
|
||||
public String getJsonWithSchema() {
|
||||
if (enableJsonSchema) {
|
||||
return JsonSchemaBuilder.preview(JSON.toJSONString(jsonSchema));
|
||||
}
|
||||
return jsonValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,8 @@ public class MsJsonBodyConverter extends MsBodyConverter<JsonBody> {
|
|||
public void parse(HTTPSamplerProxy sampler, JsonBody body, ParameterConfig config) {
|
||||
sampler.setPostBodyRaw(true);
|
||||
try {
|
||||
String raw = null;
|
||||
if (body.getEnableJsonSchema() && body.getEnableTransition()) {
|
||||
String raw;
|
||||
if (body.getEnableJsonSchema()) {
|
||||
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(JSON.toJSONString(body.getJsonSchema()));
|
||||
raw = StringEscapeUtils.unescapeJava(jsonString);
|
||||
} else {
|
||||
|
|
|
@ -18,8 +18,6 @@ import io.metersphere.api.parser.ImportParserFactory;
|
|||
import io.metersphere.api.service.ApiCommonService;
|
||||
import io.metersphere.api.service.ApiFileResourceService;
|
||||
import io.metersphere.api.utils.ApiDataUtils;
|
||||
import io.metersphere.api.utils.JsonSchemaBuilder;
|
||||
import io.metersphere.jmeter.mock.Mock;
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.metersphere.project.domain.FileAssociation;
|
||||
import io.metersphere.project.domain.FileMetadata;
|
||||
|
@ -59,8 +57,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -1126,26 +1122,4 @@ public class ApiDefinitionService {
|
|||
return apiFileResourceService.transfer(request, userId, ApiResourceType.API.name());
|
||||
}
|
||||
|
||||
public String preview(String jsonSchema) {
|
||||
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(jsonSchema);
|
||||
//需要匹配到mock函数 然后换成mock数据
|
||||
if (StringUtils.isNotBlank(jsonString)) {
|
||||
String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]";
|
||||
Pattern regex = Pattern.compile(pattern);
|
||||
Matcher matcher = regex.matcher(jsonString);
|
||||
while (matcher.find()) {
|
||||
//取出group的最后一个字符 主要是防止 @string|number 和 @string 这种情况
|
||||
String group = matcher.group();
|
||||
String lastChar = null;
|
||||
if (group.endsWith(",") || group.endsWith("\"")) {
|
||||
lastChar = group.substring(group.length() - 1);
|
||||
group = group.substring(0, group.length() - 1);
|
||||
}
|
||||
jsonString = jsonString.replace(matcher.group(),
|
||||
StringUtils.join(Mock.calculate(group), lastChar));
|
||||
}
|
||||
}
|
||||
return jsonString;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,8 +75,7 @@ public class MockServerService {
|
|||
LogUtils.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams));
|
||||
ApiDefinitionMockConfig compareMockConfig = this.match(apiDefinition.getId(), requestHeaderMap, requestMockParams);
|
||||
try {
|
||||
Object returnObj = this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response);
|
||||
return returnObj;
|
||||
return this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response);
|
||||
} catch (Exception e) {
|
||||
return this.requestNotFound(response);
|
||||
}
|
||||
|
@ -158,7 +157,7 @@ public class MockServerService {
|
|||
return StringUtils.EMPTY;
|
||||
} else {
|
||||
if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.JSON.name())) {
|
||||
return responseBody.getJsonBody().getJsonValue();
|
||||
return responseBody.getJsonBody().getJsonWithSchema();
|
||||
} else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.XML.name())) {
|
||||
return responseBody.getXmlBody().getValue();
|
||||
} else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.RAW.name())) {
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.databind.node.*;
|
||||
import io.metersphere.jmeter.mock.Mock;
|
||||
import io.metersphere.project.constants.PropertyConstant;
|
||||
import io.metersphere.sdk.util.LogUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
|
@ -16,6 +17,8 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class JsonSchemaBuilder {
|
||||
|
||||
|
@ -136,4 +139,27 @@ public class JsonSchemaBuilder {
|
|||
processMap.put(key, targetValue);
|
||||
return new TextNode(value);
|
||||
}
|
||||
|
||||
public static String preview(String jsonSchema) {
|
||||
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(jsonSchema);
|
||||
//需要匹配到mock函数 然后换成mock数据
|
||||
if (StringUtils.isNotBlank(jsonString)) {
|
||||
String pattern = "@[a-zA-Z\\\\(|,'-\\\\d ]*[a-zA-Z)-9),\\\\\"]";
|
||||
Pattern regex = Pattern.compile(pattern);
|
||||
Matcher matcher = regex.matcher(jsonString);
|
||||
while (matcher.find()) {
|
||||
//取出group的最后一个字符 主要是防止 @string|number 和 @string 这种情况
|
||||
String group = matcher.group();
|
||||
String lastChar = null;
|
||||
if (group.endsWith(",") || group.endsWith("\"")) {
|
||||
lastChar = group.substring(group.length() - 1);
|
||||
group = group.substring(0, group.length() - 1);
|
||||
}
|
||||
jsonString = jsonString.replace(matcher.group(),
|
||||
StringUtils.join(Mock.calculate(group), lastChar));
|
||||
}
|
||||
}
|
||||
return jsonString;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue