From c78ccb502c0a34150750ce8f46e24b121a4b4010 Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Thu, 29 Aug 2024 11:41:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E8=B0=83=E8=AF=95curl=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../curl/constants/CurlPatternConstants.java | 4 ++-- .../api/curl/handler/HttpBodyHandler.java | 6 +++++- .../api/curl/handler/HttpMethodHandler.java | 2 +- .../api/controller/ApiDebugControllerTests.java | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 4 deletions(-) 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 c0789b3946..0ad593cfd0 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,12 +25,12 @@ public interface CurlPatternConstants { /** * HTTP请求方法 */ - Pattern HTTP_METHOD_PATTERN = Pattern.compile("curl\\s+[^\\s]*\\s+(?:-X|--request)\\s+'?(GET|POST)'?"); + Pattern HTTP_METHOD_PATTERN = Pattern.compile("curl\\s+(?:[^\\s]+\\s+)*(-X|--request)\\s+'?(GET|POST)'?"); /** * 默认HTTP请求方法 */ - Pattern DEFAULT_HTTP_METHOD_PATTERN = Pattern.compile(".*\\s(-d|--data|--data-binary)\\s.*"); + Pattern DEFAULT_HTTP_METHOD_PATTERN = Pattern.compile(".*\\s(-d|--data|--data-binary|--data-raw|-F)\\s.*"); /** * 请求头 diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java index 4ab8492693..5efe02aaa8 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/curl/handler/HttpBodyHandler.java @@ -141,8 +141,12 @@ public class HttpBodyHandler extends CurlHandlerChain { return xml2json(rawData); } - try { + if (isJSON(rawData)) { return JSONUtil.parseObject(rawData); + } + + try { + return parseDefaultBody(rowMatcher); } catch (Exception e) { throw new MSException(Translator.get("curl_raw_content_is_invalid"), e); } 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 9e3c49b88a..c994e69b9b 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 @@ -27,7 +27,7 @@ public class HttpMethodHandler extends CurlHandlerChain { Matcher matcher = CurlPatternConstants.HTTP_METHOD_PATTERN.matcher(curl); Matcher defaultMatcher = CurlPatternConstants.DEFAULT_HTTP_METHOD_PATTERN.matcher(curl); if (matcher.find()) { - String method = matcher.group(1); + String method = matcher.group(2); return CurlEntity.Method.valueOf(method.toUpperCase()); } else if (defaultMatcher.find()) { //如果命令中包含 -d 或 --data,没有明确请求方法,默认为 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 5d5533845a..abaeaa1385 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 @@ -942,6 +942,23 @@ public class ApiDebugControllerTests extends BaseTest { curl = "curl -X POST -H 'Content-Type: application/json' --data-urlencode '{\"key\":\"value\"}' https://example.com/post"; request.setCurl(curl); this.requestPostWithOk(IMPORT_CURL, request); + + + curl = "curl 'http://xx:xx/test/tst1/orderAction/tes' \\\n" + + " -H 'Accept: application/json, text/plain, /' \\\n" + + " -H 'Accept-Language: zh-CN,zh;q=0.9' \\\n" + + " -H 'Content-Type: application/x-www-form-urlencoded' \\\n" + + " -H 'Cookie: _ati=4621860983598; saas_oms_session=hhh%3D' \\\n" + + " -H 'Origin: http://xx:xx' \\\n" + + " -H 'Proxy-Connection: keep-alive'\n" + + " -H 'Referer: http://xx:xx/order/eCommerceBus/b2cOrder' \\\n" + + " -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36' \\\n" + + " -H 'X-Requested-With: XMLHttpRequest' \\\n" + + " -H 'token: xx.yy.hh' \\\n" + + " --data-raw 'channel_type=1&channel_id=84&action_type=1&tid=44444' \\\n" + + " --insecure"; + request.setCurl(curl); + this.requestPostWithOk(IMPORT_CURL, request); }