fix(接口测试): swagger添加认证信息导入报错连接超时

--bug=1014848 --user=陈建星 【接口测试】接口定义-使用 swagger url 导入 - 添加请求参数保存后,连接超时 https://www.tapd.cn/55049933/s/1209161
This commit is contained in:
chenjianxing 2022-07-25 11:40:24 +08:00 committed by jianxing
parent 8245bef740
commit 76b7403063
7 changed files with 44 additions and 30 deletions

View File

@ -42,7 +42,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
swagger = new SwaggerParser().read(request.getSwaggerUrl(), auths, true); swagger = new SwaggerParser().read(request.getSwaggerUrl(), auths, true);
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.error(e); LoggerUtil.error(e);
MSException.throwException("swagger验证失败"); MSException.throwException(e.getMessage());
} }
} else { } else {

View File

@ -20,6 +20,7 @@ import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils; import io.metersphere.commons.utils.XMLUtils;
import io.metersphere.i18n.Translator;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.*; import io.swagger.v3.oas.models.*;
import io.swagger.v3.oas.models.headers.Header; import io.swagger.v3.oas.models.headers.Header;
@ -54,14 +55,19 @@ public class Swagger3Parser extends SwaggerAbstractParser {
public ApiDefinitionImport parse(String sourceStr, ApiTestImportRequest request) { public ApiDefinitionImport parse(String sourceStr, ApiTestImportRequest request) {
List<AuthorizationValue> auths = setAuths(request); List<AuthorizationValue> auths = setAuths(request);
SwaggerParseResult result; SwaggerParseResult result = null;
if (StringUtils.isNotBlank(request.getSwaggerUrl())) { if (StringUtils.isNotBlank(request.getSwaggerUrl())) {
result = new OpenAPIParser().readLocation(request.getSwaggerUrl(), auths, null); try {
result = new OpenAPIParser().readLocation(request.getSwaggerUrl(), auths, null);
} catch (Exception e) {
MSException.throwException(e.getMessage());
}
} else { } else {
result = new OpenAPIParser().readContents(sourceStr, null, null); result = new OpenAPIParser().readContents(sourceStr, null, null);
} }
if (result == null) { if (result == null || result.getOpenAPI() == null) {
MSException.throwException("解析失败,请确认选择的是 swagger 格式!"); MSException.throwException(Translator.get(CollectionUtils.isEmpty(auths) ?
"swagger_parse_error" : "swagger_parse_error_with_auth"));
} }
OpenAPI openAPI = result.getOpenAPI(); OpenAPI openAPI = result.getOpenAPI();
if (result.getMessages() != null) { if (result.getMessages() != null) {

View File

@ -1572,9 +1572,11 @@ public class ApiDefinitionService {
this.sendFailMessage(request, project); this.sendFailMessage(request, project);
MSException.throwException("文件格式不符合要求"); MSException.throwException("文件格式不符合要求");
} }
if (!UrlTestUtils.testUrlWithTimeOut(request.getSwaggerUrl(), 30000)) { try {
UrlTestUtils.testUrl(request.getSwaggerUrl(), 30000);
} catch (Exception e) {
this.sendFailMessage(request, project); this.sendFailMessage(request, project);
MSException.throwException(Translator.get("connection_timeout")); MSException.throwException(e.getMessage());
} }
} }
if (StringUtils.equals(request.getType(), "schedule")) { if (StringUtils.equals(request.getType(), "schedule")) {
@ -1585,21 +1587,17 @@ public class ApiDefinitionService {
if (apiImport.getMocks() == null) { if (apiImport.getMocks() == null) {
apiImport.setMocks(new ArrayList<>()); apiImport.setMocks(new ArrayList<>());
} }
} catch (MSException e) {
// 发送通知
this.sendFailMessage(request, project);
throw e;
} catch (Exception e) { } catch (Exception e) {
// 发送通知 // 发送通知
this.sendFailMessage(request, project); this.sendFailMessage(request, project);
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
String returnThrowException = e.getMessage(); MSException.throwException(Translator.get("parse_data_error"));
if (StringUtils.contains(returnThrowException, "模块树最大深度为")) {
MSException.throwException(returnThrowException);
} else {
if (returnThrowException.equals("wrong format")) {
MSException.throwException("文件格式不符合要求");
} else {
MSException.throwException(Translator.get("parse_data_error"));
}
}
} }
try { try {
importApi(request, apiImport); importApi(request, apiImport);
if (CollectionUtils.isNotEmpty(apiImport.getData())) { if (CollectionUtils.isNotEmpty(apiImport.getData())) {

View File

@ -3,6 +3,7 @@ package io.metersphere.commons.utils;
import io.metersphere.ldap.service.CustomSSLSocketFactory; import io.metersphere.ldap.service.CustomSSLSocketFactory;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
@ -10,16 +11,7 @@ public class UrlTestUtils {
public static boolean testUrlWithTimeOut(String address, int timeOutMillSeconds) { public static boolean testUrlWithTimeOut(String address, int timeOutMillSeconds) {
try { try {
URL urlObj = new URL(address); if (200 == testUrl(address, timeOutMillSeconds)) {
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
if (oc instanceof HttpsURLConnection) {
((HttpsURLConnection) oc).setSSLSocketFactory(new CustomSSLSocketFactory());
((HttpsURLConnection) oc).setHostnameVerifier((hostname, session) -> true);
}
oc.setUseCaches(false);
oc.setConnectTimeout(timeOutMillSeconds); // 设置超时时间
int status = oc.getResponseCode();// 请求状态
if (200 == status) {
return true; return true;
} }
} catch (Exception e) { } catch (Exception e) {
@ -28,4 +20,16 @@ public class UrlTestUtils {
} }
return false; return false;
} }
public static int testUrl(String address, int timeOutMillSeconds) throws IOException {
URL urlObj = new URL(address);
HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
if (oc instanceof HttpsURLConnection) {
((HttpsURLConnection) oc).setSSLSocketFactory(new CustomSSLSocketFactory());
((HttpsURLConnection) oc).setHostnameVerifier((hostname, session) -> true);
}
oc.setUseCaches(false);
oc.setConnectTimeout(timeOutMillSeconds); // 设置超时时间
return oc.getResponseCode();// 请求状态
}
} }

View File

@ -220,6 +220,8 @@ import_xmind_not_found=Test case not found
license_valid_license_error=Authorization authentication failed license_valid_license_error=Authorization authentication failed
test_review_task_notice=Test review task notice test_review_task_notice=Test review task notice
swagger_url_scheduled_import_notification=SwaggerUrl Scheduled import notification swagger_url_scheduled_import_notification=SwaggerUrl Scheduled import notification
Swagger_parse_error =Swagger parsing failed, please confirm file format is correct!
Swagger_parse_error_with_auth =Swagger parsing failed. Please check whether authentication information is correct or file format is correct!
test_track.length_less_than=The title is too long, the length must be less than test_track.length_less_than=The title is too long, the length must be less than
# check owner # check owner
check_owner_project=The current user does not have permission to operate this project check_owner_project=The current user does not have permission to operate this project

View File

@ -220,6 +220,8 @@ license_valid_license_error=授权认证失败
import_xmind_not_found=未找到测试用例 import_xmind_not_found=未找到测试用例
test_review_task_notice=测试评审任务通知 test_review_task_notice=测试评审任务通知
swagger_url_scheduled_import_notification=swagger_url定时导入通知 swagger_url_scheduled_import_notification=swagger_url定时导入通知
swagger_parse_error=Swagger 解析失败,请确认文件格式是否正确!
swagger_parse_error_with_auth=Swagger 解析失败,请确认认证信息是否正确或文件格式是否正确!
test_track.length_less_than=标题过长,字数必须小于 test_track.length_less_than=标题过长,字数必须小于
# check owner # check owner
check_owner_project=当前用户没有操作此项目的权限 check_owner_project=当前用户没有操作此项目的权限

View File

@ -219,6 +219,8 @@ license_valid_license_error=授權認證失敗
import_xmind_not_found=未找到測試用例 import_xmind_not_found=未找到測試用例
test_review_task_notice=測試評審任務通知 test_review_task_notice=測試評審任務通知
swagger_url_scheduled_import_notification=swagger_url定時導入通知 swagger_url_scheduled_import_notification=swagger_url定時導入通知
swagger_parse_error=Swagger 解析失敗,請確認文件格式是否正確!
swagger_parse_error_with_auth=Swagger 解析失敗,請確認認證信息是否正確或文件格式是否正確!
test_track.length_less_than=標題過長,字數必須小於 test_track.length_less_than=標題過長,字數必須小於
# check owner # check owner
check_owner_project=當前用戶沒有操作此項目的權限 check_owner_project=當前用戶沒有操作此項目的權限