refactor(接口测试): 导入增加解析请求头
This commit is contained in:
parent
1b36bd940f
commit
b3283e3d78
|
@ -543,3 +543,5 @@ current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限
|
||||||
global_request_header=全局请求头
|
global_request_header=全局请求头
|
||||||
|
|
||||||
url_format_error=请检查Swagger URL是否输入正确!
|
url_format_error=请检查Swagger URL是否输入正确!
|
||||||
|
|
||||||
|
swagger_version_error=Swagger 版本不支持,请检查是否为3.0版本!
|
||||||
|
|
|
@ -581,3 +581,5 @@ current_user_can_not_operation_api_key=The current user cannot operate the API k
|
||||||
global_request_header=Global request header
|
global_request_header=Global request header
|
||||||
|
|
||||||
url_format_error=Please check if the Swagger URL is entered correctly!
|
url_format_error=Please check if the Swagger URL is entered correctly!
|
||||||
|
|
||||||
|
swagger_version_error=Swagger version not supported, please check if it is version 3.0!
|
|
@ -577,4 +577,5 @@ current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限
|
||||||
#全局请求头
|
#全局请求头
|
||||||
global_request_header=全局请求头
|
global_request_header=全局请求头
|
||||||
|
|
||||||
url_format_error=请检查Swagger URL是否输入正确!
|
url_format_error=请检查Swagger URL是否输入正确!
|
||||||
|
swagger_version_error=Swagger 版本不支持,请检查是否为3.0版本!
|
|
@ -577,4 +577,5 @@ current_user_can_not_operation_api_key=當前用戶無法操作ApiKey
|
||||||
#全局请求头
|
#全局请求头
|
||||||
global_request_header=全局請求頭
|
global_request_header=全局請求頭
|
||||||
|
|
||||||
url_format_error=请检查Swagger URL是否输入正确!
|
url_format_error=请检查Swagger URL是否输入正确!
|
||||||
|
swagger_version_error=Swagger 版本不支持,请检查是否为3.0版本!
|
|
@ -7,7 +7,9 @@ package io.metersphere.api.constants;
|
||||||
public class ApiConstants {
|
public class ApiConstants {
|
||||||
public static final String HTTP_PROTOCOL = "HTTP";
|
public static final String HTTP_PROTOCOL = "HTTP";
|
||||||
public static final String CONTENT_TYPE = "Content-Type";
|
public static final String CONTENT_TYPE = "Content-Type";
|
||||||
|
public static final String ACCEPT = "Accept";
|
||||||
public static final String ASSOCIATE_RESULT_PROCESSOR_PREFIX = "ASSOCIATE_RESULT_PROCESSOR_";
|
public static final String ASSOCIATE_RESULT_PROCESSOR_PREFIX = "ASSOCIATE_RESULT_PROCESSOR_";
|
||||||
|
|
||||||
private ApiConstants() {}
|
private ApiConstants() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package io.metersphere.api.parser.api;
|
package io.metersphere.api.parser.api;
|
||||||
|
|
||||||
|
import io.metersphere.api.constants.ApiConstants;
|
||||||
import io.metersphere.api.dto.converter.ApiDefinitionImport;
|
import io.metersphere.api.dto.converter.ApiDefinitionImport;
|
||||||
import io.metersphere.api.dto.converter.ApiDefinitionImportDetail;
|
import io.metersphere.api.dto.converter.ApiDefinitionImportDetail;
|
||||||
import io.metersphere.api.dto.definition.HttpResponse;
|
import io.metersphere.api.dto.definition.HttpResponse;
|
||||||
|
@ -61,6 +62,13 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String apiTestStr = getApiTestStr(source);
|
String apiTestStr = getApiTestStr(source);
|
||||||
|
Map<String, Object> o = ApiDataUtils.parseObject(apiTestStr, Map.class);
|
||||||
|
// 判断属性 swagger的值是不是3.0开头
|
||||||
|
if (o instanceof Map map) {
|
||||||
|
if (map.containsKey("swagger") && !map.get("swagger").toString().startsWith("3.0")) {
|
||||||
|
throw new MSException(Translator.get("swagger_version_error"));
|
||||||
|
}
|
||||||
|
}
|
||||||
result = new OpenAPIParser().readContents(apiTestStr, null, null);
|
result = new OpenAPIParser().readContents(apiTestStr, null, null);
|
||||||
if (result == null || result.getOpenAPI() == null || !result.getOpenAPI().getOpenapi().startsWith("3.0") || result.isOpenapi31()) {
|
if (result == null || result.getOpenAPI() == null || !result.getOpenAPI().getOpenapi().startsWith("3.0") || result.isOpenapi31()) {
|
||||||
throw new MSException(Translator.get("swagger_parse_error"));
|
throw new MSException(Translator.get("swagger_parse_error"));
|
||||||
|
@ -119,18 +127,18 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
parseParameters(operation, request);
|
parseParameters(operation, request);
|
||||||
parseParameters(pathItem, request);
|
parseParameters(pathItem, request);
|
||||||
//构建请求体
|
//构建请求体
|
||||||
parseRequestBody(operation.getRequestBody(), request.getBody());
|
parseRequestBody(operation.getRequestBody(), request);
|
||||||
//构造 children
|
//构造 children
|
||||||
LinkedList<AbstractMsTestElement> children = new LinkedList<>();
|
LinkedList<AbstractMsTestElement> children = new LinkedList<>();
|
||||||
children.add(new MsCommonElement());
|
children.add(new MsCommonElement());
|
||||||
request.setChildren(children);
|
request.setChildren(children);
|
||||||
//认证
|
//认证
|
||||||
request.setAuthConfig(new NoAuth());
|
request.setAuthConfig(new NoAuth());
|
||||||
apiDefinitionDTO.setRequest(request);
|
|
||||||
|
|
||||||
|
|
||||||
//解析请求内容
|
//解析请求内容
|
||||||
parseResponse(operation.getResponses(), apiDefinitionDTO.getResponse());
|
parseResponse(operation.getResponses(), apiDefinitionDTO.getResponse(), request.getHeaders());
|
||||||
|
apiDefinitionDTO.setRequest(request);
|
||||||
results.add(apiDefinitionDTO);
|
results.add(apiDefinitionDTO);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -139,20 +147,28 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseRequestBody(RequestBody requestBody, Body body) {
|
private void parseRequestBody(RequestBody requestBody, MsHTTPElement request) {
|
||||||
if (requestBody != null) {
|
if (requestBody != null) {
|
||||||
Content content = requestBody.getContent();
|
Content content = requestBody.getContent();
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
|
List<MsHeader> headers = request.getHeaders();
|
||||||
content.forEach((key, value) -> {
|
content.forEach((key, value) -> {
|
||||||
setRequestBodyData(key, value, body);
|
setRequestBodyData(key, value, request.getBody());
|
||||||
|
// 如果key不包含Content-Type 则默认添加Content-Type
|
||||||
|
if (headers.stream().noneMatch(header -> StringUtils.equals(header.getKey(), ApiConstants.CONTENT_TYPE))) {
|
||||||
|
MsHeader header = new MsHeader();
|
||||||
|
header.setKey(ApiConstants.CONTENT_TYPE);
|
||||||
|
header.setValue(key);
|
||||||
|
headers.add(header);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
body.setBodyType(Body.BodyType.NONE.name());
|
request.getBody().setBodyType(Body.BodyType.NONE.name());
|
||||||
body.setNoneBody(new NoneBody());
|
request.getBody().setNoneBody(new NoneBody());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body.setBodyType(Body.BodyType.NONE.name());
|
request.getBody().setBodyType(Body.BodyType.NONE.name());
|
||||||
body.setNoneBody(new NoneBody());
|
request.getBody().setNoneBody(new NoneBody());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,7 +222,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
body.setFormDataBody(formDataBody);
|
body.setFormDataBody(formDataBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseResponse(ApiResponses responseBody, List<HttpResponse> response) {
|
private void parseResponse(ApiResponses responseBody, List<HttpResponse> response, List<MsHeader> requestHeaders) {
|
||||||
if (responseBody != null) {
|
if (responseBody != null) {
|
||||||
responseBody.forEach((key, value) -> {
|
responseBody.forEach((key, value) -> {
|
||||||
HttpResponse httpResponse = new HttpResponse();
|
HttpResponse httpResponse = new HttpResponse();
|
||||||
|
@ -227,7 +243,13 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
}
|
}
|
||||||
if (value.getContent() != null) {
|
if (value.getContent() != null) {
|
||||||
value.getContent().forEach((k, v) -> {
|
value.getContent().forEach((k, v) -> {
|
||||||
setRequestBodyData(k, v, body);
|
setResponseBodyData(k, v, body);
|
||||||
|
if (requestHeaders.stream().noneMatch(header -> StringUtils.equals(header.getKey(), ApiConstants.ACCEPT))) {
|
||||||
|
MsHeader header = new MsHeader();
|
||||||
|
header.setKey(ApiConstants.ACCEPT);
|
||||||
|
header.setValue(k);
|
||||||
|
requestHeaders.add(header);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
body.setBodyType(Body.BodyType.NONE.name());
|
body.setBodyType(Body.BodyType.NONE.name());
|
||||||
|
@ -250,7 +272,7 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setRequestBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, ResponseBody body) {
|
private void setResponseBodyData(String k, io.swagger.v3.oas.models.media.MediaType value, ResponseBody body) {
|
||||||
//TODO body 默认如果json格式
|
//TODO body 默认如果json格式
|
||||||
JsonSchemaItem jsonSchemaItem = parseSchema(value.getSchema());
|
JsonSchemaItem jsonSchemaItem = parseSchema(value.getSchema());
|
||||||
switch (k) {
|
switch (k) {
|
||||||
|
@ -373,14 +395,13 @@ public class Swagger3Parser<T> extends ApiImportAbstractParser<ApiDefinitionImpo
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parameters.forEach(parameter -> {
|
parameters.forEach(parameter -> {
|
||||||
if (parameter instanceof QueryParameter queryParameter) {
|
switch (parameter) {
|
||||||
parseQueryParameters(queryParameter, request.getQuery());
|
case QueryParameter queryParameter -> parseQueryParameters(queryParameter, request.getQuery());
|
||||||
} else if (parameter instanceof PathParameter pathParameter) {
|
case PathParameter pathParameter -> parsePathParameters(pathParameter, request.getRest());
|
||||||
parsePathParameters(pathParameter, request.getRest());
|
case HeaderParameter headerParameter -> parseHeaderParameters(headerParameter, request.getHeaders());
|
||||||
} else if (parameter instanceof HeaderParameter headerParameter) {
|
case CookieParameter cookieParameter -> parseCookieParameters(cookieParameter, request.getHeaders());
|
||||||
parseHeaderParameters(headerParameter, request.getHeaders());
|
default -> {
|
||||||
} else if (parameter instanceof CookieParameter cookieParameter) {
|
}
|
||||||
parseCookieParameters(cookieParameter, request.getHeaders());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue