Add explicit support for multipart/mixed in FormHttpMessageConverter

Commit 5008423408 added support for
multipart/* media types in FormHttpMessageConverter, but users still had
to manually register multipart/mixed as a supported media type in order
to POST multipart data with that content type.

This commit removes the need to manually register multipart/mixed as a
supported media type by registering it automatically in
FormHttpMessageConverter. In addition, this commit introduces
MULTIPART_MIXED and MULTIPART_MIXED_VALUE constants in MediaType.

Closes gh-23209
This commit is contained in:
Sam Brannen
2019-06-29 11:40:49 +03:00
parent 75d1428e24
commit 896496341a
5 changed files with 65 additions and 45 deletions

View File

@@ -49,7 +49,9 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import static org.springframework.http.MediaType.MULTIPART_MIXED;
import static org.springframework.http.MediaType.TEXT_XML;
import static org.springframework.http.converter.FormHttpMessageConverter.MULTIPART_ALL;
/**
* Unit tests for {@link FormHttpMessageConverter} and
@@ -61,8 +63,6 @@ import static org.springframework.http.MediaType.TEXT_XML;
*/
public class FormHttpMessageConverterTests {
private static final MediaType MULTIPART_ALL = new MediaType("multipart", "*");
private static final MediaType MULTIPART_MIXED = new MediaType("multipart", "mixed");
private static final MediaType MULTIPART_RELATED = new MediaType("multipart", "related");
private final FormHttpMessageConverter converter = new AllEncompassingFormHttpMessageConverter();
@@ -80,24 +80,19 @@ public class FormHttpMessageConverterTests {
@Test
public void cannotReadMultipart() {
// Without custom multipart types supported
assertCannotRead(MULTIPART_ALL);
assertCannotRead(MULTIPART_FORM_DATA);
assertCannotRead(MULTIPART_MIXED);
assertCannotRead(MULTIPART_RELATED);
asssertCannotReadMultipart();
this.converter.addSupportedMediaTypes(MULTIPART_MIXED, MULTIPART_RELATED);
this.converter.addSupportedMediaTypes(MULTIPART_RELATED);
// With custom multipart types supported
assertCannotRead(MULTIPART_ALL);
assertCannotRead(MULTIPART_FORM_DATA);
assertCannotRead(MULTIPART_MIXED);
assertCannotRead(MULTIPART_RELATED);
// Should still be the case with custom multipart types supported
asssertCannotReadMultipart();
}
@Test
public void canWrite() {
assertCanWrite(APPLICATION_FORM_URLENCODED);
assertCanWrite(MULTIPART_FORM_DATA);
assertCanWrite(MULTIPART_MIXED);
assertCanWrite(new MediaType("multipart", "form-data", StandardCharsets.UTF_8));
assertCanWrite(MediaType.ALL);
assertCanWrite(null);
@@ -105,26 +100,21 @@ public class FormHttpMessageConverterTests {
@Test
public void setSupportedMediaTypes() {
assertCannotWrite(MULTIPART_MIXED);
assertCannotWrite(MULTIPART_RELATED);
List<MediaType> supportedMediaTypes = new ArrayList<>(this.converter.getSupportedMediaTypes());
supportedMediaTypes.add(MULTIPART_MIXED);
supportedMediaTypes.add(MULTIPART_RELATED);
this.converter.setSupportedMediaTypes(supportedMediaTypes);
assertCanWrite(MULTIPART_MIXED);
assertCanWrite(MULTIPART_RELATED);
}
@Test
public void addSupportedMediaTypes() {
assertCannotWrite(MULTIPART_MIXED);
assertCannotWrite(MULTIPART_RELATED);
this.converter.addSupportedMediaTypes(MULTIPART_MIXED, MULTIPART_RELATED);
this.converter.addSupportedMediaTypes(MULTIPART_RELATED);
assertCanWrite(MULTIPART_MIXED);
assertCanWrite(MULTIPART_RELATED);
}
@@ -286,6 +276,13 @@ public class FormHttpMessageConverterTests {
assertThat(this.converter.canRead(clazz, mediaType)).as(clazz.getSimpleName() + " : " + mediaType).isTrue();
}
private void asssertCannotReadMultipart() {
assertCannotRead(MULTIPART_ALL);
assertCannotRead(MULTIPART_FORM_DATA);
assertCannotRead(MULTIPART_MIXED);
assertCannotRead(MULTIPART_RELATED);
}
private void assertCannotRead(MediaType mediaType) {
assertCannotRead(MultiValueMap.class, mediaType);
}

View File

@@ -36,6 +36,7 @@ import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpHeaders.LOCATION;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import static org.springframework.http.MediaType.MULTIPART_MIXED;
/**
* @author Brian Clozel
@@ -43,7 +44,6 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
*/
public class AbstractMockWebServerTestCase {
protected static final MediaType MULTIPART_MIXED = new MediaType("multipart", "mixed");
protected static final MediaType MULTIPART_RELATED = new MediaType("multipart", "related");
protected static final MediaType textContentType =

View File

@@ -63,6 +63,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assume.assumeFalse;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.MULTIPART_MIXED;
/**
* Integration tests for {@link RestTemplate}.
@@ -274,15 +275,10 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
}
@Test
public void multipartMixed() {
addSupportedMediaTypeToFormHttpMessageConverter(MULTIPART_MIXED);
public void multipartMixed() throws Exception {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MULTIPART_MIXED);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(createMultipartParts(),
requestHeaders);
template.postForLocation(baseUrl + "/multipartMixed", requestEntity);
template.postForLocation(baseUrl + "/multipartMixed", new HttpEntity<>(createMultipartParts(), requestHeaders));
}
@Test
@@ -291,10 +287,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MULTIPART_RELATED);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(createMultipartParts(),
requestHeaders);
template.postForLocation(baseUrl + "/multipartRelated", requestEntity);
template.postForLocation(baseUrl + "/multipartRelated", new HttpEntity<>(createMultipartParts(), requestHeaders));
}
private MultiValueMap<String, Object> createMultipartParts() {