Clear CONTENT_DISPOSITION before error handling

Closes gh-35116
This commit is contained in:
rstoyanchev
2025-07-07 13:59:00 +01:00
parent 1051592ae8
commit 6bd12e8680
2 changed files with 9 additions and 3 deletions
@@ -924,8 +924,8 @@ class DispatcherServletTests {
assertThat(response.getHeader("Test-Header")).isEqualTo("spring");
}
@Test
void shouldResetContentTypeIfNotCommitted() throws Exception {
@Test // gh-34366, gh-35116
void shouldResetContentHeadersIfNotCommitted() throws Exception {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.setServletContext(getServletContext());
context.registerSingleton("/error", ErrorController.class);
@@ -934,11 +934,15 @@ class DispatcherServletTests {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/error");
MockHttpServletResponse response = new MockHttpServletResponse();
assertThatThrownBy(() -> servlet.service(request, response)).isInstanceOf(ServletException.class)
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);
assertThat(response.getHeaderNames()).doesNotContain(HttpHeaders.CONTENT_DISPOSITION);
}
@@ -994,6 +998,7 @@ class DispatcherServletTests {
response.setStatus(400);
response.setHeader("Test-Header", "spring");
response.addHeader("Content-Type", "application/json");
response.addHeader("Content-Disposition", "attachment; filename=\"report.txt\"");
if (request.getAttribute("commit") != null) {
response.flushBuffer();
}