diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/HttpRequestParam.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/HttpRequestParam.java index 9633c5ab66..6664b10baa 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/HttpRequestParam.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/HttpRequestParam.java @@ -17,6 +17,8 @@ public class HttpRequestParam { //form-data的kv类型参数也存储在queryParamObj中 private LinkedHashMap queryParamsObj; + private LinkedHashMap bodyParamsObj; + private String paramType; //JSONArray 或 JSONObject diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java index 3e7331e3a2..b8e6aa8293 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockMatchRule.java @@ -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; } } \ No newline at end of file diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/MockServerUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/MockServerUtils.java index 17bcfa03e8..30ff05dca6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/MockServerUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/MockServerUtils.java @@ -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 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 current = JSON.parseArray(currentValue, String.class); - current.add(fileName); - return JSON.toJSONString(current); - }); - if (!queryParamsMap.containsKey(name)) { - List 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 bodyParams = new LinkedHashMap<>(); + Collection 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 current = JSON.parseArray(currentValue, String.class); + current.add(fileName); + return JSON.toJSONString(current); + }); + if (!bodyParams.containsKey(name)) { + List current = new ArrayList<>(); + current.add(fileName); + bodyParams.put(name, JSON.toJSONString(current)); + } + } + requestParam.setBodyParamsObj(bodyParams); + } + } + } catch (Exception e) { LogUtils.error(e.getMessage()); }