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

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

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

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -424,7 +424,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the names of all specified headers as a Set of Strings.
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
*/
@Override
@@ -435,7 +435,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return the primary value for the given header as a String, if any.
* Will return the first value in case of multiple values.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValue(String)} for raw Object access.
* @param name the name of the header
@@ -450,7 +450,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
/**
* Return all values for the given header as a List of Strings.
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
* <p>As of Servlet 3.0, this method is also defined in {@link HttpServletResponse}.
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
* Consider using {@link #getHeaderValues(String)} for raw Object access.
* @param name the name of the header
@@ -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);
}