作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在以下 stub 请求中,我使用 withBodyFile
使用流从本地磁盘上的特定位置加载大文件(>300MB
):
public void mockGetUrlContent(String url) {
stubFor(get(urlEqualTo(url))
.willReturn(ok()
.withBodyFile(FilenameUtils.getName(url))));
}
调用 stub 后,wiremock 会尝试完成服务事件,并且为了记录响应,它使用 from
method下面:
public static LoggedResponse from(Response response) {
return new LoggedResponse(
response.getStatus(),
response.getHeaders() == null || response.getHeaders().all().isEmpty()
? null
: response.getHeaders(),
response.getBody(),
response.getFault());
}
当到达 wiremock 的 Response
getBody
method它将流转换为 byte[]
并在那个点上发生 OutOfMemoryError
(Java 堆空间)。:
public byte[] getBody() {
try (InputStream stream = bodyStreamSource == null ? null : getBodyStream()) {
return stream == null ? null : ByteStreams.toByteArray(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
关于我做错了什么有什么想法吗?
非常感谢您的帮助!
最佳答案
WireMock 2.34.0 对此有解决方案。
为确保将记录的响应主体限制为特定大小,您需要使用新配置参数启动 WireMock,以字节为单位指定最大大小
独立运行时添加 CLI 参数:
--logged-response-body-size-limit=1000000
在 Java 中运行时,将以下内容添加到配置构建器中:
wireMockConfig().maxLoggedResponseSize(1000000)
关于java - 大文件的 Wiremock withBodyFile OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72628047/
在以下 stub 请求中,我使用 withBodyFile 使用流从本地磁盘上的特定位置加载大文件(>300MB): public void mockGetUrlContent(String url)
Wiremock 文档指出,withBodyFile 中指定的文件位置应位于 src/test/resources/__files 中。我想在 src/test/resources/Testing_A
我是一名优秀的程序员,十分优秀!