Skip to content

Commit

Permalink
Merge branch '6.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Feb 25, 2025
2 parents 75329e6 + f895d76 commit 8b14bf8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.ServletServerHttpRequest;
Expand Down Expand Up @@ -1208,9 +1209,10 @@ protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletExcepti

// Success and error responses may use different content types
request.removeAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
// Reset the response body buffer if the response is not committed already,
// leaving the response headers in place.
// Reset the response content-type header and body buffer if the response is not committed already,
// leaving the other response headers in place.
try {
response.setHeader(HttpHeaders.CONTENT_TYPE, null);
response.resetBuffer();
}
catch (IllegalStateException illegalStateException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,23 @@ void shouldAttemptToResetResponseBufferIfCommitted() throws Exception {
assertThat(response.getHeader("Test-Header")).isEqualTo("spring");
}

@Test
void shouldResetContentTypeIfNotCommitted() throws Exception {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.setServletContext(getServletContext());
context.registerSingleton("/error", ErrorController.class);
DispatcherServlet servlet = new DispatcherServlet(context);
servlet.init(servletConfig);

MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error");
MockHttpServletResponse response = new MockHttpServletResponse();
assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class)
.hasCauseInstanceOf(IllegalArgumentException.class);
assertThat(response.getContentAsByteArray()).isEmpty();
assertThat(response.getStatus()).isEqualTo(400);
assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_TYPE);
}


public static class ControllerFromParent implements Controller {

Expand Down Expand Up @@ -950,6 +967,7 @@ private static class ErrorController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setStatus(400);
response.setHeader("Test-Header", "spring");
response.addHeader("Content-Type", "application/json");
if (request.getAttribute("commit") != null) {
response.flushBuffer();
}
Expand Down

0 comments on commit 8b14bf8

Please sign in to comment.