带有bodyfile的wiremock offmoryError用于大文件

发布于 2025-02-07 11:39:38 字数 1570 浏览 2 评论 0原文

在以下存根请求中,我将与bodyfile一起使用加载大文件(> 300mb)从本地磁盘上的特定位置使用流:

public void mockGetUrlContent(String url) {
    stubFor(get(urlEqualTo(url))
            .willReturn(ok()
                    .withBodyFile(FilenameUtils.getName(url))));
}

一旦调用了存根,wiremock tries tries要完成服务事件并为了记录响应,它使用 方法下面:

  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的wempons> Response> Response> 方法它将流中的流转换为byte [],在此点上,它会在OUTOFMEMORYERROR(Java Heap Space)上吹来

  public byte[] getBody() {
    try (InputStream stream = bodyStreamSource == null ? null : getBodyStream()) {
      return stream == null ? null : ByteStreams.toByteArray(stream);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

。错误的?

高度赞赏您的帮助!

In the following stub request, I use withBodyFile which loads large files (>300MB) from specific locations on local disk using streams:

public void mockGetUrlContent(String url) {
    stubFor(get(urlEqualTo(url))
            .willReturn(ok()
                    .withBodyFile(FilenameUtils.getName(url))));
}

Once the stub is being called, wiremock tries to complete a served event and in order to log the response it uses the from method below:

  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());
  }

When reached to wiremock's Response getBody method it converts the stream in to byte[] and in that points it blows on OutOfMemoryError (Java heap space).:

  public byte[] getBody() {
    try (InputStream stream = bodyStreamSource == null ? null : getBodyStream()) {
      return stream == null ? null : ByteStreams.toByteArray(stream);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

Any ideas about what am I doing wrong?

Your help is highly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2025-02-14 11:39:38

WireMock 2.34.0有解决方案。

为了确保已记录的响应主体仅限于特定尺寸,您需要使用新的配置参数启动WireMock,

在运行独立时,指定字节中的最大大小添加CLI参数:

--logged-response-body-size-limit=1000000

在Java中运行时,将以下内容添加到配置构建器:

wireMockConfig().maxLoggedResponseSize(1000000)

WireMock 2.34.0 has a solution to this.

To ensure that logged response bodies are limited to a particular size you need to start WireMock with a new configuration parameter specifying the max size in bytes

When running standalone add the CLI parameter:

--logged-response-body-size-limit=1000000

When running in Java add the following to the configuration builder:

wireMockConfig().maxLoggedResponseSize(1000000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文