Polish
This commit is contained in:
parent
91d3e4402f
commit
e99b0038a0
|
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.http.codec.multipart;
|
package org.springframework.http.codec.multipart;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -26,19 +25,15 @@ import org.junit.Test;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.core.codec.CharSequenceEncoder;
|
|
||||||
import org.springframework.core.codec.StringDecoder;
|
import org.springframework.core.codec.StringDecoder;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
import org.springframework.http.client.MultipartBodyBuilder;
|
||||||
import org.springframework.http.codec.ResourceHttpMessageWriter;
|
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
|
||||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
@ -48,14 +43,12 @@ import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
public class MultipartHttpMessageWriterTests {
|
public class MultipartHttpMessageWriterTests {
|
||||||
|
|
||||||
private final MultipartHttpMessageWriter writer = new MultipartHttpMessageWriter(Arrays.asList(
|
private final MultipartHttpMessageWriter writer =
|
||||||
new EncoderHttpMessageWriter<>(CharSequenceEncoder.textPlainOnly()),
|
new MultipartHttpMessageWriter(ClientCodecConfigurer.create().getWriters());
|
||||||
new ResourceHttpMessageWriter(),
|
|
||||||
new EncoderHttpMessageWriter<>(new Jackson2JsonEncoder())
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -77,45 +70,30 @@ public class MultipartHttpMessageWriterTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void writeMultipart() throws Exception {
|
public void writeMultipart() throws Exception {
|
||||||
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
|
|
||||||
map.add("name 1", "value 1");
|
|
||||||
map.add("name 2", "value 2+1");
|
|
||||||
map.add("name 2", "value 2+2");
|
|
||||||
|
|
||||||
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
|
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
|
||||||
map.add("logo", logo);
|
|
||||||
|
|
||||||
// SPR-12108
|
|
||||||
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
|
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
|
||||||
@Override
|
@Override
|
||||||
public String getFilename() {
|
public String getFilename() {
|
||||||
|
// SPR-12108
|
||||||
return "Hall\u00F6le.jpg";
|
return "Hall\u00F6le.jpg";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
map.add("utf8", utf8);
|
|
||||||
|
|
||||||
HttpHeaders entityHeaders = new HttpHeaders();
|
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
|
||||||
entityHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
bodyBuilder.part("name 1", "value 1");
|
||||||
HttpEntity<Foo> entity = new HttpEntity<>(new Foo("bar"), entityHeaders);
|
bodyBuilder.part("name 2", "value 2+1");
|
||||||
map.add("json", entity);
|
bodyBuilder.part("name 2", "value 2+2");
|
||||||
|
bodyBuilder.part("logo", logo);
|
||||||
|
bodyBuilder.part("utf8", utf8);
|
||||||
|
bodyBuilder.part("json", new Foo("bar"), MediaType.APPLICATION_JSON_UTF8);
|
||||||
|
Mono<MultiValueMap<String, HttpEntity<?>>> publisher = Mono.just(bodyBuilder.build());
|
||||||
|
|
||||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||||
Map<String, Object> hints = Collections.emptyMap();
|
Map<String, Object> hints = Collections.emptyMap();
|
||||||
this.writer.write(Mono.just(map), null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
|
this.writer.write(publisher, null, MediaType.MULTIPART_FORM_DATA, response, hints).block(Duration.ofSeconds(5));
|
||||||
|
|
||||||
MediaType contentType = response.getHeaders().getContentType();
|
MultiValueMap<String, Part> requestParts = parse(response, hints);
|
||||||
assertNotNull("No boundary found", contentType.getParameter("boundary"));
|
|
||||||
|
|
||||||
// see if Synchronoss NIO Multipart can read what we wrote
|
|
||||||
SynchronossPartHttpMessageReader synchronossReader = new SynchronossPartHttpMessageReader();
|
|
||||||
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(synchronossReader);
|
|
||||||
|
|
||||||
MockServerHttpRequest request = MockServerHttpRequest.post("/foo")
|
|
||||||
.contentType(MediaType.parseMediaType(contentType.toString()))
|
|
||||||
.body(response.getBody());
|
|
||||||
|
|
||||||
ResolvableType elementType = ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
|
|
||||||
MultiValueMap<String, Part> requestParts = reader.readMono(elementType, request, hints).block();
|
|
||||||
assertEquals(5, requestParts.size());
|
assertEquals(5, requestParts.size());
|
||||||
|
|
||||||
Part part = requestParts.getFirst("name 1");
|
Part part = requestParts.getFirst("name 1");
|
||||||
|
|
@ -160,6 +138,28 @@ public class MultipartHttpMessageWriterTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<String, Object> hints) {
|
||||||
|
MediaType contentType = response.getHeaders().getContentType();
|
||||||
|
assertNotNull("No boundary found", contentType.getParameter("boundary"));
|
||||||
|
|
||||||
|
// see if Synchronoss NIO Multipart can read what we wrote
|
||||||
|
SynchronossPartHttpMessageReader synchronossReader = new SynchronossPartHttpMessageReader();
|
||||||
|
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(synchronossReader);
|
||||||
|
|
||||||
|
MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
||||||
|
.contentType(MediaType.parseMediaType(contentType.toString()))
|
||||||
|
.body(response.getBody());
|
||||||
|
|
||||||
|
ResolvableType elementType = ResolvableType.forClassWithGenerics(
|
||||||
|
MultiValueMap.class, String.class, Part.class);
|
||||||
|
|
||||||
|
MultiValueMap<String, Part> result = reader.readMono(elementType, request, hints)
|
||||||
|
.block(Duration.ofSeconds(5));
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private class Foo {
|
private class Foo {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue