Search code examples
javaintegration-testingstubwiremock

Wiremock withBodyFile OutOfMemoryError for large files


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!


Solution

  • 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)