diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java index 790ad41221..99031b4339 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiTestController.java @@ -1,5 +1,6 @@ package io.metersphere.api.controller; +import com.fasterxml.jackson.databind.node.TextNode; import io.metersphere.api.dto.ApiTestPluginOptionRequest; import io.metersphere.api.service.ApiExecuteService; import io.metersphere.api.service.ApiTestService; @@ -43,10 +44,13 @@ public class ApiTestController { return apiTestService.getProtocols(organizationId); } - @GetMapping("/mock/{key}") + @PostMapping("/mock") @Operation(summary = "获取mock数据") - public String mock(@PathVariable String key) { - return Mock.calculate(key).toString(); + public String mock(@RequestBody TextNode key) { + if (key == null || key.asText().isEmpty()) { + return ""; + } + return Mock.calculate(key.asText()).toString(); } @PostMapping("/custom/func/run") diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java index c843c24f59..524101dd6c 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestControllerTests.java @@ -9,7 +9,9 @@ import io.metersphere.project.constants.ScriptLanguageType; import io.metersphere.project.dto.customfunction.request.CustomFunctionRunRequest; import io.metersphere.project.dto.environment.EnvironmentConfig; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.domain.Environment; +import io.metersphere.sdk.util.JSON; import io.metersphere.system.base.BasePluginTestService; import io.metersphere.system.base.BaseTest; import io.metersphere.system.domain.Plugin; @@ -20,8 +22,10 @@ import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.io.File; import java.io.FileInputStream; @@ -31,6 +35,8 @@ import java.util.List; import static io.metersphere.sdk.constants.InternalUserRole.ADMIN; 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 @@ -43,7 +49,7 @@ public class ApiTestControllerTests extends BaseTest { private static final String BASE_PATH = "/api/test/"; 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 PLUGIN_FORM_OPTION = "plugin/form/option"; protected static final String PLUGIN_SCRIPT = "plugin/script/{0}"; @@ -58,6 +64,7 @@ public class ApiTestControllerTests extends BaseTest { private BasePluginTestService basePluginTestService; @Resource private BaseEnvTestService baseEnvTestService; + @Override protected String getBasePath() { return BASE_PATH; @@ -69,10 +76,22 @@ public class ApiTestControllerTests extends BaseTest { 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 public void getMock() throws Exception { // @@请求成功 - this.requestGetWithOk(MOCK, "@integer").andReturn(); + MvcResult mvcResult = responsePost(BASE_PATH + MOCK, "@integer"); + Assertions.assertEquals(200, mvcResult.getResponse().getStatus()); } @Test @@ -190,7 +209,7 @@ public class ApiTestControllerTests extends BaseTest { @Test public void getEnvList() throws Exception { // @@请求成功 - this.requestGet(ENV_LIST, DEFAULT_PROJECT_ID); + this.requestGet(ENV_LIST, DEFAULT_PROJECT_ID); } @Test diff --git a/frontend/src/api/modules/api-test/debug.ts b/frontend/src/api/modules/api-test/debug.ts index a021cce050..a75d917d73 100644 --- a/frontend/src/api/modules/api-test/debug.ts +++ b/frontend/src/api/modules/api-test/debug.ts @@ -89,8 +89,8 @@ export function deleteDebug(id: string) { } // 测试mock -export function testMock(key: string) { - return MSR.get({ url: TestMockUrl, params: key }); +export function testMock(data: string) { + return MSR.post({ url: TestMockUrl, data }); } // 上传文件 diff --git a/frontend/src/components/business/ms-params-input/locale/zh-CN.ts b/frontend/src/components/business/ms-params-input/locale/zh-CN.ts index a1eb567b91..a92fc23ce8 100644 --- a/frontend/src/components/business/ms-params-input/locale/zh-CN.ts +++ b/frontend/src/components/business/ms-params-input/locale/zh-CN.ts @@ -18,9 +18,9 @@ export default { 'ms.paramsInput.maxIntegerPlaceholder': '输入整数,如 100,最大不超过 100', 'ms.paramsInput.minIntegerPlaceholder': '输入整数,如 1', 'ms.paramsInput.float': '浮点数', - 'ms.paramsInput.floatDesc': '返回一个随机的浮点数,整数1-10,小数部分位数的最小值2,最大值5', - 'ms.paramsInput.floatMin': '小数部分位数最小值', - 'ms.paramsInput.floatMax': '小数部分位数最大值', + 'ms.paramsInput.floatDesc': '返回一个随机的浮点数,整数1-10,小数部分位数的最小位数2,最大位数5', + 'ms.paramsInput.floatMin': '小数部分位数最小位数', + 'ms.paramsInput.floatMax': '小数部分位数最大位数', 'ms.paramsInput.floatIntegerMin': '整数部分最小值', 'ms.paramsInput.floatIntegerMax': '整数部分最大值', 'ms.paramsInput.commonPlaceholder': '请输入',