refactor(接口测试): 优化mock执行

This commit is contained in:
wxg0103 2024-05-10 19:31:51 +08:00 committed by Craftsman
parent 457d709102
commit 83062061c9
3 changed files with 45 additions and 25 deletions

View File

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

View File

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

View File

@ -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,33 +45,48 @@ 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();
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);
}
if (StringUtils.isNotEmpty(fileName)) {
queryParamsMap.computeIfPresent(name, (key, currentValue) -> {
List<String> current = JSON.parseArray(currentValue, String.class);
current.add(fileName);
return JSON.toJSONString(current);
});
if (!queryParamsMap.containsKey(name)) {
List<String> current = new ArrayList<>();
current.add(fileName);
queryParamsMap.put(name, JSON.toJSONString(current));
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]);
}
}
}
}catch (Exception e){
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)) {
bodyParams.put(name, value);
}
if (StringUtils.isNotEmpty(fileName)) {
bodyParams.computeIfPresent(name, (key, currentValue) -> {
List<String> current = JSON.parseArray(currentValue, String.class);
current.add(fileName);
return JSON.toJSONString(current);
});
if (!bodyParams.containsKey(name)) {
List<String> current = new ArrayList<>();
current.add(fileName);
bodyParams.put(name, JSON.toJSONString(current));
}
}
requestParam.setBodyParamsObj(bodyParams);
}
}
} catch (Exception e) {
LogUtils.error(e.getMessage());
}