refactor(接口测试): 接口调试curl导入优化

This commit is contained in:
WangXu10 2024-08-29 11:41:32 +08:00 committed by Craftsman
parent 638e4ec6df
commit c78ccb502c
4 changed files with 25 additions and 4 deletions

View File

@ -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.*");
/**
* 请求头

View File

@ -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);
}

View File

@ -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

View File

@ -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);
}