refactor(接口测试): mock增加响应延迟时间

This commit is contained in:
wxg0103 2024-05-13 12:10:08 +08:00 committed by Craftsman
parent 5e8034ae51
commit 68a79b030d
2 changed files with 11 additions and 0 deletions

View File

@ -27,4 +27,7 @@ public class MockResponse implements Serializable {
@Schema(description = "响应请求体") @Schema(description = "响应请求体")
private ResponseBody body; private ResponseBody body;
@Schema(description = "响应延迟时间(毫秒)")
private Long delay;
} }

View File

@ -187,6 +187,7 @@ public class MockServerService {
int responseCode = -1; int responseCode = -1;
String useApiResponseId = null; String useApiResponseId = null;
Long delay = null;
if (config != null) { if (config != null) {
MockResponse mockResponse = JSON.parseObject(new String(config.getResponse()), MockResponse.class); MockResponse mockResponse = JSON.parseObject(new String(config.getResponse()), MockResponse.class);
// mock 响应引用的是接口自身响应内容 // mock 响应引用的是接口自身响应内容
@ -196,6 +197,7 @@ public class MockServerService {
responseCode = mockResponse.getStatusCode(); responseCode = mockResponse.getStatusCode();
responseHeader = mockResponse.getHeaders(); responseHeader = mockResponse.getHeaders();
responseBody = mockResponse.getBody(); responseBody = mockResponse.getBody();
delay = mockResponse.getDelay();
} }
} }
@ -232,6 +234,12 @@ public class MockServerService {
} }
if (responseBody != null) { if (responseBody != null) {
if (delay != null && delay > 0) {
try {
Thread.sleep(delay);
} catch (Exception ignored) {
}
}
boolean isMock = config != null; boolean isMock = config != null;
String resourceId = config != null ? config.getId() : apiId; String resourceId = config != null ? config.getId() : apiId;
return switch (responseBody.getBodyType()) { return switch (responseBody.getBodyType()) {