feat(Mock): #1007385 #1007387 #1007366 测试mock期望结果未生效、响应体是自定义脚本,test执行结果返回格式错误

--bug=1007385
--user=宋天阳
[接口定义]-编辑get接口-添加MOCK期望-修改rest参数-执行test测试mock期望结果未生效
https://www.tapd.cn/55049933/s/1058299;--bug=1007387
--user=宋天阳
[接口定义]-编辑接口-添加mock期望-响应体是自定义脚本,test执行结果返回格式错误
https://www.tapd.cn/55049933/s/1058224;--bug=1007366
--user=宋天阳 【接口定义】-PATCH请求使用MOCK环境测试期望结果失败
https://www.tapd.cn/55049933/s/1058406
This commit is contained in:
song-tianyang 2021-10-25 14:09:56 +08:00 committed by song-tianyang
parent 1b061f13f0
commit 69b18e97c9
7 changed files with 60 additions and 61 deletions

View File

@ -123,7 +123,6 @@ public class EsbDataStruct {
element.addAttribute("attr", attrString); element.addAttribute("attr", attrString);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(this.name);
e.printStackTrace(); e.printStackTrace();
} }
@ -163,7 +162,6 @@ public class EsbDataStruct {
element.addAttribute("attr", attrString); element.addAttribute("attr", attrString);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(this.name);
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -123,7 +123,6 @@ public class TcpTreeTableDataStruct {
element.addAttribute("attr", attrString); element.addAttribute("attr", attrString);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(this.name);
e.printStackTrace(); e.printStackTrace();
} }
@ -163,7 +162,6 @@ public class TcpTreeTableDataStruct {
element.addAttribute("attr", attrString); element.addAttribute("attr", attrString);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(this.name);
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -424,6 +424,9 @@ public class MockApiUtils {
if(StringUtils.isNotEmpty(newScript)){ if(StringUtils.isNotEmpty(newScript)){
newScript += "\n"; newScript += "\n";
} }
if(StringUtils.isNotEmpty(returnMsg)){
returnMsg += "\n";
}
} }
} }
returnMap.put("script",newScript); returnMap.put("script",newScript);
@ -435,8 +438,8 @@ public class MockApiUtils {
JSR223Sampler jmeterScriptSampler = new JSR223Sampler(); JSR223Sampler jmeterScriptSampler = new JSR223Sampler();
jmeterScriptSampler.setScriptLanguage(scriptLanguage); jmeterScriptSampler.setScriptLanguage(scriptLanguage);
jmeterScriptSampler.setScript(script); jmeterScriptSampler.setScript(script);
SampleResult result = jmeterScriptSampler.sample(null); jmeterScriptSampler.sample(null);
System.out.println(result.getResponseData());
} }
public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject,JSON paramJson){ public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject,JSON paramJson){
@ -492,7 +495,6 @@ public class MockApiUtils {
return returnJson; return returnJson;
} else if (StringUtils.equalsIgnoreCase("text/xml", request.getContentType())) { } else if (StringUtils.equalsIgnoreCase("text/xml", request.getContentType())) {
String xmlString = readXml(request); String xmlString = readXml(request);
System.out.println(xmlString);
org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString); org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString);
String jsonStr = xmlJSONObj.toString(); String jsonStr = xmlJSONObj.toString();
@ -511,6 +513,14 @@ public class MockApiUtils {
object.put(key, value); object.put(key, value);
} }
return object; return object;
} else if (StringUtils.equalsIgnoreCase("text/plain", request.getContentType())) {
JSONObject object = new JSONObject();
String bodyParam = readBody(request);
if(StringUtils.isNotEmpty(bodyParam)){
object.put("raw",bodyParam);
}
return object;
} else { } else {
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
String bodyParam = readBody(request); String bodyParam = readBody(request);

View File

@ -1443,67 +1443,55 @@ public class ApiDefinitionService {
// return apiDefinitionMapper.selectByExample(example); // return apiDefinitionMapper.selectByExample(example);
// } // }
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String url, String urlSuffix) { public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String urlSuffix) {
if (StringUtils.isEmpty(urlSuffix)) { if (StringUtils.isEmpty(urlSuffix)) {
return new ArrayList<>(); return new ArrayList<>();
} else { } else {
if (StringUtils.equalsAnyIgnoreCase(method, "GET", "DELETE")) { ApiDefinitionExample example = new ApiDefinitionExample();
ApiDefinitionExample example = new ApiDefinitionExample(); example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId);
ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId); List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
if (StringUtils.isNotEmpty(url)) {
criteria.andPathEqualTo(url);
}
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
List<String> apiIdList = new ArrayList<>(); List<String> apiIdList = new ArrayList<>();
boolean urlSuffixEndEmpty = false; boolean urlSuffixEndEmpty = false;
if (urlSuffix.endsWith("/")) { if (urlSuffix.endsWith("/")) {
urlSuffixEndEmpty = true; urlSuffixEndEmpty = true;
urlSuffix = urlSuffix + "testMock"; urlSuffix = urlSuffix + "testMock";
}
String[] urlParams = urlSuffix.split("/");
if (urlSuffixEndEmpty) {
urlParams[urlParams.length - 1] = "";
}
for (ApiDefinition api : apiList) {
String path = api.getPath();
if (path.startsWith("/")) {
path = path.substring(1);
} }
String[] urlParams = urlSuffix.split("/"); if (StringUtils.isNotEmpty(path)) {
if (urlSuffixEndEmpty) { String[] pathArr = path.split("/");
urlParams[urlParams.length - 1] = ""; if (pathArr.length == urlParams.length) {
} boolean isFetch = true;
for (ApiDefinition api : apiList) { for (int i = 0; i < urlParams.length; i++) {
String path = api.getPath(); String pathItem = pathArr[i];
if (path.startsWith("/")) { if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
path = path.substring(1); if (!StringUtils.equals(pathArr[i], urlParams[i])) {
} isFetch = false;
if (StringUtils.isNotEmpty(path)) { break;
String[] pathArr = path.split("/");
if (pathArr.length == urlParams.length) {
boolean isFetch = true;
for (int i = 0; i < urlParams.length; i++) {
String pathItem = pathArr[i];
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
if (!StringUtils.equals(pathArr[i], urlParams[i])) {
isFetch = false;
break;
}
} }
}
} }
if (isFetch) { if (isFetch) {
apiIdList.add(api.getId()); apiIdList.add(api.getId());
}
} }
} }
} }
if (apiIdList.isEmpty()) { }
return new ArrayList<>(); if (apiIdList.isEmpty()) {
} else { return new ArrayList<>();
example.clear();
example.createCriteria().andIdIn(apiIdList);
return apiDefinitionMapper.selectByExampleWithBLOBs(example);
}
} else { } else {
if (!url.startsWith("/")) { example.clear();
url = "/" + url; example.createCriteria().andIdIn(apiIdList);
}
ApiDefinitionExample example = new ApiDefinitionExample();
ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId).andPathEqualTo(url);
return apiDefinitionMapper.selectByExampleWithBLOBs(example); return apiDefinitionMapper.selectByExampleWithBLOBs(example);
} }
} }

