Skip to content

Commit

Permalink
refactor(接口测试): mock增加响应延迟时间
Browse files Browse the repository at this point in the history
  • Loading branch information
wxg0103 authored and fit2-zhao committed May 13, 2024
1 parent 5e8034a commit 68a79b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public class MockResponse implements Serializable {

@Schema(description = "响应请求体")
private ResponseBody body;

@Schema(description = "响应延迟时间(毫秒)")
private Long delay;
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ private ResponseEntity<?> getResponseBody(ApiDefinitionMockConfig config, String
int responseCode = -1;
String useApiResponseId = null;

Long delay = null;
if (config != null) {
MockResponse mockResponse = JSON.parseObject(new String(config.getResponse()), MockResponse.class);
// mock 响应引用的是接口自身响应内容
Expand All @@ -196,6 +197,7 @@ private ResponseEntity<?> getResponseBody(ApiDefinitionMockConfig config, String
responseCode = mockResponse.getStatusCode();
responseHeader = mockResponse.getHeaders();
responseBody = mockResponse.getBody();
delay = mockResponse.getDelay();
}
}

Expand Down Expand Up @@ -232,6 +234,12 @@ private ResponseEntity<?> getResponseBody(ApiDefinitionMockConfig config, String
}

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()) {
Expand Down

0 comments on commit 68a79b0

Please sign in to comment.