feat(接口测试): curl增加请求体类型

This commit is contained in:
WangXu10 2024-09-03 11:06:20 +08:00 committed by Craftsman
parent c35d1cd8b3
commit 3710ed3370
3 changed files with 37 additions and 33 deletions

View File

@ -14,6 +14,7 @@ import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.api.task.TaskRequestDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.dto.sdk.BaseTreeNode; import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;

View File

@ -2,7 +2,6 @@ package io.metersphere.api.curl.domain;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import org.json.JSONObject;
import java.util.Map; import java.util.Map;
@ -35,7 +34,9 @@ public class CurlEntity {
/** /**
* 请求体 * 请求体
*/ */
private JSONObject body; private Map<String, Object> body;
private String bodyType;
public enum Method { public enum Method {
GET, GET,

View File

@ -2,12 +2,13 @@ package io.metersphere.api.curl.handler;
import io.metersphere.api.curl.constants.CurlPatternConstants; import io.metersphere.api.curl.constants.CurlPatternConstants;
import io.metersphere.api.curl.domain.CurlEntity; import io.metersphere.api.curl.domain.CurlEntity;
import io.metersphere.api.dto.request.http.body.Body;
import io.metersphere.api.utils.JSONUtil; import io.metersphere.api.utils.JSONUtil;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.sdk.util.XMLUtils;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
@ -15,6 +16,8 @@ import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader; import java.io.StringReader;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -24,8 +27,7 @@ import java.util.regex.Matcher;
public class HttpBodyHandler extends CurlHandlerChain { public class HttpBodyHandler extends CurlHandlerChain {
@Override @Override
public void handle(CurlEntity entity, String curl) { public void handle(CurlEntity entity, String curl) {
JSONObject body = parseBody(curl); parseBody(curl, entity);
entity.setBody(body);
super.nextHandle(entity, curl); super.nextHandle(entity, curl);
} }
@ -35,31 +37,31 @@ public class HttpBodyHandler extends CurlHandlerChain {
* @param curl * @param curl
* @return * @return
*/ */
private JSONObject parseBody(String curl) { private void parseBody(String curl, CurlEntity entity) {
Matcher formMatcher = CurlPatternConstants.HTTP_FROM_BODY_PATTERN.matcher(curl); Matcher formMatcher = CurlPatternConstants.HTTP_FROM_BODY_PATTERN.matcher(curl);
if (formMatcher.find()) { if (formMatcher.find()) {
return parseFormBody(formMatcher); entity.setBodyType(Body.BodyType.FORM_DATA.name());
entity.setBody(parseFormBody(formMatcher));
} }
Matcher urlencodeMatcher = CurlPatternConstants.HTTP_URLENCODE_BODY_PATTERN.matcher(curl); Matcher urlencodeMatcher = CurlPatternConstants.HTTP_URLENCODE_BODY_PATTERN.matcher(curl);
if (urlencodeMatcher.find()) { if (urlencodeMatcher.find()) {
return parseUrlEncodeBody(urlencodeMatcher); entity.setBodyType(Body.BodyType.WWW_FORM.name());
entity.setBody(parseUrlEncodeBody(urlencodeMatcher));
} }
Matcher rawMatcher = CurlPatternConstants.HTTP_ROW_BODY_PATTERN.matcher(curl); Matcher rawMatcher = CurlPatternConstants.HTTP_ROW_BODY_PATTERN.matcher(curl);
if (rawMatcher.find()) { if (rawMatcher.find()) {
return parseRowBody(rawMatcher); entity.setBody(parseRowBody(rawMatcher, entity));
} }
Matcher defaultMatcher = CurlPatternConstants.DEFAULT_HTTP_BODY_PATTERN.matcher(curl); Matcher defaultMatcher = CurlPatternConstants.DEFAULT_HTTP_BODY_PATTERN.matcher(curl);
if (defaultMatcher.find()) { if (defaultMatcher.find()) {
return parseDefaultBody(defaultMatcher); entity.setBody(parseDefaultBody(defaultMatcher, entity));
} }
return new JSONObject();
} }
private JSONObject parseDefaultBody(Matcher defaultMatcher) { private Map<String, Object> parseDefaultBody(Matcher defaultMatcher, CurlEntity entity) {
String bodyStr = ""; String bodyStr = "";
if (defaultMatcher.group(1) != null) { if (defaultMatcher.group(1) != null) {
//单引号数据 //单引号数据
@ -73,29 +75,30 @@ public class HttpBodyHandler extends CurlHandlerChain {
} }
if (isJSON(bodyStr)) { if (isJSON(bodyStr)) {
return JSONUtil.parseObject(bodyStr); entity.setBodyType(Body.BodyType.JSON.name());
return JSON.parseMap(bodyStr);
} }
//其他格式 a=b&c=d //其他格式 a=b&c=d
entity.setBodyType(Body.BodyType.WWW_FORM.name());
Matcher kvMatcher = CurlPatternConstants.DEFAULT_HTTP_BODY_PATTERN_KV.matcher(bodyStr); Matcher kvMatcher = CurlPatternConstants.DEFAULT_HTTP_BODY_PATTERN_KV.matcher(bodyStr);
return kvMatcher.matches() ? parseKVBody(bodyStr) : new JSONObject(); return kvMatcher.matches() ? parseKVBody(bodyStr) : new HashMap<>();
} }
private JSONObject parseKVBody(String kvBodyStr) { private Map<String, Object> parseKVBody(String kvBodyStr) {
JSONObject json = new JSONObject(); Map<String, Object> map = new HashMap<>();
String[] pairs = kvBodyStr.split("&"); String[] pairs = kvBodyStr.split("&");
for (String pair : pairs) { for (String pair : pairs) {
int idx = pair.indexOf("="); int idx = pair.indexOf("=");
String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8); String key = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8);
String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8); String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8);
json.put(key, value); map.put(key, value);
} }
return json; return map;
} }
private JSONObject parseFormBody(Matcher formMatcher) { private Map<String, Object> parseFormBody(Matcher formMatcher) {
JSONObject formData = new JSONObject(); Map<String, Object> formData = new HashMap<>();
formMatcher.reset(); formMatcher.reset();
while (formMatcher.find()) { while (formMatcher.find()) {
//提取表单 //提取表单
@ -114,12 +117,11 @@ public class HttpBodyHandler extends CurlHandlerChain {
} }
} }
} }
return formData; return formData;
} }
private JSONObject parseUrlEncodeBody(Matcher urlencodeMatcher) { private Map<String, Object> parseUrlEncodeBody(Matcher urlencodeMatcher) {
JSONObject urlEncodeData = new JSONObject(); Map<String, Object> urlEncodeData = new HashMap<>();
urlencodeMatcher.reset(); urlencodeMatcher.reset();
while (urlencodeMatcher.find()) { while (urlencodeMatcher.find()) {
String keyValueEncoded = urlencodeMatcher.group(1); String keyValueEncoded = urlencodeMatcher.group(1);
@ -134,19 +136,21 @@ public class HttpBodyHandler extends CurlHandlerChain {
return urlEncodeData; return urlEncodeData;
} }
private JSONObject parseRowBody(Matcher rowMatcher) { private Map<String, 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());
return xml2json(rawData); return xml2json(rawData);
} }
if (isJSON(rawData)) { if (isJSON(rawData)) {
return JSONUtil.parseObject(rawData); entity.setBodyType(Body.BodyType.JSON.name());
return JSON.parseMap(rawData);
} }
try { try {
return parseDefaultBody(rowMatcher); return parseDefaultBody(rowMatcher, entity);
} catch (Exception e) { } catch (Exception e) {
throw new MSException(Translator.get("curl_raw_content_is_invalid"), e); throw new MSException(Translator.get("curl_raw_content_is_invalid"), e);
} }
@ -177,11 +181,9 @@ public class HttpBodyHandler extends CurlHandlerChain {
} }
} }
private JSONObject xml2json(String xmlStr) { private Map<String, Object> xml2json(String xmlStr) {
try { try {
JSONObject orgJsonObj = XML.toJSONObject(xmlStr); return XMLUtils.xmlStringToJson(xmlStr);
String jsonString = orgJsonObj.toString();
return JSONUtil.parseObject(jsonString);
} catch (JSONException e) { } catch (JSONException e) {
throw new MSException(Translator.get("curl_raw_content_is_invalid"), e); throw new MSException(Translator.get("curl_raw_content_is_invalid"), e);
} }