gpt4 book ai didi

java - MockMvcRequestBuilders.asyncDispatch 提供一个空的 HTTP Response 和 contentType

转载 作者:行者123 更新时间:2023-12-05 07:26:19 27 4
gpt4 key购买 nike

我有一个单元测试,在使用 Spring 4.3 转换方法以返回 StreamingResponseBody 之后,我试图检查发出的异步请求的响应。

测试方法如下:

final MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

MvcResult mvcResult1 = mockMvc.perform(
get("/reports/generic/100?FIELD1=&FIELD3=").headers(STANDARD_HEADERS.get()))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();

mvcResult1.getAsyncResult();

mockMvc.perform(asyncDispatch(mvcResult1))
.andExpect(status().isOk())
.andExpect(content().contentType("text/csv"))
.andExpect(content().string("Test Data" + System.lineSeparator() + "FIELD1=" + System.lineSeparator() + "FIELD3=" + System.lineSeparator()))

它调用的方法如下所示:

public StreamingResponseBody streamReport(@PathVariable("type") @NotNull String type, @PathVariable("id") @NotNull Long id, ReportConfiguration config, HttpServletResponse response) throws Exception {


ReportServiceHandler handler = reportHandlerFactory.getHandler(type);
final String reportFilename = handler.getReportFileName(id, reportConfiguration);

response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + reportFilename);
response.setContentType("text/csv");

return new StreamingResponseBody() {

@Override
public void writeTo(OutputStream outputStream) throws IOException {

try {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + reportFilename);
response.setContentType("text/csv");
ServletOutputStream out = (ServletOutputStream) outputStream;
handler.generateReport(out, id, reportConfiguration);
out.flush();
} catch ( Exception e ) {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline");
response.setContentType("");
throw new IOException(e);
}
}
};
}

调试显示原始请求中包含来自异步的响应,但异步响应对象(在 mvcResult1 中) 期间被复制asyncDispatch 因此 contextType 和内容字符串都为 null。

这里是否遗漏了处理异步 mvcResult 的测试配置,以便可以断言内容?

最佳答案

我在这方面做得相当成功。这是我的不同做法:

    return new StreamingResponseBody() {

@Override
public void writeTo(OutputStream outputStream) throws IOException {

try {
... same
out.flush();
} catch ( Exception e ) {
... your error handling
} finally {
// Try to close the stream
try {
if (out != null) {
out.close();
}
} catch (Throwable t) {
// not much we can do
}
}
}
};

现在,当我的测试与您的测试平行时,看起来是这样的。阅读您的代码时,我不确定您为什么调用 perform() 两次。对我来说只需要一次。

//Same
final MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();

// Same
MvcResult mvcResult1 = mockMvc.perform(
get(....))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();

mvcResult1.getAsyncResult();

// From this point on, I have complete response,
// if it's not bugging you to place the entire file in memory
String thisIsYourFileContent = mvcResult1.getResponse().getContentAsString();

// Go on asserting the content of the file...

希望这对某人有所帮助。

关于java - MockMvcRequestBuilders.asyncDispatch 提供一个空的 HTTP Response 和 contentType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54412062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com