refactor(接口测试): mock执行去掉mock编号

This commit is contained in:
wxg0103 2024-05-14 11:42:19 +08:00 committed by 刘瑞斌
parent be16db3689
commit 2978c5fffd
5 changed files with 24 additions and 41 deletions

View File

@ -11,7 +11,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = {"/mock-server/{projectNum}/{apiNum}/{mockNum}/**", "/mock-server/{projectNum}/{apiNum}/**"})
@RequestMapping(value = "/mock-server/{projectNum}/{apiNum}/**")
@Tag(name = "接口测试-接口管理-接口定义-Mock")
@MultipartConfig
public class MockServerController {
@ -21,54 +21,54 @@ public class MockServerController {
@RequestMapping(method = RequestMethod.OPTIONS)
@NoResultHolder
public ResponseEntity<?> optionsRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.OPTIONS.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> optionsRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.OPTIONS.name(), projectNum, apiNum, request);
}
@RequestMapping(method = RequestMethod.HEAD)
@NoResultHolder
public ResponseEntity<?> headerRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.HEAD.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> headerRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.HEAD.name(), projectNum, apiNum, request);
}
@RequestMapping(method = RequestMethod.TRACE)
@NoResultHolder
public ResponseEntity<?> traceRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.TRACE.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> traceRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return mockServerService.execute(HttpMethod.TRACE.name(), projectNum, apiNum, request);
}
@GetMapping
@NoResultHolder
public ResponseEntity<?> getMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable(required = false) String mockNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.GET.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> getMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.GET.name(), projectNum, apiNum, request);
}
@PostMapping
@NoResultHolder
public ResponseEntity<?> postMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.POST.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> postMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.POST.name(), projectNum, apiNum, request);
}
@PutMapping
@NoResultHolder
public ResponseEntity<?> putMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.PUT.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> putMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.PUT.name(), projectNum, apiNum, request);
}
@DeleteMapping
@NoResultHolder
public ResponseEntity<?> deleteMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.DELETE.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> deleteMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.DELETE.name(), projectNum, apiNum, request);
}
@PatchMapping
@NoResultHolder
public ResponseEntity<?> patchMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, @PathVariable String mockNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.PATCH.name(), projectNum, apiNum, mockNum, request);
public ResponseEntity<?> patchMockRequest(@PathVariable String projectNum, @PathVariable String apiNum, HttpServletRequest request) {
return handleMockRequest(HttpMethod.PATCH.name(), projectNum, apiNum, request);
}
private ResponseEntity<?> handleMockRequest(String method, String projectNum, String apiNum, String mockNum, HttpServletRequest request) {
return mockServerService.execute(method, projectNum, apiNum, mockNum, request);
private ResponseEntity<?> handleMockRequest(String method, String projectNum, String apiNum, HttpServletRequest request) {
return mockServerService.execute(method, projectNum, apiNum, request);
}
}

View File

@ -380,7 +380,7 @@ public class MsHTTPElementConverter extends AbstractJmeterElementConverter<MsHTT
if (match) {
// 如果是mock 返回的url格式是 /mock-server/projectNum/apiNum
if (BooleanUtils.isTrue(envConfig.getMock())) {
httpConfig.setHostname(StringUtils.join(httpConfig.getHostname(), "/", msHTTPElement.getNum(), StringUtils.isNotBlank(msHTTPElement.getMockNum()) ? "/" + msHTTPElement.getMockNum() : StringUtils.EMPTY));
httpConfig.setHostname(StringUtils.join(httpConfig.getHostname(), "/", msHTTPElement.getNum()));
}
return httpConfig;
}

View File

