fix(接口测试): 修复swagger url无法访问时,无法结束的缺陷

--bug=1041489 --user=王孝刚 【接口测试】接口swagger导入-当前服务无法访问导入url时-一直导入中-无任何提示 https://www.tapd.cn/55049933/s/1523705
This commit is contained in:
wxg0103 2024-05-30 15:56:09 +08:00 committed by 刘瑞斌
parent 753ee87b9c
commit a135ea696c
5 changed files with 33 additions and 3 deletions

View File

@ -541,3 +541,5 @@ current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限
#全局请求头 #全局请求头
global_request_header=全局请求头 global_request_header=全局请求头
url_format_error=请检查Swagger URL是否输入正确

View File

@ -579,3 +579,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!

View File

@ -576,3 +576,5 @@ current_user_can_not_operation_api_key=当前用户无操作该ApiKey的权限
#全局请求头 #全局请求头
global_request_header=全局请求头 global_request_header=全局请求头
url_format_error=请检查Swagger URL是否输入正确

View File

@ -576,3 +576,5 @@ current_user_can_not_operation_api_key=當前用戶無法操作ApiKey
#全局请求头 #全局请求头
global_request_header=全局請求頭 global_request_header=全局請求頭
url_format_error=请检查Swagger URL是否输入正确

View File

@ -57,6 +57,8 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -112,6 +114,9 @@ public class ApiDefinitionImportUtilService {
if (StringUtils.equals(request.getType(), "SCHEDULE")) { if (StringUtils.equals(request.getType(), "SCHEDULE")) {
request.setProtocol(ModuleConstants.NODE_PROTOCOL_HTTP); request.setProtocol(ModuleConstants.NODE_PROTOCOL_HTTP);
} }
if (StringUtils.equals(request.getPlatform(), ApiImportPlatform.Swagger3.name()) && StringUtils.isNotBlank(request.getSwaggerUrl())) {
testUrlTimeout(request.getSwaggerUrl(), 30000);
}
try { try {
apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request); apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request);
//TODO 处理mock数据 //TODO 处理mock数据
@ -128,6 +133,23 @@ public class ApiDefinitionImportUtilService {
} }
} }
public static void testUrlTimeout(String address, int timeOutMillSeconds) {
HttpURLConnection connection = null;
try {
URI uriObj = new URI(address);
connection = (HttpURLConnection) uriObj.toURL().openConnection();
connection.setUseCaches(false);
connection.setConnectTimeout(timeOutMillSeconds); // 设置超时时间
connection.connect(); // 建立连接
} catch (Exception e) {
LogUtils.error(e);
throw new MSException(Translator.get("url_format_error"));
} finally {
if (connection != null) {
connection.disconnect(); // 关闭连接
}
}
}
public void checkFileSuffixName(ImportRequest request, String suffixName) { public void checkFileSuffixName(ImportRequest request, String suffixName) {
if (FILE_JMX.equalsIgnoreCase(suffixName)) { if (FILE_JMX.equalsIgnoreCase(suffixName)) {