Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Wiremock:如何模拟返回InputStream的端点?_Java_Rest_Inputstream_Junit4_Wiremock - Fatal编程技术网

Java Wiremock:如何模拟返回InputStream的端点?

Java Wiremock:如何模拟返回InputStream的端点?,java,rest,inputstream,junit4,wiremock,Java,Rest,Inputstream,Junit4,Wiremock,我有一个工作代码,它请求端点并以这种方式读取其响应(流是PDF): 我尝试在测试环境中使用JUnit4@Rule使用wiremock模拟该enpoint,方法如下: byte[] pdfFile = Files.readAllBytes(Paths.get(ClassLoader.getSystemResource("file.pdf").toURI())); stubFor( get(urlPathMatching(mockPath)) .withHeader(&q

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

我尝试在测试环境中使用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的端点的正确方法是什么?

基于,我猜您可以将文件路径作为响应主体返回

  .willReturn(aResponse()
    .withStatus(200)
    .withBodyFile("/path/to/pdf/file")));
如果这不起作用,我建议在响应中添加内容类型标题。假设文件是pdf格式的,那么

.withHeader("Content-Type", "application/pdf")

在花了一些时间阅读Wiremock文档后,我发现了问题所在。创建下载某个文件的存根的一种方法是将该文件放在
src/test/resources/\uu files
目录下,如果我要使用以下方法:

withBodyFile("file.pdf")

默认情况下,这是Wiremock服务器通过存根下载任何文件的目录。这解决了我的问题。

我试过你的方法。现在错误已经改变了。我得到:javax.ws.rs.ProcessingException:org.apache.http.ConnectionClosedException:Content-Length分隔的消息正文过早结束(预期:2674;接收:0…wiremock返回状态500!我使用File.exists()测试了给定的路径我怀疑路径不是任意的或绝对的,但必须是指向
src/test/resources/\uu文件的相对路径
。此外,您可以使用下面列出的方法更改WireMock查找文件的位置:
withBodyFile("file.pdf")