3

I would like to test our app using a HTTP client with a huge amount of data. Is it possible to create an infinite or several gigabytes length output with WireMock without allocating a byte array or String with that size?

As far as I see ResponseDefinitionBuilder has three withBody* methods:

  • public ResponseDefinitionBuilder withBodyFile(String fileName)
  • public ResponseDefinitionBuilder withBody(String body)
  • public ResponseDefinitionBuilder withBody(byte[] body)

I have tried withBodyFile("/dev/zero") but I got the following exception:

WARN (ServletHandler.java:628) - /test.txt
com.github.tomakehurst.wiremock.security.NotAuthorisedException: Access to file /dev/zero is not permitted
    at com.github.tomakehurst.wiremock.common.AbstractFileSource.assertFilePathIsUnderRoot(AbstractFileSource.java:160)
    at com.github.tomakehurst.wiremock.common.AbstractFileSource.getBinaryFileNamed(AbstractFileSource.java:45)
    at com.github.tomakehurst.wiremock.http.StubResponseRenderer.renderDirectly(StubResponseRenderer.java:115)
    at com.github.tomakehurst.wiremock.http.StubResponseRenderer.buildResponse(StubResponseRenderer.java:64)
    at com.github.tomakehurst.wiremock.http.StubResponseRenderer.render(StubResponseRenderer.java:56)
    at com.github.tomakehurst.wiremock.http.AbstractRequestHandler.handle(AbstractRequestHandler.java:50)
    at com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet.service(WireMockHandlerDispatchingServlet.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    ...

The other two require a huge in-memory array or string which I also would like to avoid.

I've also checked the Fault enum but it does not seem extendable.

palacsint
  • 28,416
  • 10
  • 82
  • 109

1 Answers1

1

The reason you saw the file security error is that WireMock will only read files under its configured file root, so setting up a symlink might work.

Failing that, just creating a very large file would do the trick, and won't consume a lot of memory as body files are streamed.

Tom
  • 3,471
  • 21
  • 14
  • Thanks! Writing a file to `/tmp` also returns the exception above. Could you write a PoC, please? I would like to avoid putting a 2GB file into the source tree (although it could be ignored by git). – palacsint Feb 05 '19 at 12:29
  • 1
    As I mentioned, WireMock will only serve files that are under your configured root directory, so you either need to create that under `/tmp` and configure WM to point to it, or symlink the large file from `/tmp` under your `__files` dir. – Tom Feb 06 '19 at 13:43
  • ^ The answer `__files` works! I tried to access files outside of that directory and failed. That was the reason. – Ting Yi Shih Mar 17 '23 at 11:08