fix: mock运行缺陷、期望匹配缺陷修复

mock运行缺陷、期望匹配缺陷修复
This commit is contained in:
song-tianyang 2021-04-28 16:30:49 +08:00 committed by 刘瑞斌
parent cab29062dc
commit 725c30246d
11 changed files with 52 additions and 29 deletions

View File

@ -276,14 +276,14 @@ public class ApiDefinitionController {
esbImportService.templateExport(response);
}
@GetMapping("/getMockEnvironment/{projectId}")
public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId, HttpServletRequest request) {
@GetMapping("/getMockEnvironment/{projectId}/{protocal}")
public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId, @PathVariable String protocal, HttpServletRequest request) {
String requestUrl = request.getRequestURL().toString();
String baseUrl = "";
if (requestUrl.contains("/api/definition")) {
baseUrl = requestUrl.split("/api/definition")[0];
}
return apiTestEnvironmentService.getMockEnvironmentByProjectId(projectId, baseUrl);
return apiTestEnvironmentService.getMockEnvironmentByProjectId(projectId, protocal, baseUrl);
}
}

View File

@ -74,7 +74,6 @@ public class MsScenario extends MsTestElement {
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
boolean isMockEvn = false;
// 非导出操作且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) {
return;
@ -140,6 +139,14 @@ public class MsScenario extends MsTestElement {
});
config.setConfig(envConfig);
}
} else {
Map<String, EnvironmentConfig> map = config.getConfig();
for (EnvironmentConfig evnConfig :
map.values()) {
if (evnConfig.getHttpConfig() != null) {
this.setMockEnvironment(evnConfig.getHttpConfig().isMock());
}
}
}
if (CollectionUtils.isNotEmpty(this.getVariables())) {
config.setVariables(this.variables);

View File

@ -232,7 +232,12 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (this.isMockEnvironment()) {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
} else {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
if (httpConfig.isMock()) {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
} else {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket();
}
}
URL urlObject = new URL(url);
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();

View File

@ -20,6 +20,7 @@ public class HttpConfig {
public HttpConfig initHttpConfig(HttpConfigCondition configCondition) {
HttpConfig config = new HttpConfig();
config.isMock = this.isMock;
BeanUtils.copyBean(config, configCondition);
return config;
}

View File

@ -997,7 +997,15 @@ public class ApiDefinitionService {
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
List<String> apiIdList = new ArrayList<>();
boolean urlSuffixEndEmpty = false;
if (urlSuffix.endsWith("/")) {
urlSuffixEndEmpty = true;
urlSuffix = urlSuffix + "testMock";
}
String[] urlParams = urlSuffix.split("/");
if (urlSuffixEndEmpty) {
urlParams[urlParams.length - 1] = "";
}
for (ApiDefinition api : apiList) {
String path = api.getPath();
if (path.startsWith("/")) {
@ -1007,7 +1015,7 @@ public class ApiDefinitionService {
String[] pathArr = path.split("/");
if (pathArr.length == urlParams.length) {
boolean isFetch = true;
for (int i = 0; i < pathArr.length; i++) {
for (int i = 0; i < urlParams.length; i++) {
String pathItem = pathArr[i];
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
if (!StringUtils.equals(pathArr[i], urlParams[i])) {

View File

@ -77,24 +77,24 @@ public class ApiTestEnvironmentService {
* @param projectId
* @return
*/
public synchronized ApiTestEnvironmentWithBLOBs getMockEnvironmentByProjectId(String projectId, String baseUrl) {
public synchronized ApiTestEnvironmentWithBLOBs getMockEnvironmentByProjectId(String projectId, String protocal, String baseUrl) {
String apiName = MockConfigStaticData.MOCK_EVN_NAME;
ApiTestEnvironmentWithBLOBs returnModel = null;
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(apiName);
List<ApiTestEnvironmentWithBLOBs> list = this.selectByExampleWithBLOBs(example);
if (list.isEmpty()) {
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, apiName, baseUrl);
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, apiName, baseUrl);
this.add(returnModel);
} else {
returnModel = list.get(0);
returnModel = this.checkMockEvnIsRightful(returnModel, projectId, apiName, baseUrl);
returnModel = this.checkMockEvnIsRightful(returnModel, protocal, projectId, apiName, baseUrl);
}
return returnModel;
}
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String projectId, String name, String url) {
boolean needUpdate = true;
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId, String name, String url) {
boolean needUpdate = false;
if (returnModel.getConfig() != null) {
try {
JSONObject configObj = JSONObject.parseObject(returnModel.getConfig());
@ -115,27 +115,24 @@ public class ApiTestEnvironmentService {
}
if (needUpdate) {
String id = returnModel.getId();
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, name, url);
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, name, url);
returnModel.setId(id);
apiTestEnvironmentMapper.updateByPrimaryKeyWithBLOBs(returnModel);
}
return returnModel;
}
private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId, String name, String url) {
String protocol = "";
private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId, String protocal, String name, String url) {
String socket = "";
if (url.startsWith("http://")) {
protocol = "http";
url = url.substring(7);
} else if (url.startsWith("https://")) {
protocol = "https";
url = url.substring(8);
}
socket = url;
String portStr = "";
String ipStr = protocol;
String ipStr = url;
if (url.contains(":") && !url.endsWith(":")) {
String[] urlArr = url.split(":");
int port = -1;
@ -159,13 +156,6 @@ public class ApiTestEnvironmentService {
commonConfigObj.put("hosts", new String[]{});
JSONObject httpConfig = new JSONObject();
// httpConfig.put("socket", url);
// httpConfig.put("domain", ipStr);
// httpConfig.put("headers", variablesArr);
// httpConfig.put("protocol", protocol);
// if (StringUtils.isNotEmpty(portStr)) {
// httpConfig.put("port", portStr);
// }
httpConfig.put("socket", null);
httpConfig.put("isMock", true);
httpConfig.put("domain", null);
@ -181,7 +171,7 @@ public class ApiTestEnvironmentService {
httpItem.put("id", UUID.randomUUID().toString());
httpItem.put("type", "NONE");
httpItem.put("socket", socket);
httpItem.put("protocol", protocol);
httpItem.put("protocol", protocal);
JSONArray protocolVariablesArr = new JSONArray();
Map<String, Object> protocolMap = new HashMap<>();
protocolMap.put("enable", true);

View File

@ -299,7 +299,12 @@ public class MockConfigService {
for (int i = 0; i < statusCodeArr.size(); i++) {
JSONObject obj = statusCodeArr.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) {
response.setHeader(obj.getString("name"), obj.getString("value"));
// response.setHeader(obj.getString("name"), obj.getString("value"));
try {
int headInt = Integer.parseInt(obj.getString("name"));
response.setStatus(headInt);
} catch (Exception e) {
}
}
}
}

View File

@ -32,4 +32,6 @@ public class Project implements Serializable {
private Boolean customNum;
private static final long serialVersionUID = 1L;
private String protocal;
}

View File

@ -75,7 +75,7 @@ public class ProjectController {
if (requestUrl.contains("/project/add")) {
baseUrl = requestUrl.split("/project/add")[0];
}
apiTestEnvironmentService.getMockEnvironmentByProjectId(returnModel.getId(), baseUrl);
apiTestEnvironmentService.getMockEnvironmentByProjectId(returnModel.getId(), project.getProtocal(), baseUrl);
return returnModel;
}

View File

@ -294,8 +294,10 @@
this.httpForm.modulePath = data.path;
},
initMockEnvironment() {
var protocol = document.location.protocol;
protocol = protocol.substring(0, protocol.indexOf(":"));
let url = "/api/definition/getMockEnvironment/";
this.$get(url + this.projectId, response => {
this.$get(url + this.projectId + "/" + protocol, response => {
this.mockEnvironment = response.data;
let httpConfig = JSON.parse(this.mockEnvironment.config);
if (httpConfig != null) {

View File

@ -241,8 +241,11 @@ export default {
if (valid) {
let saveType = "add";
if (this.form.id) {
saveType = "update"
saveType = "update";
}
var protocol = document.location.protocol;
protocol = protocol.substring(0, protocol.indexOf(":"));
this.form.protocal = protocol;
this.result = this.$post("/project/" + saveType, this.form, () => {
this.createVisible = false;
this.list();