Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MockHttpServletResponse - handle multiple values for Content-Language header #34488

Closed
Romster opened this issue Feb 25, 2025 · 2 comments
Closed
Assignees
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@Romster
Copy link

Romster commented Feb 25, 2025

Affects: 6.x+

It seems that if we provide multiple values for CONTENT_LANGUAGE as an array like:

ResponseEntity.ok()
      .header(HttpHeaders.CONTENT_LANGUAGE, contentLanguage.toArray(new String[0]))
      .body(body);

It won't work nicely with MockHttpServletResponse.

The logic we have there in setSpecialHeader method:

else if (HttpHeaders.CONTENT_LANGUAGE.equalsIgnoreCase(name)) {
String contentLanguages = value.toString();
HttpHeaders headers = new HttpHeaders();
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;
}

expects that all the values are sent in a value.

but from debugging, I saw that ServletServerHttpResponse will call setSpecialHeader of MockHttpServletResponse for each header's value.

private void writeHeaders() {
if (!this.headersWritten) {
getHeaders().forEach((headerName, headerValues) -> {
for (String headerValue : headerValues) {
this.servletResponse.addHeader(headerName, headerValue);
}
});

Thus, only the last value will be present in the response.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 25, 2025
@bclozel bclozel added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Feb 25, 2025
@sbrannen sbrannen added the in: test Issues in the test module label Feb 25, 2025
@bclozel bclozel self-assigned this Feb 25, 2025
@bclozel bclozel added type: bug A general bug and removed in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on labels Feb 25, 2025
@bclozel bclozel added this to the 6.2.4 milestone Feb 25, 2025
@bclozel bclozel added the for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x label Feb 25, 2025
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.1.x Marks an issue as a candidate for backport to 6.1.x labels Feb 25, 2025
@bclozel
Copy link
Member

bclozel commented Feb 25, 2025

Thanks for the report @Romster this has been fixed for the next set of maintenance releases.

@Romster
Copy link
Author

Romster commented Feb 25, 2025

Wow! That was fast! Thank you @bclozel !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants