Update list of support multipart media types

See gh-24582
This commit is contained in:
Rossen Stoyanchev
2020-03-03 15:37:31 +00:00
parent e706fcba1f
commit 2afae430eb
8 changed files with 109 additions and 37 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -68,17 +68,23 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA)).isTrue();
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.MULTIPART_MIXED)).isTrue();
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.MULTIPART_RELATED)).isTrue();
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA)).isFalse();
assertThat(this.writer.canWrite(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED)).isTrue();
}
@Test
public void writeMultipart() throws Exception {
public void writeMultipartFormData() throws Exception {
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
@Override
@@ -109,7 +115,8 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
Map<String, Object> hints = Collections.emptyMap();
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints)
.block(Duration.ofSeconds(5));
MultiValueMap<String, Part> requestParts = parse(hints);
assertThat(requestParts.size()).isEqualTo(7);
@@ -167,6 +174,33 @@ public class MultipartHttpMessageWriterTests extends AbstractLeakCheckingTests {
assertThat(value).isEqualTo("AaBbCc");
}
@Test // gh-24582
public void writeMultipartRelated() {
MediaType mediaType = MediaType.parseMediaType("multipart/related;type=foo");
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("name 1", "value 1");
bodyBuilder.part("name 2", "value 2");
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
Map<String, Object> hints = Collections.emptyMap();
this.writer.write(result, null, mediaType, this.response, hints)
.block(Duration.ofSeconds(5));
MediaType contentType = this.response.getHeaders().getContentType();
assertThat(contentType).isNotNull();
assertThat(contentType.isCompatibleWith(mediaType)).isTrue();
assertThat(contentType.getParameter("type")).isEqualTo("foo");
assertThat(contentType.getParameter("boundary")).isNotEmpty();
assertThat(contentType.getParameter("charset")).isEqualTo("UTF-8");
MultiValueMap<String, Part> requestParts = parse(hints);
assertThat(requestParts.size()).isEqualTo(2);
assertThat(requestParts.getFirst("name 1").name()).isEqualTo("name 1");
assertThat(requestParts.getFirst("name 2").name()).isEqualTo("name 2");
}
@SuppressWarnings("ConstantConditions")
private String decodeToString(Part part) {
return StringDecoder.textPlainOnly().decodeToMono(part.content(),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -68,11 +68,13 @@ public class SynchronossPartHttpMessageReaderTests extends AbstractLeakCheckingT
private static final ResolvableType PARTS_ELEMENT_TYPE =
forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
@Test
void canRead() {
assertThat(this.reader.canRead(
PARTS_ELEMENT_TYPE,
MediaType.MULTIPART_FORM_DATA)).isTrue();
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_FORM_DATA)).isTrue();
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_MIXED)).isTrue();
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, MediaType.MULTIPART_RELATED)).isTrue();
assertThat(this.reader.canRead(PARTS_ELEMENT_TYPE, null)).isTrue();
assertThat(this.reader.canRead(
forClassWithGenerics(MultiValueMap.class, String.class, Object.class),

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,7 +52,6 @@ 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
@@ -278,7 +277,7 @@ public class FormHttpMessageConverterTests {
}
private void asssertCannotReadMultipart() {
assertCannotRead(MULTIPART_ALL);
assertCannotRead(new MediaType("multipart", "*"));
assertCannotRead(MULTIPART_FORM_DATA);
assertCannotRead(MULTIPART_MIXED);
assertCannotRead(MULTIPART_RELATED);