View File

@ -605,7 +605,6 @@ public class ApiScenarioReportService {
if (obj != null) { if (obj != null) {
ReportCounter counter = (ReportCounter) obj; ReportCounter counter = (ReportCounter) obj;
counter.setNumber(counter.getNumber() + 1); counter.setNumber(counter.getNumber() + 1);
System.out.println("得到统计数量:" + counter.getNumber());
MessageCache.cache.put(report.getScenarioId(), counter); MessageCache.cache.put(report.getScenarioId(), counter);
} }
} }

View File

@ -1032,7 +1032,7 @@ public class MockConfigService {
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>(); List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
if (project != null) { if (project != null) {
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, urlSuffix); aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
JSON paramJson = MockApiUtils.getPostParamMap(request); JSON paramJson = MockApiUtils.getPostParamMap(request);
JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request); JSONObject parameterObject = MockApiUtils.getParameterJsonObject(request);
@ -1094,7 +1094,7 @@ public class MockConfigService {
List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>(); List<ApiDefinitionWithBLOBs> aualifiedApiList = new ArrayList<>();
if (project != null) { if (project != null) {
String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); String urlSuffix = this.getUrlSuffix(project.getSystemId(), request);
aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, null, urlSuffix); aualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix);
/** /**
* GET/DELETE 这种通过url穿参数的接口在接口路径相同的情况下可能会出现这样的情况 * GET/DELETE 这种通过url穿参数的接口在接口路径相同的情况下可能会出现这样的情况

View File

@ -59,9 +59,15 @@ public class JsonStructUtils {
} }
public static boolean checkJsonArrayCompliance(JSONArray sourceArray, JSONArray matchArray) { public static boolean checkJsonArrayCompliance(JSONArray sourceArray, JSONArray matchArray) {
if (sourceArray == null && matchArray == null) { if(sourceArray == null){
sourceArray = new JSONArray();
}
if(matchArray == null){
matchArray = new JSONArray();
}
if (sourceArray.isEmpty() && matchArray.isEmpty()) {
return true; return true;
} else if (sourceArray != null && matchArray != null && sourceArray.size() >= matchArray.size()) { } else if (!sourceArray.isEmpty() && !matchArray.isEmpty() && sourceArray.size() >= matchArray.size()) {
try { try {
for (int i = 0; i < matchArray.size(); i++) { for (int i = 0; i < matchArray.size(); i++) {
Object obj = matchArray.get(i); Object obj = matchArray.get(i);