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.ApiDefinitionLogService;
|
||||||
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
|
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
|
||||||
import io.metersphere.api.service.definition.ApiDefinitionService;
|
import io.metersphere.api.service.definition.ApiDefinitionService;
|
||||||
|
import io.metersphere.api.utils.JsonSchemaBuilder;
|
||||||
import io.metersphere.project.service.FileModuleService;
|
import io.metersphere.project.service.FileModuleService;
|
||||||
import io.metersphere.sdk.constants.PermissionConstants;
|
import io.metersphere.sdk.constants.PermissionConstants;
|
||||||
import io.metersphere.system.dto.OperationHistoryDTO;
|
import io.metersphere.system.dto.OperationHistoryDTO;
|
||||||
|
@ -271,6 +272,6 @@ public class ApiDefinitionController {
|
||||||
@Operation(summary = "接口测试-接口管理-接口-json-schema-预览")
|
@Operation(summary = "接口测试-接口管理-接口-json-schema-预览")
|
||||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
|
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
|
||||||
public String preview(@RequestBody TextNode jsonSchema) {
|
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;
|
package io.metersphere.api.dto.request.http.body;
|
||||||
|
|
||||||
import io.metersphere.api.dto.schema.JsonSchemaItem;
|
import io.metersphere.api.dto.schema.JsonSchemaItem;
|
||||||
|
import io.metersphere.api.utils.JsonSchemaBuilder;
|
||||||
|
import io.metersphere.sdk.util.JSON;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@ -32,4 +34,12 @@ public class JsonBody {
|
||||||
* 默认为 false
|
* 默认为 false
|
||||||
*/
|
*/
|
||||||
private Boolean enableTransition = 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) {
|
public void parse(HTTPSamplerProxy sampler, JsonBody body, ParameterConfig config) {
|
||||||
sampler.setPostBodyRaw(true);
|
sampler.setPostBodyRaw(true);
|
||||||
try {
|
try {
|
||||||
String raw = null;
|
String raw;
|
||||||
if (body.getEnableJsonSchema() && body.getEnableTransition()) {
|
if (body.getEnableJsonSchema()) {
|
||||||
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(JSON.toJSONString(body.getJsonSchema()));
|
String jsonString = JsonSchemaBuilder.jsonSchemaToJson(JSON.toJSONString(body.getJsonSchema()));
|
||||||
raw = StringEscapeUtils.unescapeJava(jsonString);
|
raw = StringEscapeUtils.unescapeJava(jsonString);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -18,8 +18,6 @@ import io.metersphere.api.parser.ImportParserFactory;
|
||||||
import io.metersphere.api.service.ApiCommonService;
|
import io.metersphere.api.service.ApiCommonService;
|
||||||
import io.metersphere.api.service.ApiFileResourceService;
|
import io.metersphere.api.service.ApiFileResourceService;
|
||||||
import io.metersphere.api.utils.ApiDataUtils;
|
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.plugin.api.spi.AbstractMsTestElement;
|
||||||
import io.metersphere.project.domain.FileAssociation;
|
import io.metersphere.project.domain.FileAssociation;
|
||||||
import io.metersphere.project.domain.FileMetadata;
|
import io.metersphere.project.domain.FileMetadata;
|
||||||
|
@ -59,8 +57,6 @@ import org.springframework.web.multipart.MultipartFile;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@ -1126,26 +1122,4 @@ public class ApiDefinitionService {
|
||||||
return apiFileResourceService.transfer(request, userId, ApiResourceType.API.name());
|
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));
|
LogUtils.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams));
|
||||||
ApiDefinitionMockConfig compareMockConfig = this.match(apiDefinition.getId(), requestHeaderMap, requestMockParams);
|
ApiDefinitionMockConfig compareMockConfig = this.match(apiDefinition.getId(), requestHeaderMap, requestMockParams);
|
||||||
try {
|
try {
|
||||||
Object returnObj = this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response);
|
return this.getReturn(compareMockConfig, apiDefinition.getId(), apiDefinition.getProjectId(), response);
|
||||||
return returnObj;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return this.requestNotFound(response);
|
return this.requestNotFound(response);
|
||||||
}
|
}
|
||||||
|
@ -158,7 +157,7 @@ public class MockServerService {
|
||||||
return StringUtils.EMPTY;
|
return StringUtils.EMPTY;
|
||||||
} else {
|
} else {
|
||||||
if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.JSON.name())) {
|
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())) {
|
} else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.XML.name())) {
|
||||||
return responseBody.getXmlBody().getValue();
|
return responseBody.getXmlBody().getValue();
|
||||||
} else if (StringUtils.equalsIgnoreCase(responseBody.getBodyType(), Body.BodyType.RAW.name())) {
|
} 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.SerializationFeature;
|
||||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
import com.fasterxml.jackson.databind.node.*;
|
import com.fasterxml.jackson.databind.node.*;
|
||||||
|
import io.metersphere.jmeter.mock.Mock;
|
||||||
import io.metersphere.project.constants.PropertyConstant;
|
import io.metersphere.project.constants.PropertyConstant;
|
||||||
import io.metersphere.sdk.util.LogUtils;
|
import io.metersphere.sdk.util.LogUtils;
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
|
@ -16,6 +17,8 @@ import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class JsonSchemaBuilder {
|
public class JsonSchemaBuilder {
|
||||||
|
|
||||||
|
@ -136,4 +139,27 @@ public class JsonSchemaBuilder {
|
||||||
processMap.put(key, targetValue);
|
processMap.put(key, targetValue);
|
||||||
return new TextNode(value);
|
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