WireMock产生巨大或无限的响应

时间:2019-02-04 16:05:30

标签: wiremock

我想使用带有 大量的数据。是否可以创建无限或几GB的长度 使用WireMock输出而不分配具有该大小的字节数组或String?

据我所见ResponseDefinitionBuilder has three withBody* methods

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

我尝试过withBodyFile("/dev/zero"),但遇到以下异常:

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

另外两个需要巨大的内存数组或字符串,我也想避免。

我也检查了Fault enum,但它似乎不可扩展。

1 个答案:

答案 0 :(得分:1)

您看到文件安全性错误的原因是WireMock仅会读取其配置的文件根目录下的文件,因此设置符号链接可能会起作用。

如果不这样做,仅创建一个很大的文件就可以解决问题,并且在流式处理正文文件时不会占用大量内存。