8000 Extend Content-Disposition "inline" to error responses · spring-projects/spring-framework@16d125c · GitHub
[go: up one dir, main page]

Skip to content

Commit 16d125c

Browse files
committed
Extend Content-Disposition "inline" to error responses
We don't expect a browser to save error responses to a file but we extend this protection anyway since "inline" is only a suggestion that shouldn't have any side effects.
1 parent dccc781 commit 16d125c

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ private void addContentDispositionHeader(ServletServerHttpRequest request, Servl
411411

412412
try {
413413
int status = response.getServletResponse().getStatus();
414-
if (status < 200 || status > 299) {
414+
if (status < 200 || (status > 299 && status < 400)) {
415415
return;
416416
}
417417
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,25 @@ public void addContentDispositionHeader() throws Exception {
400400
this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
401401
}
402402

403+
@Test
404+
public void addContentDispositionHeaderToErrorResponse() throws Exception {
405+
ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
406+
factory.addMediaType("pdf", new MediaType("application", "pdf"));
407+
factory.afterPropertiesSet();
408+
409+
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
410+
Collections.singletonList(new StringHttpMessageConverter()),
411+
factory.getObject());
412+
413+
this.servletRequest.setRequestURI("/hello.dataless");
414+
this.servletResponse.setStatus(400);
415+
416+
processor.handleReturnValue("body", this.returnTypeString, this.container, this.request);
417+
418+
String header = servletResponse.getHeader("Content-Disposition");
419+
assertThat(header).isEqualTo("inline;filename=f.txt");
420+
}
421+
403422
@Test
404423
public void supportsReturnTypeResponseBodyOnType() throws Exception {
405424
Method method = ResponseBodyController.class.getMethod("handle");
@@ -724,10 +743,14 @@ private void assertContentDisposition(RequestResponseBodyMethodProcessor process
724743

725744
String header = servletResponse.getHeader("Content-Disposition");
726745
if (expectContentDisposition) {
727-
assertThat(header).as("Expected 'Content-Disposition' header. Use case: '" + comment + "'").isEqualTo("inline;filename=f.txt");
746+
assertThat(header)
747+
.as("Expected 'Content-Disposition' header. Use case: '" + comment + "'")
748+
.isEqualTo("inline;filename=f.txt");
728749
}
729750
else {
730-
assertThat(header).as("Did not expect 'Content-Disposition' header. Use case: '" + comment + "'").isNull();
751+
assertThat(header)
752+
.as("Did not expect 'Content-Disposition' header. Use case: '" + comment + "'")
753+
.isNull();
731754
}
732755

733756
this.servletRequest = new MockHttpServletRequest();

0 commit comments

Comments
 (0)
0