From 68a79b030d2ad846ee3879cdcf56ca3b0d8a171e Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 13 May 2024 12:10:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20mock=E5=A2=9E=E5=8A=A0=E5=93=8D=E5=BA=94=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/api/dto/mockserver/MockResponse.java | 3 +++ .../api/service/mockserver/MockServerService.java | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockResponse.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockResponse.java index e5f8895889..558064ae4f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockResponse.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/mockserver/MockResponse.java @@ -27,4 +27,7 @@ public class MockResponse implements Serializable { @Schema(description = "响应请求体") private ResponseBody body; + + @Schema(description = "响应延迟时间(毫秒)") + private Long delay; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java index e334b5bf10..1b7582d65b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/mockserver/MockServerService.java @@ -187,6 +187,7 @@ public class MockServerService { int responseCode = -1; String useApiResponseId = null; + Long delay = null; if (config != null) { MockResponse mockResponse = JSON.parseObject(new String(config.getResponse()), MockResponse.class); // mock 响应引用的是接口自身响应内容 @@ -196,6 +197,7 @@ public class MockServerService { responseCode = mockResponse.getStatusCode(); responseHeader = mockResponse.getHeaders(); responseBody = mockResponse.getBody(); + delay = mockResponse.getDelay(); } } @@ -232,6 +234,12 @@ public class MockServerService { } if (responseBody != null) { + if (delay != null && delay > 0) { + try { + Thread.sleep(delay); + } catch (Exception ignored) { + } + } boolean isMock = config != null; String resourceId = config != null ? config.getId() : apiId; return switch (responseBody.getBodyType()) {