Skip to content

Commit b6a5402

Browse files
committed
Fix multiple Content-Language values in MockHttpServletResponse
Prior to this commit, `MockHttpServletResponse` would only support adding a `Content-Language` once. Adding multiple header values would always replace the content-language property in the response and the entire header value. This commit ensures that this behavior is supported. Fixes gh-34488
1 parent aff9ac7 commit b6a5402

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -743,14 +743,17 @@ else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
743743
}
744744
else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
745745
String contentLanguages = value.toString();
746-
HttpHeaders headers = new HttpHeaders();
747-
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
748-
Locale language = headers.getContentLanguage();
749-
setLocale(language != null ? language : Locale.getDefault());
750-
// Since setLocale() sets the Content-Language header to the given
751-
// single Locale, we have to explicitly set the Content-Language header
752-
// to the user-provided value.
753-
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, true);
746+
// only set the locale if we replace the header or if there was none before
747+
if (replaceHeader || !this.headers.containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
748+
HttpHeaders headers = new HttpHeaders();
749+
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
750+
Locale language = headers.getContentLanguage();
751+
this.locale = language != null ? language : Locale.getDefault();
752+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, replaceHeader);
753+
}
754+
else {
755+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, false);
756+
}
754757
return true;
755758
}
756759
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {

spring-test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -641,4 +641,12 @@ void contentAsStringEncodingWithJson() throws IOException {
641641
assertThat(response.getContentAsString()).isEqualTo(content);
642642
}
643643

644+
@Test // gh-34488
645+
void shouldAddMultipleContentLanguage() {
646+
response.addHeader("Content-Language", "en");
647+
response.addHeader("Content-Language", "fr");
648+
assertThat(response.getHeaders("Content-Language")).contains("en", "fr");
649+
assertThat(response.getLocale()).isEqualTo(Locale.ENGLISH);
650+
}
651+
644652
}

spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockHttpServletResponse.java

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -743,14 +743,17 @@ else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
743743
}
744744
else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
745745
String contentLanguages = value.toString();
746-
HttpHeaders headers = new HttpHeaders();
747-
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
748-
Locale language = headers.getContentLanguage();
749-
setLocale(language != null ? language : Locale.getDefault());
750-
// Since setLocale() sets the Content-Language header to the given
751-
// single Locale, we have to explicitly set the Content-Language header
752-
// to the user-provided value.
753-
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, true);
746+
// only set the locale if we replace the header or if there was none before
747+
if (replaceHeader || !this.headers.containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
748+
HttpHeaders headers = new HttpHeaders();
749+
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
750+
Locale language = headers.getContentLanguage();
751+
this.locale = language != null ? language : Locale.getDefault();
752+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, replaceHeader);
753+
}
754+
else {
755+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, false);
756+
}
754757
return true;
755758
}
756759
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {

0 commit comments

Comments
 (0)