-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Vladimir L. opened SPR-14456 and commented
This is a ticket to comply with contributing guideline of Spring Framework project for already created pull-request.
Background story
While testing my Spring MVC application I came across your great examples of how to test asynchronous request processing using spring-test
library in AsyncTests.java file.
My application however is designed to steam a huge amount of data and thus controllers return instances of StreamingResponseBody
interface.
Unfortunately the examples from AsyncTests.java
did not work as expected. During second step after the asyncDispatch(mvcResult)
method my expectations against content()
always failed claiming that response is empty whereas I've expected some data to be written to output stream.
It took me some time to debug and figure out that the asyncDispatch(...)
method is actually returns back only the modified version of original request, and the perform(...)
method creates a new instance of MvcResult
with a fresh version of MockHttpServletResponse
. The logic inside controllers was writing the data into original HTTP response generated by initial perform(...)
method, while I was validating the content of HTTP response generated by second perform(...)
.
Test sample
@Test
public void streaming() throws Exception {
this.mockMvc.perform(get("/streamingresult"))
.andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult()) // fetch async result similar to what "asyncDispatch" builder does
.andExpect(status().isOk())
.andExpect(content().string("expected content of streamed data ..."));
}
Affects: 4.3.1
Reference URL: #1106
Referenced from: commits df55633