Wiremock:如何模拟返回InputStream的终结点?

问题描述

我有一个工作代码,可以请求一个端点并以此方式读取其响应(流是PDF):

private Response readResponseBody(Response response) throws IOException {
  InputStream inputStream = response.readEntity(InputStream.class);
  try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
    if (inputStream != null) {
      byte[] buffer = new byte[1024];
      int len;
      while ((len = inputStream.read(buffer)) != -1) { //error this line with wiremock
        os.write(buffer,len);
      }
    }
  }
  //other stuffs...
}

我尝试使用JUnit4 @Rule在测试环境中使用Wiremock模拟这种情况,

byte[] pdfFile = Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("file.pdf").toURI()));
stubFor(
  get(urlPathMatching(mockPath))
  .withHeader("Authorization",equalTo(mockedToken))
  .willReturn(aResponse()
    .withStatus(200)
    .withBody(pdfFile)));

但是当我请求模拟的端点时,我无法读取InputStream,我在上面的引用行中收到了此错误:

org.apache.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected

哪种方法是模拟使用Wiremock返回InputStream的端点的正确方法?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)