refactor(接口测试): 优化mock执行
This commit is contained in:
parent
457d709102
commit
83062061c9
|
@ -17,6 +17,8 @@ public class HttpRequestParam {
|
|||
//form-data的kv类型参数也存储在queryParamObj中
|
||||
private LinkedHashMap<String, String> queryParamsObj;
|
||||
|
||||
private LinkedHashMap<String, String> bodyParamsObj;
|
||||
|
||||
private String paramType;
|
||||
|
||||
//JSONArray 或 JSONObject
|
||||
|
|
|
@ -42,6 +42,9 @@ public class MockMatchRule implements Serializable {
|
|||
if (!this.keyValueMatch("rest", httpRequestParam.getRestParams())) {
|
||||
return false;
|
||||
}
|
||||
if (!this.keyValueMatch("query", httpRequestParam.getQueryParamsObj())) {
|
||||
return false;
|
||||
}
|
||||
if (httpRequestParam.isPost()) {
|
||||
switch (Body.BodyType.valueOf(body.getBodyType())) {
|
||||
case XML:
|
||||
|
@ -67,16 +70,15 @@ public class MockMatchRule implements Serializable {
|
|||
formDataBodyRule.getMatchRules().add(keyValueInfo);
|
||||
});
|
||||
}
|
||||
return formDataBodyRule.match(httpRequestParam.getQueryParamsObj());
|
||||
return formDataBodyRule.match(httpRequestParam.getBodyParamsObj());
|
||||
case RAW:
|
||||
return StringUtils.contains(body.getRawBody().getValue(), httpRequestParam.getRaw());
|
||||
case WWW_FORM:
|
||||
return body.getWwwFormBody().match(httpRequestParam.getQueryParamsObj());
|
||||
return body.getWwwFormBody().match(httpRequestParam.getBodyParamsObj());
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return this.keyValueMatch("query", httpRequestParam.getQueryParamsObj());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import jakarta.servlet.http.Part;
|
|||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
@ -44,31 +45,46 @@ public class MockServerUtils {
|
|||
try {
|
||||
if (request instanceof ShiroHttpServletRequest shiroHttpServletRequest) {
|
||||
InputStream inputStream = shiroHttpServletRequest.getRequest().getInputStream();
|
||||
if (inputStream != null) {
|
||||
if (inputStream != null && inputStream.available() > 0) {
|
||||
queryParamsMap.put("binaryFile", new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).readLine());
|
||||
}
|
||||
}
|
||||
Collection<Part> parts = request.getParts();
|
||||
String queryString = request.getQueryString();
|
||||
if (StringUtils.isNotEmpty(queryString)) {
|
||||
String[] queryParamArr = queryString.split("&");
|
||||
for (String queryParam : queryParamArr) {
|
||||
String[] queryParamKV = queryParam.split("=");
|
||||
if (queryParamKV.length == 2) {
|
||||
queryParamsMap.put(queryParamKV[0], queryParamKV[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
requestParam.setQueryParamsObj(queryParamsMap);
|
||||
if (request instanceof StandardMultipartHttpServletRequest standardMultipartHttpServletRequest) {
|
||||
LinkedHashMap<String, String> bodyParams = new LinkedHashMap<>();
|
||||
Collection<Part> parts = standardMultipartHttpServletRequest.getParts();
|
||||
for (Part part : parts) {
|
||||
String name = part.getName();
|
||||
String fileName = part.getSubmittedFileName();
|
||||
String value = new BufferedReader(new InputStreamReader(part.getInputStream(), StandardCharsets.UTF_8)).readLine();
|
||||
|
||||
if (StringUtils.isBlank(fileName)) {
|
||||
queryParamsMap.put(name, value);
|
||||
bodyParams.put(name, value);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(fileName)) {
|
||||
queryParamsMap.computeIfPresent(name, (key, currentValue) -> {
|
||||
bodyParams.computeIfPresent(name, (key, currentValue) -> {
|
||||
List<String> current = JSON.parseArray(currentValue, String.class);
|
||||
current.add(fileName);
|
||||
return JSON.toJSONString(current);
|
||||
});
|
||||
if (!queryParamsMap.containsKey(name)) {
|
||||
if (!bodyParams.containsKey(name)) {
|
||||
List<String> current = new ArrayList<>();
|
||||
current.add(fileName);
|
||||
queryParamsMap.put(name, JSON.toJSONString(current));
|
||||
bodyParams.put(name, JSON.toJSONString(current));
|
||||
}
|
||||
}
|
||||
requestParam.setBodyParamsObj(bodyParams);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtils.error(e.getMessage());
|
||||
|
|
Loading…
Reference in New Issue