Polishing contribution
Closes gh-31924
This commit is contained in:
@@ -19,6 +19,8 @@ package org.springframework.test.web.client.match;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -60,7 +62,12 @@ import static org.springframework.test.util.AssertionErrors.assertTrue;
|
|||||||
*/
|
*/
|
||||||
public class ContentRequestMatchers {
|
public class ContentRequestMatchers {
|
||||||
|
|
||||||
private static final String DEFAULT_ENCODING = "UTF-8";
|
/**
|
||||||
|
* The encoding for parsing multipart content when the sender hasn't specified it.
|
||||||
|
* @see DiskFileItemFactory#setDefaultCharset(String)
|
||||||
|
*/
|
||||||
|
private static final Charset DEFAULT_MULTIPART_ENCODING = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
|
||||||
private final XmlExpectationsHelper xmlHelper;
|
private final XmlExpectationsHelper xmlHelper;
|
||||||
|
|
||||||
@@ -206,7 +213,15 @@ public class ContentRequestMatchers {
|
|||||||
* @since 5.3
|
* @since 5.3
|
||||||
*/
|
*/
|
||||||
public RequestMatcher multipartData(MultiValueMap<String, ?> expectedMap) {
|
public RequestMatcher multipartData(MultiValueMap<String, ?> expectedMap) {
|
||||||
return multipartData(expectedMap, true);
|
return multipartData(expectedMap, DEFAULT_MULTIPART_ENCODING, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variant of {@link #multipartData(MultiValueMap)} with a defaultCharset.
|
||||||
|
* @since 6.2
|
||||||
|
*/
|
||||||
|
public RequestMatcher multipartData(MultiValueMap<String, ?> expectedMap, Charset defaultCharset) {
|
||||||
|
return multipartData(expectedMap, defaultCharset, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,13 +236,15 @@ public class ContentRequestMatchers {
|
|||||||
public RequestMatcher multipartDataContains(Map<String, ?> expectedMap) {
|
public RequestMatcher multipartDataContains(Map<String, ?> expectedMap) {
|
||||||
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(expectedMap.size());
|
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(expectedMap.size());
|
||||||
expectedMap.forEach(map::add);
|
expectedMap.forEach(map::add);
|
||||||
return multipartData(map, false);
|
return multipartData(map, DEFAULT_MULTIPART_ENCODING, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
@SuppressWarnings("ConstantConditions")
|
||||||
private RequestMatcher multipartData(MultiValueMap<String, ?> expectedMap, boolean containsExactly) {
|
private RequestMatcher multipartData(
|
||||||
|
MultiValueMap<String, ?> expectedMap, Charset defaultCharset, boolean containsExactly) {
|
||||||
|
|
||||||
return request -> {
|
return request -> {
|
||||||
MultiValueMap<String, ?> actualMap = MultipartHelper.parse((MockClientHttpRequest) request);
|
MultiValueMap<String, ?> actualMap = MultipartHelper.parse((MockClientHttpRequest) request, defaultCharset);
|
||||||
if (containsExactly) {
|
if (containsExactly) {
|
||||||
assertEquals("Multipart request content: " + actualMap, expectedMap.size(), actualMap.size());
|
assertEquals("Multipart request content: " + actualMap, expectedMap.size(), actualMap.size());
|
||||||
}
|
}
|
||||||
@@ -366,11 +383,11 @@ public class ContentRequestMatchers {
|
|||||||
|
|
||||||
private static class MultipartHelper {
|
private static class MultipartHelper {
|
||||||
|
|
||||||
public static MultiValueMap<String, ?> parse(MockClientHttpRequest request) {
|
public static MultiValueMap<String, ?> parse(MockClientHttpRequest request, Charset defaultCharset) {
|
||||||
try {
|
try {
|
||||||
FileUpload fileUpload = new FileUpload();
|
FileUpload fileUpload = new FileUpload();
|
||||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||||
factory.setDefaultCharset(DEFAULT_ENCODING);
|
factory.setDefaultCharset(defaultCharset.name());
|
||||||
fileUpload.setFileItemFactory(factory);
|
fileUpload.setFileItemFactory(factory);
|
||||||
|
|
||||||
List<FileItem> fileItems = fileUpload.parseRequest(new UploadContext() {
|
List<FileItem> fileItems = fileUpload.parseRequest(new UploadContext() {
|
||||||
|
|||||||
@@ -128,23 +128,25 @@ public class ContentRequestMatchersTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testMultipartData() throws Exception {
|
public void testMultipartData() throws Exception {
|
||||||
String contentType = "multipart/form-data;boundary=1234567890";
|
String contentType = "multipart/form-data;boundary=1234567890";
|
||||||
String body = "--1234567890\r\n" +
|
String body = """
|
||||||
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 1"\r
|
||||||
"vølue 1\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
vølue 1\r
|
||||||
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 2"\r
|
||||||
"value 🙂\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
value 🙂\r
|
||||||
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 3"\r
|
||||||
"value 漢字\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
value 漢字\r
|
||||||
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 4"\r
|
||||||
"\r\n" +
|
\r
|
||||||
"--1234567890--\r\n";
|
\r
|
||||||
|
--1234567890--\r
|
||||||
|
""";
|
||||||
|
|
||||||
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
|
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
|
||||||
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
|
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
|
||||||
@@ -160,23 +162,25 @@ public class ContentRequestMatchersTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testMultipartDataContains() throws Exception {
|
public void testMultipartDataContains() throws Exception {
|
||||||
String contentType = "multipart/form-data;boundary=1234567890";
|
String contentType = "multipart/form-data;boundary=1234567890";
|
||||||
String body = "--1234567890\r\n" +
|
String body = """
|
||||||
"Content-Disposition: form-data; name=\"name 1\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 1"\r
|
||||||
"vølue 1\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
vølue 1\r
|
||||||
"Content-Disposition: form-data; name=\"name 2\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 2"\r
|
||||||
"value 🙂\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
value 🙂\r
|
||||||
"Content-Disposition: form-data; name=\"name 3\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 3"\r
|
||||||
"value 漢字\r\n" +
|
\r
|
||||||
"--1234567890\r\n" +
|
value 漢字\r
|
||||||
"Content-Disposition: form-data; name=\"name 4\"\r\n" +
|
--1234567890\r
|
||||||
"\r\n" +
|
Content-Disposition: form-data; name="name 4"\r
|
||||||
"\r\n" +
|
\r
|
||||||
"--1234567890--\r\n";
|
\r
|
||||||
|
--1234567890--\r
|
||||||
|
""";
|
||||||
|
|
||||||
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
|
this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType));
|
||||||
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
|
this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8));
|
||||||
|
|||||||
Reference in New Issue
Block a user