提交 725c3024 编写于 作者: S song-tianyang 提交者: 刘瑞斌

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

mock运行缺陷、期望匹配缺陷修复
上级 cab29062
...@@ -276,14 +276,14 @@ public class ApiDefinitionController { ...@@ -276,14 +276,14 @@ public class ApiDefinitionController {
esbImportService.templateExport(response); esbImportService.templateExport(response);
} }
@GetMapping("/getMockEnvironment/{projectId}") @GetMapping("/getMockEnvironment/{projectId}/{protocal}")
public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId, HttpServletRequest request) { public ApiTestEnvironmentWithBLOBs getMockEnvironment(@PathVariable String projectId, @PathVariable String protocal, HttpServletRequest request) {
String requestUrl = request.getRequestURL().toString(); String requestUrl = request.getRequestURL().toString();
String baseUrl = ""; String baseUrl = "";
if (requestUrl.contains("/api/definition")) { if (requestUrl.contains("/api/definition")) {
baseUrl = requestUrl.split("/api/definition")[0]; baseUrl = requestUrl.split("/api/definition")[0];
} }
return apiTestEnvironmentService.getMockEnvironmentByProjectId(projectId, baseUrl); return apiTestEnvironmentService.getMockEnvironmentByProjectId(projectId, protocal, baseUrl);
} }
} }
...@@ -74,7 +74,6 @@ public class MsScenario extends MsTestElement { ...@@ -74,7 +74,6 @@ public class MsScenario extends MsTestElement {
@Override @Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) { public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
boolean isMockEvn = false;
// 非导出操作,且不是启用状态则跳过执行 // 非导出操作,且不是启用状态则跳过执行
if (!config.isOperating() && !this.isEnable()) { if (!config.isOperating() && !this.isEnable()) {
return; return;
...@@ -140,6 +139,14 @@ public class MsScenario extends MsTestElement { ...@@ -140,6 +139,14 @@ public class MsScenario extends MsTestElement {
}); });
config.setConfig(envConfig); 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())) { if (CollectionUtils.isNotEmpty(this.getVariables())) {
config.setVariables(this.variables); config.setVariables(this.variables);
......
...@@ -232,7 +232,12 @@ public class MsHTTPSamplerProxy extends MsTestElement { ...@@ -232,7 +232,12 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (this.isMockEnvironment()) { if (this.isMockEnvironment()) {
url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId(); url = httpConfig.getProtocol() + "://" + httpConfig.getSocket() + "/mock/" + this.getProjectId();
} else { } 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); URL urlObject = new URL(url);
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath(); String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
......
...@@ -20,6 +20,7 @@ public class HttpConfig { ...@@ -20,6 +20,7 @@ public class HttpConfig {
public HttpConfig initHttpConfig(HttpConfigCondition configCondition) { public HttpConfig initHttpConfig(HttpConfigCondition configCondition) {
HttpConfig config = new HttpConfig(); HttpConfig config = new HttpConfig();
config.isMock = this.isMock;
BeanUtils.copyBean(config, configCondition); BeanUtils.copyBean(config, configCondition);
return config; return config;
} }
......
...@@ -997,7 +997,15 @@ public class ApiDefinitionService { ...@@ -997,7 +997,15 @@ public class ApiDefinitionService {
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example); List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
List<String> apiIdList = new ArrayList<>(); List<String> apiIdList = new ArrayList<>();
boolean urlSuffixEndEmpty = false;
if (urlSuffix.endsWith("/")) {
urlSuffixEndEmpty = true;
urlSuffix = urlSuffix + "testMock";
}
String[] urlParams = urlSuffix.split("/"); String[] urlParams = urlSuffix.split("/");
if (urlSuffixEndEmpty) {
urlParams[urlParams.length - 1] = "";
}
for (ApiDefinition api : apiList) { for (ApiDefinition api : apiList) {
String path = api.getPath(); String path = api.getPath();
if (path.startsWith("/")) { if (path.startsWith("/")) {
...@@ -1007,7 +1015,7 @@ public class ApiDefinitionService { ...@@ -1007,7 +1015,7 @@ public class ApiDefinitionService {
String[] pathArr = path.split("/"); String[] pathArr = path.split("/");
if (pathArr.length == urlParams.length) { if (pathArr.length == urlParams.length) {
boolean isFetch = true; boolean isFetch = true;
for (int i = 0; i < pathArr.length; i++) { for (int i = 0; i < urlParams.length; i++) {
String pathItem = pathArr[i]; String pathItem = pathArr[i];
if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) { if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) {
if (!StringUtils.equals(pathArr[i], urlParams[i])) { if (!StringUtils.equals(pathArr[i], urlParams[i])) {
......
...@@ -77,24 +77,24 @@ public class ApiTestEnvironmentService { ...@@ -77,24 +77,24 @@ public class ApiTestEnvironmentService {
* @param projectId * @param projectId
* @return * @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; String apiName = MockConfigStaticData.MOCK_EVN_NAME;
ApiTestEnvironmentWithBLOBs returnModel = null; ApiTestEnvironmentWithBLOBs returnModel = null;
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(apiName); example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(apiName);
List<ApiTestEnvironmentWithBLOBs> list = this.selectByExampleWithBLOBs(example); List<ApiTestEnvironmentWithBLOBs> list = this.selectByExampleWithBLOBs(example);
if (list.isEmpty()) { if (list.isEmpty()) {
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, apiName, baseUrl); returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, apiName, baseUrl);
this.add(returnModel); this.add(returnModel);
} else { } else {
returnModel = list.get(0); returnModel = list.get(0);
returnModel = this.checkMockEvnIsRightful(returnModel, projectId, apiName, baseUrl); returnModel = this.checkMockEvnIsRightful(returnModel, protocal, projectId, apiName, baseUrl);
} }
return returnModel; return returnModel;
} }
private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String projectId, String name, String url) { private ApiTestEnvironmentWithBLOBs checkMockEvnIsRightful(ApiTestEnvironmentWithBLOBs returnModel, String protocal, String projectId, String name, String url) {
boolean needUpdate = true; boolean needUpdate = false;
if (returnModel.getConfig() != null) { if (returnModel.getConfig() != null) {
try { try {
JSONObject configObj = JSONObject.parseObject(returnModel.getConfig()); JSONObject configObj = JSONObject.parseObject(returnModel.getConfig());
...@@ -115,27 +115,24 @@ public class ApiTestEnvironmentService { ...@@ -115,27 +115,24 @@ public class ApiTestEnvironmentService {
} }
if (needUpdate) { if (needUpdate) {
String id = returnModel.getId(); String id = returnModel.getId();
returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, name, url); returnModel = this.genHttpApiTestEnvironmentByUrl(projectId, protocal, name, url);
returnModel.setId(id); returnModel.setId(id);
apiTestEnvironmentMapper.updateByPrimaryKeyWithBLOBs(returnModel); apiTestEnvironmentMapper.updateByPrimaryKeyWithBLOBs(returnModel);
} }
return returnModel; return returnModel;
} }
private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId, String name, String url) { private ApiTestEnvironmentWithBLOBs genHttpApiTestEnvironmentByUrl(String projectId, String protocal, String name, String url) {
String protocol = "";
String socket = ""; String socket = "";
if (url.startsWith("http://")) { if (url.startsWith("http://")) {
protocol = "http";
url = url.substring(7); url = url.substring(7);
} else if (url.startsWith("https://")) { } else if (url.startsWith("https://")) {
protocol = "https";
url = url.substring(8); url = url.substring(8);
} }
socket = url; socket = url;
String portStr = ""; String portStr = "";
String ipStr = protocol; String ipStr = url;
if (url.contains(":") && !url.endsWith(":")) { if (url.contains(":") && !url.endsWith(":")) {
String[] urlArr = url.split(":"); String[] urlArr = url.split(":");
int port = -1; int port = -1;
...@@ -159,13 +156,6 @@ public class ApiTestEnvironmentService { ...@@ -159,13 +156,6 @@ public class ApiTestEnvironmentService {
commonConfigObj.put("hosts", new String[]{}); commonConfigObj.put("hosts", new String[]{});
JSONObject httpConfig = new JSONObject(); 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("socket", null);
httpConfig.put("isMock", true); httpConfig.put("isMock", true);
httpConfig.put("domain", null); httpConfig.put("domain", null);
...@@ -181,7 +171,7 @@ public class ApiTestEnvironmentService { ...@@ -181,7 +171,7 @@ public class ApiTestEnvironmentService {
httpItem.put("id", UUID.randomUUID().toString()); httpItem.put("id", UUID.randomUUID().toString());
httpItem.put("type", "NONE"); httpItem.put("type", "NONE");
httpItem.put("socket", socket); httpItem.put("socket", socket);
httpItem.put("protocol", protocol); httpItem.put("protocol", protocal);
JSONArray protocolVariablesArr = new JSONArray(); JSONArray protocolVariablesArr = new JSONArray();
Map<String, Object> protocolMap = new HashMap<>(); Map<String, Object> protocolMap = new HashMap<>();
protocolMap.put("enable", true); protocolMap.put("enable", true);
......
...@@ -299,7 +299,12 @@ public class MockConfigService { ...@@ -299,7 +299,12 @@ public class MockConfigService {
for (int i = 0; i < statusCodeArr.size(); i++) { for (int i = 0; i < statusCodeArr.size(); i++) {
JSONObject obj = statusCodeArr.getJSONObject(i); JSONObject obj = statusCodeArr.getJSONObject(i);
if (obj.containsKey("name") && obj.containsKey("value") && StringUtils.isNotEmpty(obj.getString("name"))) { 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) {
}
} }
} }
} }
......
...@@ -32,4 +32,6 @@ public class Project implements Serializable { ...@@ -32,4 +32,6 @@ public class Project implements Serializable {
private Boolean customNum; private Boolean customNum;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String protocal;
} }
\ No newline at end of file
...@@ -75,7 +75,7 @@ public class ProjectController { ...@@ -75,7 +75,7 @@ public class ProjectController {
if (requestUrl.contains("/project/add")) { if (requestUrl.contains("/project/add")) {
baseUrl = requestUrl.split("/project/add")[0]; baseUrl = requestUrl.split("/project/add")[0];
} }
apiTestEnvironmentService.getMockEnvironmentByProjectId(returnModel.getId(), baseUrl); apiTestEnvironmentService.getMockEnvironmentByProjectId(returnModel.getId(), project.getProtocal(), baseUrl);
return returnModel; return returnModel;
} }
......
...@@ -294,8 +294,10 @@ ...@@ -294,8 +294,10 @@
this.httpForm.modulePath = data.path; this.httpForm.modulePath = data.path;
}, },
initMockEnvironment() { initMockEnvironment() {
var protocol = document.location.protocol;
protocol = protocol.substring(0, protocol.indexOf(":"));
let url = "/api/definition/getMockEnvironment/"; let url = "/api/definition/getMockEnvironment/";
this.$get(url + this.projectId, response => { this.$get(url + this.projectId + "/" + protocol, response => {
this.mockEnvironment = response.data; this.mockEnvironment = response.data;
let httpConfig = JSON.parse(this.mockEnvironment.config); let httpConfig = JSON.parse(this.mockEnvironment.config);
if (httpConfig != null) { if (httpConfig != null) {
......
...@@ -241,8 +241,11 @@ export default { ...@@ -241,8 +241,11 @@ export default {
if (valid) { if (valid) {
let saveType = "add"; let saveType = "add";
if (this.form.id) { 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.result = this.$post("/project/" + saveType, this.form, () => {
this.createVisible = false; this.createVisible = false;
this.list(); this.list();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册