Skip to content

Use UTF-8 by default for JSON multipart content in ContentRequestMatchers #31924

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
*/
public class ContentRequestMatchers {

private static final String DEFAULT_ENCODING = "UTF-8";

private final XmlExpectationsHelper xmlHelper;

private final JsonExpectationsHelper jsonHelper;
Expand Down Expand Up @@ -363,7 +365,9 @@ private static class MultipartHelper {
public static MultiValueMap<String, ?> parse(MockClientHttpRequest request) {
try {
FileUpload fileUpload = new FileUpload();
fileUpload.setFileItemFactory(new DiskFileItemFactory());
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setDefaultCharset(DEFAULT_ENCODING);
fileUpload.setFileItemFactory(factory);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching the general default could break existing tests, and that should wait for a major or minor release. In the meantime, we could narrow the scope and default to UTF-8 specifically for JSON. That would be safer and addresses the original issue.


List<FileItem> fileItems = fileUpload.parseRequest(new UploadContext() {
private final byte[] body = request.getBodyAsBytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -124,6 +125,72 @@ public void testFormDataContains() throws Exception {
.match(this.request);
}

@Test
public void testMultipartData() throws Exception {
String contentType = "multipart/form-data;boundary=1234567890";
String body = "--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
"\r\n" +
"vølue 1\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
"\r\n" +
"value 🙂\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
"\r\n" +
"value 漢字\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
"\r\n" +
"\r\n" +
"--1234567890--\r\n";

this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("name 1", "vølue 1");
map.add("name 2", "value 🙂");
map.add("name 3", "value 漢字");
map.add("name 4", "");
MockRestRequestMatchers.content().multipartData(map).match(this.request);
}

@Test
public void testMultipartDataContains() throws Exception {
String contentType = "multipart/form-data;boundary=1234567890";
String body = "--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
"\r\n" +
"vølue 1\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
"\r\n" +
"value 🙂\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
"\r\n" +
"value 漢字\r\n" +
"--1234567890\r\n" +
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
"\r\n" +
"\r\n" +
"--1234567890--\r\n";

this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));

MockRestRequestMatchers.content()
.multipartDataContains(Map.of(
"name 1", "vølue 1",
"name 2", "value 🙂",
"name 3", "value 漢字",
"name 4", "")
)
.match(this.request);
}

@Test
public void testXml() throws Exception {
String content = "<foo><bar>baz</bar><bar>bazz</bar></foo>";
Expand Down