diff --git a/backend/src/main/java/io/metersphere/api/controller/MockConfigController.java b/backend/src/main/java/io/metersphere/api/controller/MockConfigController.java index fd8029240e..270e9167cd 100644 --- a/backend/src/main/java/io/metersphere/api/controller/MockConfigController.java +++ b/backend/src/main/java/io/metersphere/api/controller/MockConfigController.java @@ -1,6 +1,7 @@ package io.metersphere.api.controller; import io.metersphere.api.dto.automation.TcpTreeTableDataStruct; +import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO; import io.metersphere.api.dto.mock.MockParamSuggestions; import io.metersphere.api.dto.mock.MockTestDataRequest; import io.metersphere.api.dto.mockconfig.MockConfigRequest; @@ -71,9 +72,9 @@ public class MockConfigController { } @GetMapping("/getApiResponse/{id}") - public Map getApiResponse(@PathVariable String id) { + public ApiDefinitionResponseDTO getApiResponse(@PathVariable String id) { ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionService.getBLOBs(id); - Map returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); + ApiDefinitionResponseDTO returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); return returnMap; } diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/ApiDefinitionResponseDTO.java b/backend/src/main/java/io/metersphere/api/dto/mock/ApiDefinitionResponseDTO.java new file mode 100644 index 0000000000..c48d723b38 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/mock/ApiDefinitionResponseDTO.java @@ -0,0 +1,15 @@ +package io.metersphere.api.dto.mock; + +import lombok.Getter; +import lombok.Setter; + +import java.util.HashMap; +import java.util.Map; + +@Getter +@Setter +public class ApiDefinitionResponseDTO { + private String returnData; + private int returnCode = 404; + private Map headers = new HashMap<>(); +} diff --git a/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java index 0cb2133f35..90d5dfa45f 100644 --- a/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/mock/utils/MockApiUtils.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONValidator; +import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO; import io.metersphere.api.dto.mock.MockConfigRequestParams; import io.metersphere.api.dto.mock.RequestMockParams; import io.metersphere.api.dto.mockconfig.response.JsonSchemaReturnObj; @@ -229,13 +230,13 @@ public class MockApiUtils { return requestParamsList; } - public static Map getApiResponse(String response) { - Map returnMap = new HashMap<>(); - String returnStr = ""; + public static ApiDefinitionResponseDTO getApiResponse(String response) { + ApiDefinitionResponseDTO responseDTO = new ApiDefinitionResponseDTO(); if (StringUtils.isNotEmpty(response)) { try { JSONObject respObj = JSONObject.parseObject(response); if (respObj.containsKey("body")) { + String returnStr = ""; JSONObject bodyObj = respObj.getJSONObject("body"); if (bodyObj.containsKey("type")) { String type = bodyObj.getString("type"); @@ -289,16 +290,17 @@ public class MockApiUtils { returnStr = JSONObject.toJSONString(paramMap); } } + responseDTO.setReturnData(returnStr); } if (respObj.containsKey("statusCode")) { JSONArray statusCodeArray = respObj.getJSONArray("statusCode"); + int code = 200; if (statusCodeArray != null) { for (int i = 0; i < statusCodeArray.size(); i++) { JSONObject object = statusCodeArray.getJSONObject(i); if (object.containsKey("name")) { try { - int code = Integer.parseInt(object.getString("name")); - returnMap.put("code", code + ""); + code = Integer.parseInt(object.getString("name")); break; } catch (Exception e) { LogUtil.error(e); @@ -306,13 +308,27 @@ public class MockApiUtils { } } } + responseDTO.setReturnCode(code); + } + if(respObj.containsKey("headers")){ + JSONArray jsonArray = respObj.getJSONArray("headers"); + Map headMap = new HashMap<>(); + for(int i = 0; i < jsonArray.size(); i ++){ + JSONObject headObj = jsonArray.getJSONObject(i); + if(headObj.containsKey("name") && headObj.containsKey("value") && headObj.containsKey("enable")){ + boolean enable = headObj.getBoolean("enable"); + if(enable){ + headMap.put(headObj.getString("name"),headObj.getString("value")); + } + } + } + responseDTO.setHeaders(headMap); } } catch (Exception e) { MSException.throwException(e); } } - returnMap.put("returnMsg", returnStr); - return returnMap; + return responseDTO; } public String getResultByResponseResult(JSONObject bodyObj, String url, Map headerMap, RequestMockParams requestMockParams, boolean useScript) { diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 0bb6ec1e01..d3b84ab8a1 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -9,6 +9,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct; import io.metersphere.api.dto.automation.TcpTreeTableDataStruct; import io.metersphere.api.dto.automation.parse.TcpTreeTableDataParser; import io.metersphere.api.dto.definition.parse.ApiDefinitionImport; +import io.metersphere.api.dto.mock.ApiDefinitionResponseDTO; import io.metersphere.api.dto.mock.MockConfigRequestParams; import io.metersphere.api.dto.mock.MockParamSuggestions; import io.metersphere.api.dto.mock.RequestMockParams; @@ -726,10 +727,10 @@ public class MockConfigService { if (responseJsonObj.containsKey("body")) { MockApiUtils mockApiUtils = new MockApiUtils(); boolean useScript = false; - if(responseJsonObj.containsKey("usePostScript")){ + if (responseJsonObj.containsKey("usePostScript")) { useScript = responseJsonObj.getBoolean("usePostScript"); } - returnStr = mockApiUtils.getResultByResponseResult(responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams,useScript); + returnStr = mockApiUtils.getResultByResponseResult(responseJsonObj.getJSONObject("body"), url, headerMap, requestMockParams, useScript); } if (responseJsonObj.containsKey("httpCode")) { int httpCodeNum = 500; @@ -1131,19 +1132,10 @@ public class MockConfigService { returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response); break; } - if(!isMatch){ - Map apiResponseMap = MockApiUtils.getApiResponse(api.getResponse()); - if(MapUtils.isNotEmpty(apiResponseMap)){ - returnStr = apiResponseMap.get("returnMsg"); - if(StringUtils.isNotEmpty(returnStr)){ - isMatch = true; - int code = 200; - if(apiResponseMap.containsKey("code")){ - code = Integer.parseInt(apiResponseMap.get("code")); - } - response.setStatus(code); - break; - } + if (!isMatch) { + returnStr = this.getApiDefinitionResponse(api, response); + if (StringUtils.isNotEmpty(returnStr)) { + isMatch = true; } } } @@ -1188,19 +1180,10 @@ public class MockConfigService { break; } } - if(!isMatch){ - Map apiResponseMap = MockApiUtils.getApiResponse(api.getResponse()); - if(MapUtils.isNotEmpty(apiResponseMap)){ - returnStr = apiResponseMap.get("returnMsg"); - if(StringUtils.isNotEmpty(returnStr)){ - isMatch = true; - int code = 200; - if(apiResponseMap.containsKey("code")){ - code = Integer.parseInt(apiResponseMap.get("code")); - } - response.setStatus(code); - break; - } + if (!isMatch) { + returnStr = this.getApiDefinitionResponse(api, response); + if (StringUtils.isNotEmpty(returnStr)) { + isMatch = true; } } } @@ -1213,6 +1196,24 @@ public class MockConfigService { return returnStr; } + private String getApiDefinitionResponse(ApiDefinitionWithBLOBs api, HttpServletResponse response) { + String returnStr = null; + try { + ApiDefinitionResponseDTO responseDTO = MockApiUtils.getApiResponse(api.getResponse()); + returnStr = responseDTO.getReturnData(); + response.setStatus(responseDTO.getReturnCode()); + if (MapUtils.isNotEmpty(responseDTO.getHeaders())) { + for (Map.Entry entry : responseDTO.getHeaders().entrySet()) { + response.setHeader(entry.getKey(), entry.getValue()); + } + } + } catch (Exception e) { + LogUtil.error(e); + } + return returnStr; + } + + private List parseByJsonDataStruct(String dataString) { List returnList = new ArrayList<>(); try { @@ -1294,7 +1295,7 @@ public class MockConfigService { JSONObject sourceObj = XMLUtils.XmlToJson(message); try { List tcpDataList = JSONArray.parseArray(requestJson.getString("xmlDataStruct"), TcpTreeTableDataStruct.class); - isMatch = TcpTreeTableDataParser.isMatchTreeTableData(sourceObj,tcpDataList); + isMatch = TcpTreeTableDataParser.isMatchTreeTableData(sourceObj, tcpDataList); } catch (Exception e) { LogUtil.error(e); } @@ -1474,8 +1475,8 @@ public class MockConfigService { if (apiDefinitionWithBLOBs == null) { return; } - Map returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); - if (MapUtils.isEmpty(returnMap) || !returnMap.containsKey("returnMsg")) { + ApiDefinitionResponseDTO responseDTO = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse()); + if (StringUtils.isEmpty(responseDTO.getReturnData())) { return; } List updateList = this.selectMockExpectConfigByApiId(apiDefinitionWithBLOBs.getId()); @@ -1487,7 +1488,7 @@ public class MockConfigService { if (responseObj.containsKey("responseResult")) { JSONObject responseResultObject = responseObj.getJSONObject("responseResult"); if (responseResultObject.containsKey("body")) { - responseResultObject.getJSONObject("body").put("apiRspRaw", returnMap.get("returnMsg")); + responseResultObject.getJSONObject("body").put("apiRspRaw", responseDTO.getReturnData()); model.setResponse(responseObj.toJSONString()); mockExpectConfigMapper.updateByPrimaryKeySelective(model); diff --git a/frontend/src/business/components/api/definition/components/mock/Components/MockApiResponseBody.vue b/frontend/src/business/components/api/definition/components/mock/Components/MockApiResponseBody.vue index 85f29829a1..ec0bf7cd58 100644 --- a/frontend/src/business/components/api/definition/components/mock/Components/MockApiResponseBody.vue +++ b/frontend/src/business/components/api/definition/components/mock/Components/MockApiResponseBody.vue @@ -258,7 +258,7 @@ export default { this.$get(selectUrl, response => { let apiResponse = response.data; if (apiResponse && apiResponse.returnMsg) { - this.body.apiRspRaw = apiResponse.returnMsg; + this.body.apiRspRaw = apiResponse.returnData; } this.refreshMsCodeEdit(); });