Skip to content

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

Closed
@Romster

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.

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