Extract body extraction logic in w.r.f
This commit refactors the response body extraction logic into a separate function: BodyExtractor. Standard populators can be found in BodyExtractors.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.web.reactive.function;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class BodyExtractorsTests {
|
||||
|
||||
@Test
|
||||
public void toMono() throws Exception {
|
||||
BodyExtractor<Mono<String>> extractor = BodyExtractors.toMono(String.class);
|
||||
|
||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
DefaultDataBuffer dataBuffer =
|
||||
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
|
||||
Flux<DataBuffer> body = Flux.just(dataBuffer);
|
||||
|
||||
MockServerHttpRequest request = new MockServerHttpRequest();
|
||||
request.setBody(body);
|
||||
|
||||
Configuration configuration = Configuration.builder().build();
|
||||
|
||||
Mono<String> result = extractor.extract(request, configuration);
|
||||
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete()
|
||||
.assertValues("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toFlux() throws Exception {
|
||||
BodyExtractor<Flux<String>> extractor = BodyExtractors.toFlux(String.class);
|
||||
|
||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
DefaultDataBuffer dataBuffer =
|
||||
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
|
||||
Flux<DataBuffer> body = Flux.just(dataBuffer);
|
||||
|
||||
MockServerHttpRequest request = new MockServerHttpRequest();
|
||||
request.setBody(body);
|
||||
|
||||
Configuration configuration = Configuration.builder().build();
|
||||
|
||||
Flux<String> result = extractor.extract(request, configuration);
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete()
|
||||
.assertValues("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toFluxUnacceptable() throws Exception {
|
||||
BodyExtractor<Flux<String>> extractor = BodyExtractors.toFlux(String.class);
|
||||
|
||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
DefaultDataBuffer dataBuffer =
|
||||
factory.wrap(ByteBuffer.wrap("foo".getBytes(StandardCharsets.UTF_8)));
|
||||
Flux<DataBuffer> body = Flux.just(dataBuffer);
|
||||
|
||||
MockServerHttpRequest request = new MockServerHttpRequest();
|
||||
request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
request.setBody(body);
|
||||
|
||||
Configuration configuration = Configuration.empty().build();
|
||||
|
||||
Flux<String> result = extractor.extract(request, configuration);
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertError(UnsupportedMediaTypeStatusException.class);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.web.reactive.function;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.codec.ServerSentEvent;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.tests.TestSubscriber;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class BodyPopulatorsTests {
|
||||
|
||||
@Test
|
||||
public void ofObject() throws Exception {
|
||||
String body = "foo";
|
||||
BodyPopulator<String> populator = BodyPopulators.fromObject(body);
|
||||
|
||||
assertEquals(body, populator.supplier().get());
|
||||
|
||||
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer = populator.writer();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
Mono<Void> result = writer.apply(response, Configuration.builder().build());
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete();
|
||||
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(body.getBytes(UTF_8));
|
||||
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
|
||||
TestSubscriber.subscribe(response.getBody())
|
||||
.assertComplete()
|
||||
.assertValues(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofPublisher() throws Exception {
|
||||
Flux<String> body = Flux.just("foo");
|
||||
BodyPopulator<Flux<String>> populator = BodyPopulators.fromPublisher(body, String.class);
|
||||
|
||||
assertEquals(body, populator.supplier().get());
|
||||
|
||||
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer = populator.writer();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
Mono<Void> result = writer.apply(response, Configuration.builder().build());
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete();
|
||||
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap("foo".getBytes(UTF_8));
|
||||
DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);
|
||||
TestSubscriber.subscribe(response.getBody())
|
||||
.assertComplete()
|
||||
.assertValues(buffer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofResource() throws Exception {
|
||||
Resource body = new ClassPathResource("response.txt", getClass());
|
||||
BodyPopulator<Resource> populator = BodyPopulators.fromResource(body);
|
||||
|
||||
assertEquals(body, populator.supplier().get());
|
||||
|
||||
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer = populator.writer();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
Mono<Void> result = writer.apply(response, Configuration.builder().build());
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete();
|
||||
|
||||
byte[] expectedBytes = Files.readAllBytes(body.getFile().toPath());
|
||||
|
||||
TestSubscriber.subscribe(response.getBody())
|
||||
.assertComplete()
|
||||
.assertValuesWith(dataBuffer -> {
|
||||
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
|
||||
dataBuffer.read(resultBytes);
|
||||
assertArrayEquals(expectedBytes, resultBytes);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofServerSentEventFlux() throws Exception {
|
||||
ServerSentEvent<String> event = ServerSentEvent.builder("foo").build();
|
||||
Flux<ServerSentEvent<String>> body = Flux.just(event);
|
||||
BodyPopulator<Flux<ServerSentEvent<String>>> populator =
|
||||
BodyPopulators.fromServerSentEvents(body);
|
||||
|
||||
assertEquals(body, populator.supplier().get());
|
||||
|
||||
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer = populator.writer();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
Mono<Void> result = writer.apply(response, Configuration.builder().build());
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofServerSentEventClass() throws Exception {
|
||||
Flux<String> body = Flux.just("foo");
|
||||
BodyPopulator<Flux<String>> populator =
|
||||
BodyPopulators.fromServerSentEvents(body, String.class);
|
||||
|
||||
assertEquals(body, populator.supplier().get());
|
||||
|
||||
BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer = populator.writer();
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
Mono<Void> result = writer.apply(response, Configuration.builder().build());
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertComplete();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.web.reactive.function;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.web.reactive.function.support.RequestWrapper;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class BodyWrapperTests {
|
||||
|
||||
private Request.Body mockBody;
|
||||
|
||||
private RequestWrapper.BodyWrapper wrapper;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mockBody = mock(Request.Body.class);
|
||||
wrapper = new RequestWrapper.BodyWrapper(mockBody);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stream() throws Exception {
|
||||
DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
|
||||
Flux<DataBuffer> flux = Flux.just(buffer);
|
||||
when(mockBody.stream()).thenReturn(flux);
|
||||
|
||||
assertSame(flux, wrapper.stream());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertTo() throws Exception {
|
||||
Flux<String> flux = Flux.just("foo", "bar");
|
||||
when(mockBody.convertTo(String.class)).thenReturn(flux);
|
||||
|
||||
assertSame(flux, wrapper.convertTo(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertToMono() throws Exception {
|
||||
Mono<String> mono = Mono.just("foo");
|
||||
when(mockBody.convertToMono(String.class)).thenReturn(mono);
|
||||
|
||||
assertSame(mono, wrapper.convertToMono(String.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,8 +27,6 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -54,6 +52,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -66,6 +65,8 @@ public class DefaultRequestTests {
|
||||
|
||||
private ServerWebExchange mockExchange;
|
||||
|
||||
private Configuration mockConfiguration;
|
||||
|
||||
private DefaultRequest defaultRequest;
|
||||
|
||||
@Before
|
||||
@@ -76,8 +77,9 @@ public class DefaultRequestTests {
|
||||
mockExchange = mock(ServerWebExchange.class);
|
||||
when(mockExchange.getRequest()).thenReturn(mockRequest);
|
||||
when(mockExchange.getResponse()).thenReturn(mockResponse);
|
||||
mockConfiguration = mock(Configuration.class);
|
||||
|
||||
defaultRequest = new DefaultRequest(mockExchange);
|
||||
defaultRequest = new DefaultRequest(mockExchange, mockConfiguration);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,6 +114,14 @@ public class DefaultRequestTests {
|
||||
assertEquals(Optional.of("bar"), defaultRequest.queryParam("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathVariable() throws Exception {
|
||||
Map<String, String> pathVariables = Collections.singletonMap("foo", "bar");
|
||||
when(mockExchange.getAttribute(RoutingFunctions.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).thenReturn(Optional.of(pathVariables));
|
||||
|
||||
assertEquals(Optional.of("bar"), defaultRequest.pathVariable("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pathVariables() throws Exception {
|
||||
Map<String, String> pathVariables = Collections.singletonMap("foo", "bar");
|
||||
@@ -161,14 +171,9 @@ public class DefaultRequestTests {
|
||||
|
||||
Set<HttpMessageReader<?>> messageReaders = Collections
|
||||
.singleton(new DecoderHttpMessageReader<String>(new StringDecoder()));
|
||||
Configuration mockConfig = mock(Configuration.class);
|
||||
when(mockConfig.messageReaders()).thenReturn(messageReaders::stream);
|
||||
when(mockExchange.getAttribute(RoutingFunctions.CONFIGURATION_ATTRIBUTE))
|
||||
.thenReturn(Optional.of(mockConfig));
|
||||
when(mockConfiguration.messageReaders()).thenReturn(messageReaders::stream);
|
||||
|
||||
assertEquals(body, defaultRequest.body().stream());
|
||||
|
||||
Mono<String> resultMono = defaultRequest.body().convertToMono(String.class);
|
||||
Mono<String> resultMono = defaultRequest.body(toMono(String.class));
|
||||
assertEquals("foo", resultMono.block());
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,6 @@ import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.web.reactive.function.BodyPopulators.ofPublisher;
|
||||
import static org.springframework.web.reactive.function.RoutingFunctions.route;
|
||||
|
||||
/**
|
||||
@@ -156,13 +155,14 @@ public class DispatcherHandlerIntegrationTests extends AbstractHttpHandlerIntegr
|
||||
|
||||
public Response<Publisher<Person>> mono(Request request) {
|
||||
Person person = new Person("John");
|
||||
return Response.ok().body(ofPublisher(Mono.just(person), Person.class));
|
||||
return Response.ok().body(BodyPopulators.fromPublisher(Mono.just(person), Person.class));
|
||||
}
|
||||
|
||||
public Response<Publisher<Person>> flux(Request request) {
|
||||
Person person1 = new Person("John");
|
||||
Person person2 = new Person("Jane");
|
||||
return Response.ok().body(ofPublisher(Flux.just(person1, person2), Person.class));
|
||||
return Response.ok().body(
|
||||
BodyPopulators.fromPublisher(Flux.just(person1, person2), Person.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,10 +29,6 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalLong;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRange;
|
||||
@@ -44,7 +40,7 @@ import org.springframework.util.MultiValueMap;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MockRequest implements Request {
|
||||
public class MockRequest<T> implements Request {
|
||||
|
||||
private final HttpMethod method;
|
||||
|
||||
@@ -52,7 +48,7 @@ public class MockRequest implements Request {
|
||||
|
||||
private final MockHeaders headers;
|
||||
|
||||
private final MockBody body;
|
||||
private final T body;
|
||||
|
||||
private final Map<String, Object> attributes;
|
||||
|
||||
@@ -61,7 +57,7 @@ public class MockRequest implements Request {
|
||||
private final Map<String, String> pathVariables;
|
||||
|
||||
private MockRequest(HttpMethod method, URI uri,
|
||||
MockHeaders headers, MockBody body, Map<String, Object> attributes,
|
||||
MockHeaders headers, T body, Map<String, Object> attributes,
|
||||
MultiValueMap<String, String> queryParams,
|
||||
Map<String, String> pathVariables) {
|
||||
this.method = method;
|
||||
@@ -73,8 +69,8 @@ public class MockRequest implements Request {
|
||||
this.pathVariables = pathVariables;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new BuilderImpl();
|
||||
public static <T> Builder<T> builder() {
|
||||
return new BuilderImpl<T>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,15 +88,16 @@ public class MockRequest implements Request {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Body body() {
|
||||
return this.body;
|
||||
public <S> S body(BodyExtractor<S> extractor) {
|
||||
return (S) this.body;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> Optional<T> attribute(String name) {
|
||||
return Optional.ofNullable((T) this.attributes.get(name));
|
||||
public <S> Optional<S> attribute(String name) {
|
||||
return Optional.ofNullable((S) this.attributes.get(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -113,37 +110,35 @@ public class MockRequest implements Request {
|
||||
return Collections.unmodifiableMap(this.pathVariables);
|
||||
}
|
||||
|
||||
public interface Builder {
|
||||
public interface Builder<T> {
|
||||
|
||||
Builder method(HttpMethod method);
|
||||
Builder<T> method(HttpMethod method);
|
||||
|
||||
Builder uri(URI uri);
|
||||
Builder<T> uri(URI uri);
|
||||
|
||||
Builder header(String key, String value);
|
||||
Builder<T> header(String key, String value);
|
||||
|
||||
Builder headers(HttpHeaders headers);
|
||||
Builder<T> headers(HttpHeaders headers);
|
||||
|
||||
Builder attribute(String name, Object value);
|
||||
Builder<T> attribute(String name, Object value);
|
||||
|
||||
Builder attributes(Map<String, Object> attributes);
|
||||
Builder<T> attributes(Map<String, Object> attributes);
|
||||
|
||||
Builder queryParam(String key, String value);
|
||||
Builder<T> queryParam(String key, String value);
|
||||
|
||||
Builder queryParams(MultiValueMap<String, String> queryParams);
|
||||
Builder<T> queryParams(MultiValueMap<String, String> queryParams);
|
||||
|
||||
Builder pathVariable(String key, String value);
|
||||
Builder<T> pathVariable(String key, String value);
|
||||
|
||||
Builder pathVariables(Map<String, String> pathVariables);
|
||||
Builder<T> pathVariables(Map<String, String> pathVariables);
|
||||
|
||||
<T> MockRequest body(Flux<T> body);
|
||||
MockRequest<T> body(T body);
|
||||
|
||||
<T> MockRequest body(Mono<T> body);
|
||||
|
||||
MockRequest build();
|
||||
MockRequest<Void> build();
|
||||
|
||||
}
|
||||
|
||||
private static class BuilderImpl implements Builder {
|
||||
private static class BuilderImpl<T> implements Builder<T> {
|
||||
|
||||
private HttpMethod method = HttpMethod.GET;
|
||||
|
||||
@@ -151,6 +146,8 @@ public class MockRequest implements Request {
|
||||
|
||||
private MockHeaders headers = new MockHeaders(new HttpHeaders());
|
||||
|
||||
private T body;
|
||||
|
||||
private Map<String, Object> attributes = new LinkedHashMap<>();
|
||||
|
||||
private MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||
@@ -158,21 +155,21 @@ public class MockRequest implements Request {
|
||||
private Map<String, String> pathVariables = new LinkedHashMap<>();
|
||||
|
||||
@Override
|
||||
public Builder method(HttpMethod method) {
|
||||
public Builder<T> method(HttpMethod method) {
|
||||
Assert.notNull(method, "'method' must not be null");
|
||||
this.method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder uri(URI uri) {
|
||||
public Builder<T> uri(URI uri) {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
this.uri = uri;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder header(String key, String value) {
|
||||
public Builder<T> header(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.headers.header(key, value);
|
||||
@@ -180,14 +177,14 @@ public class MockRequest implements Request {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder headers(HttpHeaders headers) {
|
||||
public Builder<T> headers(HttpHeaders headers) {
|
||||
Assert.notNull(headers, "'headers' must not be null");
|
||||
this.headers = new MockHeaders(headers);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder attribute(String name, Object value) {
|
||||
public Builder<T> attribute(String name, Object value) {
|
||||
Assert.notNull(name, "'name' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.attributes.put(name, value);
|
||||
@@ -195,14 +192,14 @@ public class MockRequest implements Request {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder attributes(Map<String, Object> attributes) {
|
||||
public Builder<T> attributes(Map<String, Object> attributes) {
|
||||
Assert.notNull(attributes, "'attributes' must not be null");
|
||||
this.attributes = attributes;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder queryParam(String key, String value) {
|
||||
public Builder<T> queryParam(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.queryParams.add(key, value);
|
||||
@@ -210,14 +207,14 @@ public class MockRequest implements Request {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder queryParams(MultiValueMap<String, String> queryParams) {
|
||||
public Builder<T> queryParams(MultiValueMap<String, String> queryParams) {
|
||||
Assert.notNull(queryParams, "'queryParams' must not be null");
|
||||
this.queryParams = queryParams;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder pathVariable(String key, String value) {
|
||||
public Builder<T> pathVariable(String key, String value) {
|
||||
Assert.notNull(key, "'key' must not be null");
|
||||
Assert.notNull(value, "'value' must not be null");
|
||||
this.pathVariables.put(key, value);
|
||||
@@ -225,45 +222,25 @@ public class MockRequest implements Request {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder pathVariables(Map<String, String> pathVariables) {
|
||||
public Builder<T> pathVariables(Map<String, String> pathVariables) {
|
||||
Assert.notNull(pathVariables, "'pathVariables' must not be null");
|
||||
this.pathVariables = pathVariables;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> MockRequest body(Flux<T> flux) {
|
||||
MockBody body = new MockBody() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <S> Flux<S> convertTo(Class<? extends S> aClass) {
|
||||
return (Flux<S>) flux;
|
||||
}
|
||||
};
|
||||
return build(body);
|
||||
public MockRequest<T> body(T body) {
|
||||
this.body = body;
|
||||
return new MockRequest<T>(this.method, this.uri, this.headers, this.body,
|
||||
this.attributes, this.queryParams, this.pathVariables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> MockRequest body(Mono<T> mono) {
|
||||
MockBody body = new MockBody() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <S> Mono<S> convertToMono(Class<? extends S> aClass) {
|
||||
return (Mono<S>) mono;
|
||||
}
|
||||
};
|
||||
return build(body);
|
||||
public MockRequest<Void> build() {
|
||||
return new MockRequest<Void>(this.method, this.uri, this.headers, null,
|
||||
this.attributes, this.queryParams, this.pathVariables);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockRequest build() {
|
||||
return build(new MockBody());
|
||||
}
|
||||
|
||||
private MockRequest build(MockBody body) {
|
||||
return new MockRequest(this.method, this.uri, this.headers, body, this.attributes,
|
||||
this.queryParams, this.pathVariables);
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockHeaders implements Headers {
|
||||
@@ -339,24 +316,4 @@ public class MockRequest implements Request {
|
||||
}
|
||||
}
|
||||
|
||||
private static class MockBody implements Body {
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> stream() {
|
||||
return Flux.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Flux<T> convertTo(Class<? extends T> aClass) {
|
||||
return Flux.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Mono<T> convertToMono(Class<? extends T> aClass) {
|
||||
return Mono.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.web.reactive.function.BodyPopulators.ofPublisher;
|
||||
import static org.springframework.web.reactive.function.BodyExtractors.toMono;
|
||||
import static org.springframework.web.reactive.function.RequestPredicates.GET;
|
||||
import static org.springframework.web.reactive.function.RequestPredicates.POST;
|
||||
import static org.springframework.web.reactive.function.RoutingFunctions.route;
|
||||
@@ -98,18 +98,19 @@ public class PublisherHandlerFunctionIntegrationTests
|
||||
|
||||
public Response<Publisher<Person>> mono(Request request) {
|
||||
Person person = new Person("John");
|
||||
return Response.ok().body(ofPublisher(Mono.just(person), Person.class));
|
||||
return Response.ok().body(BodyPopulators.fromPublisher(Mono.just(person), Person.class));
|
||||
}
|
||||
|
||||
public Response<Publisher<Person>> postMono(Request request) {
|
||||
Mono<Person> personMono = request.body().convertToMono(Person.class);
|
||||
return Response.ok().body(ofPublisher(personMono, Person.class));
|
||||
Mono<Person> personMono = request.body(toMono(Person.class));
|
||||
return Response.ok().body(BodyPopulators.fromPublisher(personMono, Person.class));
|
||||
}
|
||||
|
||||
public Response<Publisher<Person>> flux(Request request) {
|
||||
Person person1 = new Person("John");
|
||||
Person person2 = new Person("Jane");
|
||||
return Response.ok().body(ofPublisher(Flux.just(person1, person2), Person.class));
|
||||
return Response.ok().body(
|
||||
BodyPopulators.fromPublisher(Flux.just(person1, person2), Person.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -85,14 +85,6 @@ public class RequestWrapperTests {
|
||||
assertSame(headers, wrapper.headers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void body() throws Exception {
|
||||
Request.Body body = mock(Request.Body.class);
|
||||
when(mockRequest.body()).thenReturn(body);
|
||||
|
||||
assertEquals(body, wrapper.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void attribute() throws Exception {
|
||||
String name = "foo";
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.web.reactive.function.BodyPopulators.ofObject;
|
||||
import static org.springframework.web.reactive.function.BodyPopulators.fromObject;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
@@ -48,7 +48,7 @@ public class RoutingFunctionTests {
|
||||
|
||||
@Test
|
||||
public void and() throws Exception {
|
||||
HandlerFunction<String> handlerFunction = request -> Response.ok().body(ofObject("42"));
|
||||
HandlerFunction<String> handlerFunction = request -> Response.ok().body(fromObject("42"));
|
||||
RoutingFunction<Void> routingFunction1 = request -> Optional.empty();
|
||||
RoutingFunction<String> routingFunction2 = request -> Optional.of(handlerFunction);
|
||||
|
||||
@@ -63,13 +63,13 @@ public class RoutingFunctionTests {
|
||||
|
||||
@Test
|
||||
public void filter() throws Exception {
|
||||
HandlerFunction<String> handlerFunction = request -> Response.ok().body(ofObject("42"));
|
||||
HandlerFunction<String> handlerFunction = request -> Response.ok().body(fromObject("42"));
|
||||
RoutingFunction<String> routingFunction = request -> Optional.of(handlerFunction);
|
||||
|
||||
FilterFunction<String, Integer> filterFunction = (request, next) -> {
|
||||
Response<String> response = next.handle(request);
|
||||
int i = Integer.parseInt(response.body());
|
||||
return Response.ok().body(ofObject(i));
|
||||
return Response.ok().body(fromObject(i));
|
||||
};
|
||||
RoutingFunction<Integer> result = routingFunction.filter(filterFunction);
|
||||
assertNotNull(result);
|
||||
|
||||
@@ -32,7 +32,6 @@ import org.springframework.web.client.reactive.WebClient;
|
||||
|
||||
import static org.springframework.web.client.reactive.ClientWebRequestBuilders.get;
|
||||
import static org.springframework.web.client.reactive.ResponseExtractors.bodyStream;
|
||||
import static org.springframework.web.reactive.function.BodyPopulators.ofServerSentEvents;
|
||||
import static org.springframework.web.reactive.function.RoutingFunctions.route;
|
||||
|
||||
/**
|
||||
@@ -112,13 +111,13 @@ public class SseHandlerFunctionIntegrationTests
|
||||
|
||||
public Response<Publisher<String>> string(Request request) {
|
||||
Flux<String> flux = Flux.interval(Duration.ofMillis(100)).map(l -> "foo " + l).take(2);
|
||||
return Response.ok().body(ofServerSentEvents(flux, String.class));
|
||||
return Response.ok().body(BodyPopulators.fromServerSentEvents(flux, String.class));
|
||||
}
|
||||
|
||||
public Response<Publisher<Person>> person(Request request) {
|
||||
Flux<Person> flux = Flux.interval(Duration.ofMillis(100))
|
||||
.map(l -> new Person("foo " + l)).take(2);
|
||||
return Response.ok().body(ofServerSentEvents(flux, Person.class));
|
||||
return Response.ok().body(BodyPopulators.fromServerSentEvents(flux, Person.class));
|
||||
}
|
||||
|
||||
public Response<Publisher<ServerSentEvent<String>>> sse(Request request) {
|
||||
@@ -128,7 +127,7 @@ public class SseHandlerFunctionIntegrationTests
|
||||
.comment("bar")
|
||||
.build()).take(2);
|
||||
|
||||
return Response.ok().body(ofServerSentEvents(flux));
|
||||
return Response.ok().body(BodyPopulators.fromServerSentEvents(flux));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user