@ -362,7 +362,7 @@ public class ApiDefinitionMockService {
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
if (CollectionUtils.isNotEmpty(environments)) {
EnvironmentInfoDTO environmentInfoDTO = environmentService.get(environments.getFirst().getId());
return StringUtils.join(environmentInfoDTO.getConfig().getHttpConfig().getFirst().getUrl(), "/", apiDefinition.getNum(), "/", apiDefinitionMock.getExpectNum(), apiDefinition.getPath());
return StringUtils.join(environmentInfoDTO.getConfig().getHttpConfig().getFirst().getUrl(), "/", apiDefinition.getNum(), apiDefinition.getPath());
}
return null;

View File

@ -62,10 +62,9 @@ public class MockServerService {
}
public ResponseEntity<?> execute(String method, String projectNum, String apiNum, String mockNum, HttpServletRequest request) {
public ResponseEntity<?> execute(String method, String projectNum, String apiNum, HttpServletRequest request) {
var requestHeaderMap = MockServerUtils.getHttpRequestHeader(request);
String url = request.getRequestURL().toString();
//判断根据mockNum是否可以找到对应的mock配置 如果找不到需要拿接口下所有的mock配置进行匹配
String requestUrlSuffix = MockServerUtils.getUrlSuffix(StringUtils.joinWith("/", "/mock-server", projectNum, apiNum), request);
// Try to find API definition based on projectNum and apiNum
@ -81,13 +80,6 @@ public class MockServerService {
return requestNotFound();
}
if (StringUtils.isNotBlank(mockNum) && mockNum.startsWith(apiNum)) {
requestUrlSuffix = MockServerUtils.getUrlSuffix(StringUtils.joinWith("/", "/mock-server", projectNum, apiNum, mockNum), request);
} else {
mockNum = null;
}
// Check if method and path match the API definition
if (!StringUtils.equalsIgnoreCase(method, apiDefinition.getMethod()) || !MockServerUtils.checkUrlMatch(apiDefinition.getPath(), requestUrlSuffix)) {
return requestNotFound();
@ -97,7 +89,7 @@ public class MockServerService {
HttpRequestParam requestMockParams = MockServerUtils.getHttpRequestParam(request, requestUrlSuffix, apiDefinition.getPath(), !isUrlParamMethod(method));
LogUtils.info("Mock [" + url + "] Header:{}", requestHeaderMap);
LogUtils.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams));
ApiMockConfigDTO compareMockConfig = findMatchingMockConfig(apiDefinition.getId(), requestHeaderMap, requestMockParams, mockNum);
ApiMockConfigDTO compareMockConfig = findMatchingMockConfig(apiDefinition.getId(), requestHeaderMap, requestMockParams);
if (compareMockConfig != null && !compareMockConfig.isEnable()) {
return requestNotFound();
@ -119,13 +111,10 @@ public class MockServerService {
.orElse(null);
}
private ApiMockConfigDTO findMatchingMockConfig(String apiId, Map<String, String> requestHeaderMap, HttpRequestParam param, String mockNum) {
private ApiMockConfigDTO findMatchingMockConfig(String apiId, Map<String, String> requestHeaderMap, HttpRequestParam param) {
// 查询符合条件的 ApiDefinitionMockConfig 列表
ApiDefinitionMockExample mockExample = new ApiDefinitionMockExample();
mockExample.createCriteria().andApiDefinitionIdEqualTo(apiId);
if (StringUtils.isNotBlank(mockNum)) {
mockExample.getOredCriteria().forEach(criteria -> criteria.andExpectNumEqualTo(mockNum));
}
List<ApiDefinitionMock> apiDefinitionMockList = apiDefinitionMockMapper.selectByExample(mockExample);
if (CollectionUtils.isEmpty(apiDefinitionMockList)) {

View File

@ -1250,12 +1250,6 @@ public class ApiDefinitionMockControllerTests extends BaseTest {
MockHttpServletRequestBuilder requestBuilder = mockServerTestService.getRequestBuilder("GET", url);
ResultActions action = mockMvc.perform(requestBuilder);
MockHttpServletResponse mockServerResponse = action.andReturn().getResponse();
url = "/" + mockServerDomain + "/100001/" + apiDefinition.getNum() + "/" + mockData.getExpectNum() + apiDefinition.getPath();
//开始创建请求
requestBuilder = mockServerTestService.getRequestBuilder("GET", url);
action = mockMvc.perform(requestBuilder);
mockServerResponse = action.andReturn().getResponse();
//判断响应
mockServerResponse.getContentAsString(StandardCharsets.UTF_8);
mockData.setEnable(false);