【问题标题】:Huge or infinite response with WireMockWireMock 的巨大或无限响应
【发布时间】:2019-02-04 16:05:30
【问题描述】:

我想使用 HTTP 客户端测试我们的应用 大量的数据。是否可以创建无限或几千兆字节的长度 使用 WireMock 输出而不分配具有该大小的字节数组或字符串?

据我所知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,但它似乎无法扩展。

【问题讨论】:

    标签: wiremock


    【解决方案1】:

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

    如果做不到这一点,只需创建一个非常大的文件就可以解决问题,并且在流式传输正文文件时不会消耗大量内存。

    【讨论】:

    • 谢谢!将文件写入/tmp 也会返回上述异常。请你写一个PoC好吗?我想避免将 2GB 的文件放入源代码树中(尽管它可以被 git 忽略)。
    • 正如我所提到的,WireMock 将只提供您配置的根目录下的文件,因此您需要在 /tmp 下创建它并配置 WM 指向它,或者将大文件从/tmp 在您的__files 目录下。
    猜你喜欢
    • 2012-06-27
    • 2019-02-04
    • 2014-10-21
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多