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:
Sam Brannen
2020-07-27 17:46:02 +02:00
parent d2db43a900
commit 86c52a842f
3 changed files with 41 additions and 16 deletions

View File

@@ -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");