Support multiple Content-Language values in MockHttpServletResponse
Prior to this commit, if the user supplied a comma-separated list such as "en, it" as the Content-Language header value to MockHttpServletResponse's setHeader() method, only the first language was actually set in the response's Content-Language header (e.g., "en"). This commit ensures that all supplied content languages are set in the response's Content-Language header. Closes gh-25281
This commit is contained in:
@@ -638,10 +638,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
return true;
|
||||
}
|
||||
else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
|
||||
String contentLanguages = value.toString();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_LANGUAGE, value.toString());
|
||||
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
|
||||
Locale language = headers.getContentLanguage();
|
||||
setLocale(language != null ? language : Locale.getDefault());
|
||||
// Since setLocale() sets the Content-Language header to the given
|
||||
// single Locale, we have to explicitly set the Content-Language header
|
||||
// to the user-provided value.
|
||||
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, true);
|
||||
return true;
|
||||
}
|
||||
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {
|
||||
@@ -660,12 +665,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
}
|
||||
|
||||
private void doAddHeaderValue(String name, Object value, boolean replace) {
|
||||
HeaderValueHolder header = this.headers.get(name);
|
||||
Assert.notNull(value, "Header value must not be null");
|
||||
if (header == null) {
|
||||
header = new HeaderValueHolder();
|
||||
this.headers.put(name, header);
|
||||
}
|
||||
HeaderValueHolder header = this.headers.computeIfAbsent(name, key -> new HeaderValueHolder());
|
||||
if (replace) {
|
||||
header.setValue(value);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -30,6 +31,8 @@ import org.springframework.web.util.WebUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.SoftAssertions.assertSoftly;
|
||||
import static org.springframework.http.HttpHeaders.CONTENT_LANGUAGE;
|
||||
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
|
||||
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
|
||||
import static org.springframework.http.HttpHeaders.LAST_MODIFIED;
|
||||
@@ -117,6 +120,26 @@ class MockHttpServletResponseTests {
|
||||
assertThat(response.getCharacterEncoding()).isEqualTo("UTF-8");
|
||||
}
|
||||
|
||||
@Test // gh-25281
|
||||
void contentLanguageHeaderWithSingleValue() {
|
||||
String contentLanguage = "it";
|
||||
response.setHeader(CONTENT_LANGUAGE, contentLanguage);
|
||||
assertSoftly(softly -> {
|
||||
softly.assertThat(response.getHeader(CONTENT_LANGUAGE)).isEqualTo(contentLanguage);
|
||||
softly.assertThat(response.getLocale()).isEqualTo(Locale.ITALIAN);
|
||||
});
|
||||
}
|
||||
|
||||
@Test // gh-25281
|
||||
void contentLanguageHeaderWithMultipleValues() {
|
||||
String contentLanguage = "it, en";
|
||||
response.setHeader(CONTENT_LANGUAGE, contentLanguage);
|
||||
assertSoftly(softly -> {
|
||||
softly.assertThat(response.getHeader(CONTENT_LANGUAGE)).isEqualTo(contentLanguage);
|
||||
softly.assertThat(response.getLocale()).isEqualTo(Locale.ITALIAN);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void setContentTypeThenCharacterEncoding() {
|
||||
response.setContentType("test/plain");
|
||||
|
||||
Reference in New Issue
Block a user