Skip to content

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

Description

@Romster

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.

Activity

  1. added
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    on Feb 25, 2025
  2. self-assigned this
    on Feb 25, 2025
  3. added and removed
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    on Feb 25, 2025
  4. added this to the 6.2.4 milestone on Feb 25, 2025
  5. added a commit that references this issue on Feb 25, 2025
    b6a5402
  6. bclozel commented on Feb 25, 2025

    @bclozel
    Member

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

  7. Romster commented on Feb 25, 2025

    @Romster
    Author

    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

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions