Skip to content

Commit c02d07f

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-34491
1 parent 7460be6 commit c02d07f

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
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.
@@ -728,14 +728,17 @@ else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
728728
}
729729
else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
730730
String contentLanguages = value.toString();
731-
HttpHeaders headers = new HttpHeaders();
732-
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
733-
Locale language = headers.getContentLanguage();
734-
setLocale(language != null ? language : Locale.getDefault());
735-
// Since setLocale() sets the Content-Language header to the given
736-
// single Locale, we have to explicitly set the Content-Language header
737-
// to the user-provided value.
738-
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, true);
731+
// only set the locale if we replace the header or if there was none before
732+
if (replaceHeader || !this.headers.containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
733+
HttpHeaders headers = new HttpHeaders();
734+
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
735+
Locale language = headers.getContentLanguage();
736+
this.locale = language != null ? language : Locale.getDefault();
737+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, replaceHeader);
738+
}
739+
else {
740+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, false);
741+
}
739742
return true;
740743
}
741744
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {

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

+9-1
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.
@@ -631,4 +631,12 @@ void resetResetsCharset() {
631631
assertThat(contentTypeHeader).isEqualTo("text/plain");
632632
}
633633

634+
@Test // gh-34488
635+
void shouldAddMultipleContentLanguage() {
636+
response.addHeader("Content-Language", "en");
637+
response.addHeader("Content-Language", "fr");
638+
assertThat(response.getHeaders("Content-Language")).contains("en", "fr");
639+
assertThat(response.getLocale()).isEqualTo(Locale.ENGLISH);
640+
}
641+
634642
}

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.
@@ -728,14 +728,17 @@ else if (HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(name)) {
728728
}
729729
else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
730730
String contentLanguages = value.toString();
731-
HttpHeaders headers = new HttpHeaders();
732-
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
733-
Locale language = headers.getContentLanguage();
734-
setLocale(language != null ? language : Locale.getDefault());
735-
// Since setLocale() sets the Content-Language header to the given
736-
// single Locale, we have to explicitly set the Content-Language header
737-
// to the user-provided value.
738-
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, true);
731+
// only set the locale if we replace the header or if there was none before
732+
if (replaceHeader || !this.headers.containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
733+
HttpHeaders headers = new HttpHeaders();
734+
headers.add(HttpHeaders.CONTENT_LANGUAGE, contentLanguages);
735+
Locale language = headers.getContentLanguage();
736+
this.locale = language != null ? language : Locale.getDefault();
737+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, replaceHeader);
738+
}
739+
else {
740+
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, contentLanguages, false);
741+
}
739742
return true;
740743
}
741744
else if (HttpHeaders.SET_COOKIE.equalsIgnoreCase(name)) {

0 commit comments

Comments
 (0)