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

This commit is contained in:
WangXu10 2024-09-23 18:12:00 +08:00 committed by Craftsman
parent 895628a48e
commit d1122b8958
3 changed files with 13 additions and 16 deletions

View File

@ -46,7 +46,7 @@ public interface CurlPatternConstants {
* -d/--data 请求体
*/
Pattern DEFAULT_HTTP_BODY_PATTERN = Pattern.compile("(?:--data|-d)\\s+(?:'([^']*)'|\"([^\"]*)\"|(\\S+))", Pattern.DOTALL);
Pattern DEFAULT_HTTP_BODY_PATTERN_KV = Pattern.compile("^([^=&]+=[^=&]+)(?:&[^=&]+=[^=&]+)*$", Pattern.DOTALL);
Pattern DEFAULT_HTTP_BODY_PATTERN_KV = Pattern.compile("^([^=&]+=[^=&]*)(?:&[^=&]+=[^=&]*)*$", Pattern.DOTALL);
/**
* --data-raw 请求体

View File

@ -34,7 +34,7 @@ public class CurlEntity {
/**
* 请求体
*/
private Map<String, Object> body;
private Object body;
private String bodyType;

View File

@ -61,7 +61,7 @@ public class HttpBodyHandler extends CurlHandlerChain {
}
}
private Map<String, Object> parseDefaultBody(Matcher defaultMatcher, CurlEntity entity) {
private Object parseDefaultBody(Matcher defaultMatcher, CurlEntity entity) {
String bodyStr = "";
if (defaultMatcher.group(1) != null) {
//单引号数据
@ -78,6 +78,10 @@ public class HttpBodyHandler extends CurlHandlerChain {
entity.setBodyType(Body.BodyType.JSON.name());
return JSON.parseMap(bodyStr);
}
if(isXML(bodyStr)){
entity.setBodyType(Body.BodyType.XML.name());
return bodyStr;
}
//其他格式 a=b&c=d
entity.setBodyType(Body.BodyType.WWW_FORM.name());
@ -136,12 +140,12 @@ public class HttpBodyHandler extends CurlHandlerChain {
return urlEncodeData;
}
private Map<String, Object> parseRowBody(Matcher rowMatcher, CurlEntity entity) {
private Object parseRowBody(Matcher rowMatcher, CurlEntity entity) {
String rawData = rowMatcher.group(1);
if (isXML(rawData)) {
entity.setBodyType(Body.BodyType.XML.name());
return xml2json(rawData);
return rawData;
}
if (isJSON(rawData)) {
@ -168,9 +172,10 @@ public class HttpBodyHandler extends CurlHandlerChain {
public static boolean isXML(String xmlStr) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("disallow-doctype-decl", false);
factory.setFeature("external-general-entities", false);
factory.setFeature("external-parameter-entities", false);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlStr));
@ -181,12 +186,4 @@ public class HttpBodyHandler extends CurlHandlerChain {
}
}
private Map<String, Object> xml2json(String xmlStr) {
try {
return XMLUtils.xmlStringToJson(xmlStr);
} catch (JSONException e) {
throw new MSException(Translator.get("curl_raw_content_is_invalid"), e);
}
}
}