From a02322ed43cc4b2686c875735fc2c00e1077669f Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Wed, 25 Sep 2024 10:25:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20he?= =?UTF-8?q?ad=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1046993 --user=王旭 【接口测试】场景自定义请求-curl导入head请求-导入后请求方法为get https://www.tapd.cn/55049933/s/1585097 --- .../api/curl/constants/CurlPatternConstants.java | 3 ++- .../metersphere/api/curl/handler/HttpMethodHandler.java | 3 +++ .../api/controller/ApiDebugControllerTests.java | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java index 2102dbf7cf..bcaf1160a4 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/constants/CurlPatternConstants.java @@ -25,7 +25,8 @@ public interface CurlPatternConstants { /** * HTTP请求方法 */ - Pattern HTTP_METHOD_PATTERN = Pattern.compile("curl\\s+(?:[^\\s]+\\s+)*(-X|--request)\\s+'?(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|CONNECT)'?"); + Pattern HTTP_METHOD_PATTERN = Pattern.compile("curl\\s+(?:[^\\s]+\\s+)*(-X|--request|--head)\\s+'?(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|CONNECT)'?"); + Pattern HTTP_HEAD_METHOD_PATTERN = Pattern.compile("curl\\s+(?:[^\\s]+\\s+)*(--head|-I)\\s"); /** * 默认HTTP请求方法 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpMethodHandler.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpMethodHandler.java index c994e69b9b..dc479ea1c1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpMethodHandler.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpMethodHandler.java @@ -26,9 +26,12 @@ public class HttpMethodHandler extends CurlHandlerChain { private CurlEntity.Method parseMethod(String curl) { Matcher matcher = CurlPatternConstants.HTTP_METHOD_PATTERN.matcher(curl); Matcher defaultMatcher = CurlPatternConstants.DEFAULT_HTTP_METHOD_PATTERN.matcher(curl); + Matcher headMatcher = CurlPatternConstants.HTTP_HEAD_METHOD_PATTERN.matcher(curl); if (matcher.find()) { String method = matcher.group(2); return CurlEntity.Method.valueOf(method.toUpperCase()); + } else if (headMatcher.find()) { + return CurlEntity.Method.HEAD; } else if (defaultMatcher.find()) { //如果命令中包含 -d 或 --data,没有明确请求方法,默认为 POST return CurlEntity.Method.POST; diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java index abaeaa1385..3358ad0208 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java @@ -959,6 +959,14 @@ public class ApiDebugControllerTests extends BaseTest { " --insecure"; request.setCurl(curl); this.requestPostWithOk(IMPORT_CURL, request); + + curl = "curl -I https://example.com"; + request.setCurl(curl); + this.requestPostWithOk(IMPORT_CURL, request); + + curl = "curl --head https://example.com"; + request.setCurl(curl); + this.requestPostWithOk(IMPORT_CURL, request); }