Skip to content

Commit 4ce2559

Browse files
committed
Merge branch '6.1.x'
2 parents 1c5bb73 + 7493ce8 commit 4ce2559

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.http.HttpHeaders;
2929
import org.springframework.http.HttpStatusCode;
30+
import org.springframework.http.MediaType;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.CollectionUtils;
@@ -118,12 +119,13 @@ private void writeHeaders() {
118119
}
119120
});
120121
// HttpServletResponse exposes some headers as properties: we should include those if not already present
121-
if (this.servletResponse.getContentType() == null && this.headers.getContentType() != null) {
122-
this.servletResponse.setContentType(this.headers.getContentType().toString());
122+
MediaType contentTypeHeader = this.headers.getContentType();
123+
if (this.servletResponse.getContentType() == null && contentTypeHeader != null) {
124+
this.servletResponse.setContentType(contentTypeHeader.toString());
123125
}
124-
if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null &&
125-
this.headers.getContentType().getCharset() != null) {
126-
this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name());
126+
if (this.servletResponse.getCharacterEncoding() == null && contentTypeHeader != null &&
127+
contentTypeHeader.getCharset() != null) {
128+
this.servletResponse.setCharacterEncoding(contentTypeHeader.getCharset().name());
127129
}
128130
long contentLength = getHeaders().getContentLength();
129131
if (contentLength != -1) {
@@ -169,13 +171,15 @@ public String getFirst(String headerName) {
169171
}
170172

171173
@Override
174+
@Nullable
172175
public List<String> get(Object key) {
173176
Assert.isInstanceOf(String.class, key, "Key must be a String-based header name");
174177

175178
String headerName = (String) key;
176179
if (headerName.equalsIgnoreCase(CONTENT_TYPE)) {
177180
// Content-Type is written as an override so don't merge
178-
return Collections.singletonList(getFirst(headerName));
181+
String value = getFirst(headerName);
182+
return (value != null ? Collections.singletonList(value) : null);
179183
}
180184

181185
Collection<String> values1 = servletResponse.getHeaders(headerName);

spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ void getHeaders() {
7575
assertThat(mockResponse.getCharacterEncoding()).as("Invalid Content-Type").isEqualTo("UTF-8");
7676
}
7777

78+
@Test
79+
void getHeadersWithNoContentType() {
80+
this.response = new ServletServerHttpResponse(this.mockResponse);
81+
assertThat(this.response.getHeaders().get(HttpHeaders.CONTENT_TYPE)).isNull();
82+
}
83+
84+
@Test
85+
void getHeadersWithContentType() {
86+
this.mockResponse.setContentType(MediaType.TEXT_PLAIN_VALUE);
87+
this.response = new ServletServerHttpResponse(this.mockResponse);
88+
assertThat(this.response.getHeaders().get(HttpHeaders.CONTENT_TYPE)).containsExactly(MediaType.TEXT_PLAIN_VALUE);
89+
}
90+
7891
@Test
7992
void preExistingHeadersFromHttpServletResponse() {
8093
String headerName = "Access-Control-Allow-Origin";

0 commit comments

Comments
 (0)