fix(接口测试): 修复mock请求,匹配不到mock期望的时候,没有返回接口定义的响应头的问题

--bug=1010332 --user=宋天阳 【接口测试】mock请求,匹配不到mock期望的时候,没有返回接口定义的响应头
https://www.tapd.cn/55049933/s/1106301
This commit is contained in:
song-tianyang 2022-02-21 11:26:02 +08:00 committed by fit2-zhao
parent d759253d3e
commit 5aca315a01
5 changed files with 75 additions and 42 deletions

View File

@ -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<String, String> getApiResponse(@PathVariable String id) {
public ApiDefinitionResponseDTO getApiResponse(@PathVariable String id) {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionService.getBLOBs(id);
Map<String, String> returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
ApiDefinitionResponseDTO returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
return returnMap;
}

View File

@ -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<String,String> headers = new HashMap<>();
}

View File

@ -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<String, String> getApiResponse(String response) {
Map<String, String> 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<String,String> 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<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {

View File

@ -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<String,String> 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<String,String> 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<String, String> entry : responseDTO.getHeaders().entrySet()) {
response.setHeader(entry.getKey(), entry.getValue());
}
}
} catch (Exception e) {
LogUtil.error(e);
}
return returnStr;
}
private List<String> parseByJsonDataStruct(String dataString) {
List<String> returnList = new ArrayList<>();
try {
@ -1294,7 +1295,7 @@ public class MockConfigService {
JSONObject sourceObj = XMLUtils.XmlToJson(message);
try {
List<TcpTreeTableDataStruct> 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<String, String> 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<MockExpectConfigWithBLOBs> 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);

View File

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