refactor(接口测试): 优化接口导入,默认一个响应结果

This commit is contained in:
wxg0103 2024-05-20 14:48:54 +08:00 committed by wxg0103
parent 7977da1766
commit 5cc30123e2
2 changed files with 22 additions and 2 deletions

View File

@ -27,6 +27,7 @@ import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.dto.environment.auth.BasicAuth; import io.metersphere.project.dto.environment.auth.BasicAuth;
import io.metersphere.project.dto.environment.auth.DigestAuth; import io.metersphere.project.dto.environment.auth.DigestAuth;
import io.metersphere.project.dto.environment.auth.HTTPAuthConfig; import io.metersphere.project.dto.environment.auth.HTTPAuthConfig;
import io.metersphere.system.uid.IDGenerator;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -110,7 +111,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
if (CollectionUtils.isNotEmpty(response)) { if (CollectionUtils.isNotEmpty(response)) {
response.forEach(r -> { response.forEach(r -> {
HttpResponse httpResponse = new HttpResponse(); HttpResponse httpResponse = new HttpResponse();
//httpResponse.setId(IDGenerator.nextStr()); httpResponse.setId(IDGenerator.nextStr());
httpResponse.setName(r.getName()); httpResponse.setName(r.getName());
httpResponse.setStatusCode(r.getCode().toString()); httpResponse.setStatusCode(r.getCode().toString());
if (CollectionUtils.isNotEmpty(r.getHeader())) { if (CollectionUtils.isNotEmpty(r.getHeader())) {
@ -123,6 +124,14 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
httpResponse.getBody().setBodyType(Body.BodyType.RAW.name()); httpResponse.getBody().setBodyType(Body.BodyType.RAW.name());
detail.getResponse().add(httpResponse); detail.getResponse().add(httpResponse);
}); });
detail.getResponse().forEach(httpResponse -> {
if (StringUtils.equals("200", httpResponse.getStatusCode())) {
httpResponse.setDefaultFlag(true);
}
});
if (detail.getResponse().stream().noneMatch(httpResponse -> StringUtils.equals("200", httpResponse.getStatusCode()))) {
detail.getResponse().getFirst().setDefaultFlag(true);
}
} }
} }
@ -317,7 +326,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
JsonNode usernameNode = digestObject.get(USERNAME); JsonNode usernameNode = digestObject.get(USERNAME);
JsonNode passwordNode = digestObject.get(PASSWORD); JsonNode passwordNode = digestObject.get(PASSWORD);
digestAuth.setUserName(usernameNode instanceof TextNode ? usernameNode.asText() : StringUtils.EMPTY); digestAuth.setUserName(usernameNode instanceof TextNode ? usernameNode.asText() : StringUtils.EMPTY);
digestAuth.setPassword(passwordNode instanceof TextNode ?passwordNode.asText() : StringUtils.EMPTY); digestAuth.setPassword(passwordNode instanceof TextNode ? passwordNode.asText() : StringUtils.EMPTY);
} else if (digestNode instanceof ArrayNode digestArray) { } else if (digestNode instanceof ArrayNode digestArray) {
digestArray.forEach(node -> { digestArray.forEach(node -> {
JsonNode keyNode = node.get(KEY); JsonNode keyNode = node.get(KEY);

View File

@ -210,6 +210,17 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
httpResponse.setBody(body); httpResponse.setBody(body);
response.add(httpResponse); response.add(httpResponse);
}); });
// 判断 如果是200 默认defaultFlag为true 否则的话 随机挑一个为true
if (CollectionUtils.isNotEmpty(response)) {
response.forEach(httpResponse -> {
if (StringUtils.equals("200", httpResponse.getStatusCode())) {
httpResponse.setDefaultFlag(true);
}
});
if (response.stream().noneMatch(httpResponse -> StringUtils.equals("200", httpResponse.getStatusCode()))) {
response.getFirst().setDefaultFlag(true);
}
}
} }
} }