fix(接口测试): 修复mock请求,匹配不到mock期望的时候,没有返回接口定义的响应头的问题
--bug=1010332 --user=宋天阳 【接口测试】mock请求,匹配不到mock期望的时候,没有返回接口定义的响应头 https://www.tapd.cn/55049933/s/1106301
This commit is contained in:
parent
d759253d3e
commit
5aca315a01
|
@ -1,6 +1,7 @@
|
||||||
package io.metersphere.api.controller;
|
package io.metersphere.api.controller;
|
||||||
|
|
||||||
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
|
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.MockParamSuggestions;
|
||||||
import io.metersphere.api.dto.mock.MockTestDataRequest;
|
import io.metersphere.api.dto.mock.MockTestDataRequest;
|
||||||
import io.metersphere.api.dto.mockconfig.MockConfigRequest;
|
import io.metersphere.api.dto.mockconfig.MockConfigRequest;
|
||||||
|
@ -71,9 +72,9 @@ public class MockConfigController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/getApiResponse/{id}")
|
@GetMapping("/getApiResponse/{id}")
|
||||||
public Map<String, String> getApiResponse(@PathVariable String id) {
|
public ApiDefinitionResponseDTO getApiResponse(@PathVariable String id) {
|
||||||
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionService.getBLOBs(id);
|
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionService.getBLOBs(id);
|
||||||
Map<String, String> returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
|
ApiDefinitionResponseDTO returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
|
||||||
return returnMap;
|
return returnMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<>();
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.alibaba.fastjson.JSONValidator;
|
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.MockConfigRequestParams;
|
||||||
import io.metersphere.api.dto.mock.RequestMockParams;
|
import io.metersphere.api.dto.mock.RequestMockParams;
|
||||||
import io.metersphere.api.dto.mockconfig.response.JsonSchemaReturnObj;
|
import io.metersphere.api.dto.mockconfig.response.JsonSchemaReturnObj;
|
||||||
|
@ -229,13 +230,13 @@ public class MockApiUtils {
|
||||||
return requestParamsList;
|
return requestParamsList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, String> getApiResponse(String response) {
|
public static ApiDefinitionResponseDTO getApiResponse(String response) {
|
||||||
Map<String, String> returnMap = new HashMap<>();
|
ApiDefinitionResponseDTO responseDTO = new ApiDefinitionResponseDTO();
|
||||||
String returnStr = "";
|
|
||||||
if (StringUtils.isNotEmpty(response)) {
|
if (StringUtils.isNotEmpty(response)) {
|
||||||
try {
|
try {
|
||||||
JSONObject respObj = JSONObject.parseObject(response);
|
JSONObject respObj = JSONObject.parseObject(response);
|
||||||
if (respObj.containsKey("body")) {
|
if (respObj.containsKey("body")) {
|
||||||
|
String returnStr = "";
|
||||||
JSONObject bodyObj = respObj.getJSONObject("body");
|
JSONObject bodyObj = respObj.getJSONObject("body");
|
||||||
if (bodyObj.containsKey("type")) {
|
if (bodyObj.containsKey("type")) {
|
||||||
String type = bodyObj.getString("type");
|
String type = bodyObj.getString("type");
|
||||||
|
@ -289,16 +290,17 @@ public class MockApiUtils {
|
||||||
returnStr = JSONObject.toJSONString(paramMap);
|
returnStr = JSONObject.toJSONString(paramMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
responseDTO.setReturnData(returnStr);
|
||||||
}
|
}
|
||||||
if (respObj.containsKey("statusCode")) {
|
if (respObj.containsKey("statusCode")) {
|
||||||
JSONArray statusCodeArray = respObj.getJSONArray("statusCode");
|
JSONArray statusCodeArray = respObj.getJSONArray("statusCode");
|
||||||
|
int code = 200;
|
||||||
if (statusCodeArray != null) {
|
if (statusCodeArray != null) {
|
||||||
for (int i = 0; i < statusCodeArray.size(); i++) {
|
for (int i = 0; i < statusCodeArray.size(); i++) {
|
||||||
JSONObject object = statusCodeArray.getJSONObject(i);
|
JSONObject object = statusCodeArray.getJSONObject(i);
|
||||||
if (object.containsKey("name")) {
|
if (object.containsKey("name")) {
|
||||||
try {
|
try {
|
||||||
int code = Integer.parseInt(object.getString("name"));
|
code = Integer.parseInt(object.getString("name"));
|
||||||
returnMap.put("code", code + "");
|
|
||||||
break;
|
break;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(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) {
|
} catch (Exception e) {
|
||||||
MSException.throwException(e);
|
MSException.throwException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
returnMap.put("returnMsg", returnStr);
|
return responseDTO;
|
||||||
return returnMap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getResultByResponseResult(JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {
|
public String getResultByResponseResult(JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams, boolean useScript) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import io.metersphere.api.dto.automation.EsbDataStruct;
|
||||||
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
|
import io.metersphere.api.dto.automation.TcpTreeTableDataStruct;
|
||||||
import io.metersphere.api.dto.automation.parse.TcpTreeTableDataParser;
|
import io.metersphere.api.dto.automation.parse.TcpTreeTableDataParser;
|
||||||
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
|
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.MockConfigRequestParams;
|
||||||
import io.metersphere.api.dto.mock.MockParamSuggestions;
|
import io.metersphere.api.dto.mock.MockParamSuggestions;
|
||||||
import io.metersphere.api.dto.mock.RequestMockParams;
|
import io.metersphere.api.dto.mock.RequestMockParams;
|
||||||
|
@ -726,10 +727,10 @@ public class MockConfigService {
|
||||||
if (responseJsonObj.containsKey("body")) {
|
if (responseJsonObj.containsKey("body")) {
|
||||||
MockApiUtils mockApiUtils = new MockApiUtils();
|
MockApiUtils mockApiUtils = new MockApiUtils();
|
||||||
boolean useScript = false;
|
boolean useScript = false;
|
||||||
if(responseJsonObj.containsKey("usePostScript")){
|
if (responseJsonObj.containsKey("usePostScript")) {
|
||||||
useScript = responseJsonObj.getBoolean("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")) {
|
if (responseJsonObj.containsKey("httpCode")) {
|
||||||
int httpCodeNum = 500;
|
int httpCodeNum = 500;
|
||||||
|
@ -1131,19 +1132,10 @@ public class MockConfigService {
|
||||||
returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response);
|
returnStr = this.updateHttpServletResponse(finalExpectConfig, url, requestHeaderMap, mockParams, response);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(!isMatch){
|
if (!isMatch) {
|
||||||
Map<String,String> apiResponseMap = MockApiUtils.getApiResponse(api.getResponse());
|
returnStr = this.getApiDefinitionResponse(api, response);
|
||||||
if(MapUtils.isNotEmpty(apiResponseMap)){
|
if (StringUtils.isNotEmpty(returnStr)) {
|
||||||
returnStr = apiResponseMap.get("returnMsg");
|
isMatch = true;
|
||||||
if(StringUtils.isNotEmpty(returnStr)){
|
|
||||||
isMatch = true;
|
|
||||||
int code = 200;
|
|
||||||
if(apiResponseMap.containsKey("code")){
|
|
||||||
code = Integer.parseInt(apiResponseMap.get("code"));
|
|
||||||
}
|
|
||||||
response.setStatus(code);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1188,19 +1180,10 @@ public class MockConfigService {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!isMatch){
|
if (!isMatch) {
|
||||||
Map<String,String> apiResponseMap = MockApiUtils.getApiResponse(api.getResponse());
|
returnStr = this.getApiDefinitionResponse(api, response);
|
||||||
if(MapUtils.isNotEmpty(apiResponseMap)){
|
if (StringUtils.isNotEmpty(returnStr)) {
|
||||||
returnStr = apiResponseMap.get("returnMsg");
|
isMatch = true;
|
||||||
if(StringUtils.isNotEmpty(returnStr)){
|
|
||||||
isMatch = true;
|
|
||||||
int code = 200;
|
|
||||||
if(apiResponseMap.containsKey("code")){
|
|
||||||
code = Integer.parseInt(apiResponseMap.get("code"));
|
|
||||||
}
|
|
||||||
response.setStatus(code);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1213,6 +1196,24 @@ public class MockConfigService {
|
||||||
return returnStr;
|
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) {
|
private List<String> parseByJsonDataStruct(String dataString) {
|
||||||
List<String> returnList = new ArrayList<>();
|
List<String> returnList = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
@ -1294,7 +1295,7 @@ public class MockConfigService {
|
||||||
JSONObject sourceObj = XMLUtils.XmlToJson(message);
|
JSONObject sourceObj = XMLUtils.XmlToJson(message);
|
||||||
try {
|
try {
|
||||||
List<TcpTreeTableDataStruct> tcpDataList = JSONArray.parseArray(requestJson.getString("xmlDataStruct"), TcpTreeTableDataStruct.class);
|
List<TcpTreeTableDataStruct> tcpDataList = JSONArray.parseArray(requestJson.getString("xmlDataStruct"), TcpTreeTableDataStruct.class);
|
||||||
isMatch = TcpTreeTableDataParser.isMatchTreeTableData(sourceObj,tcpDataList);
|
isMatch = TcpTreeTableDataParser.isMatchTreeTableData(sourceObj, tcpDataList);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
|
@ -1474,8 +1475,8 @@ public class MockConfigService {
|
||||||
if (apiDefinitionWithBLOBs == null) {
|
if (apiDefinitionWithBLOBs == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Map<String, String> returnMap = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
|
ApiDefinitionResponseDTO responseDTO = MockApiUtils.getApiResponse(apiDefinitionWithBLOBs.getResponse());
|
||||||
if (MapUtils.isEmpty(returnMap) || !returnMap.containsKey("returnMsg")) {
|
if (StringUtils.isEmpty(responseDTO.getReturnData())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<MockExpectConfigWithBLOBs> updateList = this.selectMockExpectConfigByApiId(apiDefinitionWithBLOBs.getId());
|
List<MockExpectConfigWithBLOBs> updateList = this.selectMockExpectConfigByApiId(apiDefinitionWithBLOBs.getId());
|
||||||
|
@ -1487,7 +1488,7 @@ public class MockConfigService {
|
||||||
if (responseObj.containsKey("responseResult")) {
|
if (responseObj.containsKey("responseResult")) {
|
||||||
JSONObject responseResultObject = responseObj.getJSONObject("responseResult");
|
JSONObject responseResultObject = responseObj.getJSONObject("responseResult");
|
||||||
if (responseResultObject.containsKey("body")) {
|
if (responseResultObject.containsKey("body")) {
|
||||||
responseResultObject.getJSONObject("body").put("apiRspRaw", returnMap.get("returnMsg"));
|
responseResultObject.getJSONObject("body").put("apiRspRaw", responseDTO.getReturnData());
|
||||||
|
|
||||||
model.setResponse(responseObj.toJSONString());
|
model.setResponse(responseObj.toJSONString());
|
||||||
mockExpectConfigMapper.updateByPrimaryKeySelective(model);
|
mockExpectConfigMapper.updateByPrimaryKeySelective(model);
|
||||||
|
|
|
@ -258,7 +258,7 @@ export default {
|
||||||
this.$get(selectUrl, response => {
|
this.$get(selectUrl, response => {
|
||||||
let apiResponse = response.data;
|
let apiResponse = response.data;
|
||||||
if (apiResponse && apiResponse.returnMsg) {
|
if (apiResponse && apiResponse.returnMsg) {
|
||||||
this.body.apiRspRaw = apiResponse.returnMsg;
|
this.body.apiRspRaw = apiResponse.returnData;
|
||||||
}
|
}
|
||||||
this.refreshMsCodeEdit();
|
this.refreshMsCodeEdit();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue