Set UTF-8 as default multipart charset to ContentRequestMatchers

See gh-31924
This commit is contained in:
Anton Ždanov
2023-12-29 20:25:44 +02:00
committed by rstoyanchev
parent f9883d8bd6
commit 667e30f580
2 changed files with 72 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.test.web.client.match;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
@@ -124,6 +125,72 @@ public class ContentRequestMatchersTests {
.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>";