Skip to content

Commit

Permalink
Flush headers after null StreamingResponseBody
Browse files Browse the repository at this point in the history
Issue: SPR-14315
  • Loading branch information
zhoulifu authored and rstoyanchev committed May 31, 2016
1 parent 71463fb commit 7e95cd8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType,
returnValue = responseEntity.getBody();
if (returnValue == null) {
mavContainer.setRequestHandled(true);
// Ensure headers are flushed
outputMessage.flush();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.Test;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class StreamingResponseBodyReturnValueHandlerTests {

private MockHttpServletResponse response;

private HttpHeaders headers = new HttpHeaders();


@Before
public void setUp() throws Exception {
Expand All @@ -69,6 +72,8 @@ public void setUp() throws Exception {
this.response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, this.response);

this.headers.add("foo", "bar");

AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
Expand Down Expand Up @@ -140,6 +145,14 @@ public void responseEntityNoContent() throws Exception {
assertEquals(204, this.response.getStatus());
}

@Test
public void responseEntityWithHeadersAndNoContent() throws Exception {
MethodParameter returnType = returnType(TestController.class, "handleResponseEntity");
ResponseEntity<?> emitter = ResponseEntity.noContent().headers(headers).build();
this.handler.handleReturnValue(emitter, returnType, this.mavContainer, this.webRequest);

assertEquals(this.response.getHeaders("foo"), this.headers.get("foo"));
}

private MethodParameter returnType(Class<?> clazz, String methodName) throws NoSuchMethodException {
Method method = clazz.getDeclaredMethod(methodName);
Expand Down

0 comments on commit 7e95cd8

Please sign in to comment.