Only write non-default charset in FormHttpMessageConverter

This commit only writes the 'charset' parameter in the written headers
if it is non-default (not UTF-8), since RFC7578 states that the only
allowed parameter is 'boundary'.

See gh-25885
Closes gh-26290
This commit is contained in:
Arjen Poutsma
2021-01-08 14:03:58 +01:00
parent 69ce7d33b9
commit ce1ae2f1b2
2 changed files with 27 additions and 1 deletions

View File

@@ -273,6 +273,29 @@ public class FormHttpMessageConverterTests {
.endsWith("><string>foo</string></MyBean>");
}
@Test
public void writeMultipartCharset() throws Exception {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
parts.add("logo", logo);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
MediaType contentType = outputMessage.getHeaders().getContentType();
Map<String, String> parameters = contentType.getParameters();
assertThat(parameters).containsOnlyKeys("boundary");
this.converter.setCharset(StandardCharsets.ISO_8859_1);
outputMessage = new MockHttpOutputMessage();
this.converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
parameters = outputMessage.getHeaders().getContentType().getParameters();
assertThat(parameters).containsOnlyKeys("boundary", "charset");
assertThat(parameters).containsEntry("charset", "ISO-8859-1");
}
private void assertCanRead(MediaType mediaType) {
assertCanRead(MultiValueMap.class, mediaType);
}