fix(接口测试): 修复获取mock函数报错问题

This commit is contained in:
fit2-zhao 2024-03-14 19:24:37 +08:00 committed by Craftsman
parent c6e70c0dc9
commit 51c199264c
4 changed files with 34 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package io.metersphere.api.controller; package io.metersphere.api.controller;
import com.fasterxml.jackson.databind.node.TextNode;
import io.metersphere.api.dto.ApiTestPluginOptionRequest; import io.metersphere.api.dto.ApiTestPluginOptionRequest;
import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.ApiExecuteService;
import io.metersphere.api.service.ApiTestService; import io.metersphere.api.service.ApiTestService;
@ -43,10 +44,13 @@ public class ApiTestController {
return apiTestService.getProtocols(organizationId); return apiTestService.getProtocols(organizationId);
} }
@GetMapping("/mock/{key}") @PostMapping("/mock")
@Operation(summary = "获取mock数据") @Operation(summary = "获取mock数据")
public String mock(@PathVariable String key) { public String mock(@RequestBody TextNode key) {
return Mock.calculate(key).toString(); if (key == null || key.asText().isEmpty()) {
return "";
}
return Mock.calculate(key.asText()).toString();
} }
@PostMapping("/custom/func/run") @PostMapping("/custom/func/run")

View File

@ -9,7 +9,9 @@ import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest;
import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.project.dto.environment.EnvironmentConfig;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BasePluginTestService; import io.metersphere.system.base.BasePluginTestService;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.domain.Plugin; import io.metersphere.system.domain.Plugin;
@ -20,8 +22,10 @@ import jakarta.annotation.Resource;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -31,6 +35,8 @@ import java.util.List;
import static io.metersphere.sdk.constants.InternalUserRole.ADMIN; import static io.metersphere.sdk.constants.InternalUserRole.ADMIN;
import static io.metersphere.system.controller.handler.result.MsHttpResultCode.NOT_FOUND; import static io.metersphere.system.controller.handler.result.MsHttpResultCode.NOT_FOUND;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/** /**
* @Author: jianxing * @Author: jianxing
@ -43,7 +49,7 @@ public class ApiTestControllerTests extends BaseTest {
private static final String BASE_PATH = "/api/test/"; private static final String BASE_PATH = "/api/test/";
protected static final String PROTOCOL_LIST = "protocol/{0}"; protected static final String PROTOCOL_LIST = "protocol/{0}";
protected static final String MOCK = "mock/{0}"; protected static final String MOCK = "mock";
protected static final String CUSTOM_FUNC_RUN = "custom/func/run"; protected static final String CUSTOM_FUNC_RUN = "custom/func/run";
protected static final String PLUGIN_FORM_OPTION = "plugin/form/option"; protected static final String PLUGIN_FORM_OPTION = "plugin/form/option";
protected static final String PLUGIN_SCRIPT = "plugin/script/{0}"; protected static final String PLUGIN_SCRIPT = "plugin/script/{0}";
@ -58,6 +64,7 @@ public class ApiTestControllerTests extends BaseTest {
private BasePluginTestService basePluginTestService; private BasePluginTestService basePluginTestService;
@Resource @Resource
private BaseEnvTestService baseEnvTestService; private BaseEnvTestService baseEnvTestService;
@Override @Override
protected String getBasePath() { protected String getBasePath() {
return BASE_PATH; return BASE_PATH;
@ -69,10 +76,22 @@ public class ApiTestControllerTests extends BaseTest {
this.requestGetWithOk(PROTOCOL_LIST, this.DEFAULT_ORGANIZATION_ID).andReturn(); this.requestGetWithOk(PROTOCOL_LIST, this.DEFAULT_ORGANIZATION_ID).andReturn();
} }
private MvcResult responsePost(String url, Object param) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(param))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
}
@Test @Test
public void getMock() throws Exception { public void getMock() throws Exception {
// @@请求成功 // @@请求成功
this.requestGetWithOk(MOCK, "@integer").andReturn(); MvcResult mvcResult = responsePost(BASE_PATH + MOCK, "@integer");
Assertions.assertEquals(200, mvcResult.getResponse().getStatus());
} }
@Test @Test
@ -190,7 +209,7 @@ public class ApiTestControllerTests extends BaseTest {
@Test @Test
public void getEnvList() throws Exception { public void getEnvList() throws Exception {
// @@请求成功 // @@请求成功
this.requestGet(ENV_LIST, DEFAULT_PROJECT_ID); this.requestGet(ENV_LIST, DEFAULT_PROJECT_ID);
} }
@Test @Test

View File

@ -89,8 +89,8 @@ export function deleteDebug(id: string) {
} }
// 测试mock // 测试mock
export function testMock(key: string) { export function testMock(data: string) {
return MSR.get({ url: TestMockUrl, params: key }); return MSR.post({ url: TestMockUrl, data });
} }
// 上传文件 // 上传文件

View File

@ -18,9 +18,9 @@ export default {
'ms.paramsInput.maxIntegerPlaceholder': '输入整数,如 100最大不超过 100', 'ms.paramsInput.maxIntegerPlaceholder': '输入整数,如 100最大不超过 100',
'ms.paramsInput.minIntegerPlaceholder': '输入整数,如 1', 'ms.paramsInput.minIntegerPlaceholder': '输入整数,如 1',
'ms.paramsInput.float': '浮点数', 'ms.paramsInput.float': '浮点数',
'ms.paramsInput.floatDesc': '返回一个随机的浮点数整数1-10小数部分位数的最小值2最大值5', 'ms.paramsInput.floatDesc': '返回一个随机的浮点数整数1-10小数部分位数的最小位数2最大位数5',
'ms.paramsInput.floatMin': '小数部分位数最小', 'ms.paramsInput.floatMin': '小数部分位数最小位数',
'ms.paramsInput.floatMax': '小数部分位数最大', 'ms.paramsInput.floatMax': '小数部分位数最大位数',
'ms.paramsInput.floatIntegerMin': '整数部分最小值', 'ms.paramsInput.floatIntegerMin': '整数部分最小值',
'ms.paramsInput.floatIntegerMax': '整数部分最大值', 'ms.paramsInput.floatIntegerMax': '整数部分最大值',
'ms.paramsInput.commonPlaceholder': '请输入', 'ms.paramsInput.commonPlaceholder': '请输入',