【发布时间】:2020-10-19 15:37:18
【问题描述】:
我有一个请求端点并以这种方式读取其响应的工作代码(流是 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, 0, len);
}
}
}
//other stuffs...
}
我尝试在使用 JUnit4 @Rule 的测试环境中使用 wiremock 来模拟那个 enpoint,这样:
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 的端点的正确方法?
【问题讨论】:
标签: java rest inputstream junit4 wiremock