Skip to content

Commit

Permalink
Set UTF-8 as default multipart charset to ContentRequestMatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
azdanov authored and rstoyanchev committed Mar 12, 2024
1 parent f9883d8 commit 667e30f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
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 @@ -367,7 +369,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);

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

0 comments on commit 667e30f

Please sign in to comment.