WebFlux multpart support polish + minor refactoring

This commit is contained in:
Rossen Stoyanchev
2017-05-01 18:00:08 -04:00
parent ea85431ac5
commit 2390748fd7
11 changed files with 353 additions and 399 deletions

View File

@@ -1,69 +0,0 @@
/*
* Copyright 2002-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec.multipart;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author Sebastien Deleuze
*/
public class MultipartHttpMessageReaderTests {
private MultipartHttpMessageReader reader;
@Before
public void setUp() throws Exception {
this.reader = (elementType, message, hints) -> {
throw new UnsupportedOperationException();
};
}
@Test
public void canRead() {
assertTrue(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(Map.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class),
MediaType.APPLICATION_FORM_URLENCODED));
}
}

View File

@@ -25,6 +25,8 @@ import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import reactor.core.publisher.Mono;
@@ -57,27 +59,27 @@ public class MultipartHttpMessageWriterTests {
@Test
public void canWrite() {
assertTrue(this.writer.canWrite(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
assertTrue(this.writer.canWrite(forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA));
assertTrue(this.writer.canWrite(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
assertTrue(this.writer.canWrite(forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.writer.canWrite(ResolvableType.forClassWithGenerics(MultiValueMap.class, Object.class, Object.class),
assertFalse(this.writer.canWrite(forClassWithGenerics(Map.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.writer.canWrite(ResolvableType.forClassWithGenerics(Map.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.writer.canWrite(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
assertFalse(this.writer.canWrite(forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.APPLICATION_FORM_URLENCODED));
}
@Test
public void writeMultipart() throws Exception {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("name 1", "value 1");
parts.add("name 2", "value 2+1");
parts.add("name 2", "value 2+2");
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");
parts.add("logo", logo);
map.add("logo", logo);
// SPR-12108
Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {
@@ -86,28 +88,28 @@ public class MultipartHttpMessageWriterTests {
return "Hall\u00F6le.jpg";
}
};
parts.add("utf8", utf8);
map.add("utf8", utf8);
Foo foo = new Foo("bar");
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Foo> entity = new HttpEntity<>(foo, entityHeaders);
parts.add("json", entity);
HttpEntity<Foo> entity = new HttpEntity<>(new Foo("bar"), entityHeaders);
map.add("json", entity);
MockServerHttpResponse response = new MockServerHttpResponse();
this.writer.write(Mono.just(parts), null, MediaType.MULTIPART_FORM_DATA, response, Collections.emptyMap()).block();
Map<String, Object> hints = Collections.emptyMap();
this.writer.write(Mono.just(map), null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
final MediaType contentType = response.getHeaders().getContentType();
assertNotNull("No boundary found", contentType.getParameter("boundary"));
// see if NIO Multipart can read what we wrote
MultipartHttpMessageReader multipartReader = new SynchronossMultipartHttpMessageReader();
// see if Synchronoss NIO Multipart can read what we wrote
SynchronossMultipartHttpMessageReader reader = new SynchronossMultipartHttpMessageReader();
MockServerHttpRequest request = MockServerHttpRequest.post("/foo")
.header(CONTENT_TYPE, contentType.toString())
.body(response.getBody());
MultiValueMap<String, Part> requestParts = multipartReader.
readMono(MultipartHttpMessageReader.MULTIPART_VALUE_TYPE, request, Collections.emptyMap()).block();
ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
MultiValueMap<String, Part> requestParts = reader.readMono(elementType, request, hints).block();
assertEquals(5, requestParts.size());

View File

@@ -17,46 +17,75 @@
package org.springframework.http.codec.multipart;
import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import static org.springframework.http.codec.multipart.MultipartHttpMessageReader.*;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static java.util.Collections.emptyMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.ResolvableType.forClassWithGenerics;
import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
/**
* @author Sebastien Deleuze
*/
public class SynchronossMultipartHttpMessageReaderTests {
private final HttpMessageReader<MultiValueMap<String, Part>> reader =
new SynchronossMultipartHttpMessageReader();
@Test
public void canRead() {
assertTrue(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(Map.class, String.class, String.class),
MediaType.MULTIPART_FORM_DATA));
assertFalse(this.reader.canRead(
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class),
MediaType.APPLICATION_FORM_URLENCODED));
}
@Test
public void resolveParts() throws IOException {
ServerHttpRequest request = generateMultipartRequest();
MultipartHttpMessageReader multipartReader = new SynchronossMultipartHttpMessageReader();
MultiValueMap<String, Part> parts = multipartReader.readMono(MULTIPART_VALUE_TYPE, request, emptyMap()).block();
ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block();
assertEquals(2, parts.size());
assertTrue(parts.containsKey("fooPart"));
@@ -65,10 +94,7 @@ public class SynchronossMultipartHttpMessageReaderTests {
Optional<String> filename = part.getFilename();
assertTrue(filename.isPresent());
assertEquals("foo.txt", filename.get());
DataBuffer buffer = part
.getContent()
.reduce((s1, s2) -> s1.write(s2))
.block();
DataBuffer buffer = part.getContent().reduce(DataBuffer::write).block();
assertEquals(12, buffer.readableByteCount());
byte[] byteContent = new byte[12];
buffer.read(byteContent);
@@ -85,9 +111,8 @@ public class SynchronossMultipartHttpMessageReaderTests {
@Test
public void bodyError() {
ServerHttpRequest request = generateErrorMultipartRequest();
MultipartHttpMessageReader multipartReader = new SynchronossMultipartHttpMessageReader();
StepVerifier.create(multipartReader.readMono(MULTIPART_VALUE_TYPE, request, emptyMap()))
.verifyError();
ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class);
StepVerifier.create(this.reader.readMono(elementType, request, emptyMap())).verifyError();
}
private ServerHttpRequest generateMultipartRequest() throws IOException {
@@ -103,21 +128,18 @@ public class SynchronossMultipartHttpMessageReaderTests {
parts.add("barPart", barPart);
converter.write(parts, MULTIPART_FORM_DATA, outputMessage);
byte[] content = outputMessage.getBodyAsBytes();
MockServerHttpRequest request = MockServerHttpRequest
return MockServerHttpRequest
.post("/foo")
.header(CONTENT_TYPE, outputMessage.getHeaders().getContentType().toString())
.header(CONTENT_LENGTH, String.valueOf(content.length))
.body(new String(content));
return request;
}
private ServerHttpRequest generateErrorMultipartRequest() {
DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
MockServerHttpRequest request = MockServerHttpRequest
return MockServerHttpRequest
.post("/foo")
.header(CONTENT_TYPE, MULTIPART_FORM_DATA.toString())
.body(Flux.just(bufferFactory.wrap("invalid content".getBytes())));
return request;
.body(Flux.just(new DefaultDataBufferFactory().wrap("invalid content".getBytes())));
}
}