Skip to content

Commit 667e30f

Browse files
azdanovrstoyanchev
authored andcommitted
Set UTF-8 as default multipart charset to ContentRequestMatchers
See gh-31924
1 parent f9883d8 commit 667e30f

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/web/client/match/ContentRequestMatchers.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
*/
6161
public class ContentRequestMatchers {
6262

63+
private static final String DEFAULT_ENCODING = "UTF-8";
64+
6365
private final XmlExpectationsHelper xmlHelper;
6466

6567
private final JsonExpectationsHelper jsonHelper;
@@ -367,7 +369,9 @@ private static class MultipartHelper {
367369
public static MultiValueMap<String, ?> parse(MockClientHttpRequest request) {
368370
try {
369371
FileUpload fileUpload = new FileUpload();
370-
fileUpload.setFileItemFactory(new DiskFileItemFactory());
372+
DiskFileItemFactory factory = new DiskFileItemFactory();
373+
factory.setDefaultCharset(DEFAULT_ENCODING);
374+
fileUpload.setFileItemFactory(factory);
371375

372376
List<FileItem> fileItems = fileUpload.parseRequest(new UploadContext() {
373377
private final byte[] body = request.getBodyAsBytes();

Diff for: spring-test/src/test/java/org/springframework/test/web/client/match/ContentRequestMatchersTests.java

+67
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.nio.charset.StandardCharsets;
2020
import java.util.Collections;
21+
import java.util.Map;
2122

2223
import org.junit.jupiter.api.Test;
2324

@@ -124,6 +125,72 @@ public void testFormDataContains() throws Exception {
124125
.match(this.request);
125126
}
126127

128+
@Test
129+
public void testMultipartData() throws Exception {
130+
String contentType = "multipart/form-data;boundary=1234567890";
131+
String body = "--1234567890\r\n" +
132+
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
133+
"\r\n" +
134+
"vølue 1\r\n" +
135+
"--1234567890\r\n" +
136+
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
137+
"\r\n" +
138+
"value 🙂\r\n" +
139+
"--1234567890\r\n" +
140+
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
141+
"\r\n" +
142+
"value 漢字\r\n" +
143+
"--1234567890\r\n" +
144+
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
145+
"\r\n" +
146+
"\r\n" +
147+
"--1234567890--\r\n";
148+
149+
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
150+
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
151+
152+
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
153+
map.add("name 1", "vølue 1");
154+
map.add("name 2", "value 🙂");
155+
map.add("name 3", "value 漢字");
156+
map.add("name 4", "");
157+
MockRestRequestMatchers.content().multipartData(map).match(this.request);
158+
}
159+
160+
@Test
161+
public void testMultipartDataContains() throws Exception {
162+
String contentType = "multipart/form-data;boundary=1234567890";
163+
String body = "--1234567890\r\n" +
164+
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
165+
"\r\n" +
166+
"vølue 1\r\n" +
167+
"--1234567890\r\n" +
168+
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
169+
"\r\n" +
170+
"value 🙂\r\n" +
171+
"--1234567890\r\n" +
172+
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
173+
"\r\n" +
174+
"value 漢字\r\n" +
175+
"--1234567890\r\n" +
176+
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
177+
"\r\n" +
178+
"\r\n" +
179+
"--1234567890--\r\n";
180+
181+
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
182+
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
183+
184+
MockRestRequestMatchers.content()
185+
.multipartDataContains(Map.of(
186+
"name 1", "vølue 1",
187+
"name 2", "value 🙂",
188+
"name 3", "value 漢字",
189+
"name 4", "")
190+
)
191+
.match(this.request);
192+
}
193+
127194
@Test
128195
public void testXml() throws Exception {
129196
String content = "<foo><bar>baz</bar><bar>bazz</bar></foo>";

0 commit comments

Comments
 (0)