refactor(接口测试): curl导入优化
This commit is contained in:
parent
895628a48e
commit
d1122b8958
|
@ -46,7 +46,7 @@ public interface CurlPatternConstants {
|
||||||
* -d/--data 请求体
|
* -d/--data 请求体
|
||||||
*/
|
*/
|
||||||
Pattern DEFAULT_HTTP_BODY_PATTERN = Pattern.compile("(?:--data|-d)\\s+(?:'([^']*)'|\"([^\"]*)\"|(\\S+))", Pattern.DOTALL);
|
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 请求体
|
* --data-raw 请求体
|
||||||
|
|
|
@ -34,7 +34,7 @@ public class CurlEntity {
|
||||||
/**
|
/**
|
||||||
* 请求体
|
* 请求体
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> body;
|
private Object body;
|
||||||
|
|
||||||
private String bodyType;
|
private String bodyType;
|
||||||
|
|
||||||
|
|
|
@ -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 = "";
|
String bodyStr = "";
|
||||||
if (defaultMatcher.group(1) != null) {
|
if (defaultMatcher.group(1) != null) {
|
||||||
//单引号数据
|
//单引号数据
|
||||||
|
@ -78,6 +78,10 @@ public class HttpBodyHandler extends CurlHandlerChain {
|
||||||
entity.setBodyType(Body.BodyType.JSON.name());
|
entity.setBodyType(Body.BodyType.JSON.name());
|
||||||
return JSON.parseMap(bodyStr);
|
return JSON.parseMap(bodyStr);
|
||||||
}
|
}
|
||||||
|
if(isXML(bodyStr)){
|
||||||
|
entity.setBodyType(Body.BodyType.XML.name());
|
||||||
|
return bodyStr;
|
||||||
|
}
|
||||||
|
|
||||||
//其他格式 a=b&c=d
|
//其他格式 a=b&c=d
|
||||||
entity.setBodyType(Body.BodyType.WWW_FORM.name());
|
entity.setBodyType(Body.BodyType.WWW_FORM.name());
|
||||||
|
@ -136,12 +140,12 @@ public class HttpBodyHandler extends CurlHandlerChain {
|
||||||
return urlEncodeData;
|
return urlEncodeData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> parseRowBody(Matcher rowMatcher, CurlEntity entity) {
|
private Object parseRowBody(Matcher rowMatcher, CurlEntity entity) {
|
||||||
String rawData = rowMatcher.group(1);
|
String rawData = rowMatcher.group(1);
|
||||||
|
|
||||||
if (isXML(rawData)) {
|
if (isXML(rawData)) {
|
||||||
entity.setBodyType(Body.BodyType.XML.name());
|
entity.setBodyType(Body.BodyType.XML.name());
|
||||||
return xml2json(rawData);
|
return rawData;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isJSON(rawData)) {
|
if (isJSON(rawData)) {
|
||||||
|
@ -168,9 +172,10 @@ public class HttpBodyHandler extends CurlHandlerChain {
|
||||||
public static boolean isXML(String xmlStr) {
|
public static boolean isXML(String xmlStr) {
|
||||||
try {
|
try {
|
||||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
factory.setFeature("disallow-doctype-decl", false);
|
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
|
||||||
factory.setFeature("external-general-entities", false);
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||||
factory.setFeature("external-parameter-entities", false);
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||||
|
|
||||||
|
|
||||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||||
InputSource is = new InputSource(new StringReader(xmlStr));
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue