diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 9c82524116..62ee054513 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -77,13 +77,13 @@ class DefaultWebTestClient implements WebTestClient { DefaultWebTestClient(WebClient.Builder webClientBuilder, ClientHttpConnector connector, - ExchangeMutatingWebFilter exchangeMutatingWebFilter, Duration timeout) { + ExchangeMutatingWebFilter filter, Duration timeout) { Assert.notNull(webClientBuilder, "WebClient.Builder is required"); this.wiretapConnector = new WiretapConnector(connector); this.webClient = webClientBuilder.clientConnector(this.wiretapConnector).build(); - this.exchangeMutatingWebFilter = exchangeMutatingWebFilter; + this.exchangeMutatingWebFilter = (filter != null ? filter : new ExchangeMutatingWebFilter()); this.timeout = (timeout != null ? timeout : Duration.ofSeconds(5)); } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java index c757a2c135..c8d28c6b81 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java @@ -35,6 +35,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.http.codec.ServerCodecConfigurer; +import org.springframework.http.server.reactive.HttpHandler; import org.springframework.util.MultiValueMap; import org.springframework.validation.Validator; import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder; @@ -140,7 +141,7 @@ public interface WebTestClient { // Static, factory methods /** - * Integration testing without a server, targeting specific annotated, + * Integration testing without a server targeting specific annotated, * WebFlux controllers. The default configuration is the same as for * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux} * but can also be further customized through the returned spec. @@ -152,7 +153,7 @@ public interface WebTestClient { } /** - * Integration testing without a server, with WebFlux infrastructure detected + * Integration testing without a server with WebFlux infrastructure detected * from an {@link ApplicationContext} such as {@code @EnableWebFlux} * Java config and annotated controller Spring beans. * @param applicationContext the context @@ -164,7 +165,7 @@ public interface WebTestClient { } /** - * Integration testing without a server, targeting WebFlux functional endpoints. + * Integration testing without a server targeting WebFlux functional endpoints. * @param routerFunction the RouterFunction to test * @return the {@link WebTestClient} builder */ @@ -172,6 +173,15 @@ public interface WebTestClient { return new RouterFunctionSpec(routerFunction); } + /** + * Integration testing without a server targeting the given HttpHandler. + * @param httpHandler the handler to test + * @return the {@link WebTestClient} builder + */ + static Builder bindToHttpHandler(HttpHandler httpHandler) { + return new DefaultWebTestClientBuilder(httpHandler, null); + } + /** * Complete end-to-end integration tests with actual requests to a running server. * @return the {@link WebTestClient} builder diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpHandlerTests.java new file mode 100644 index 0000000000..36e0d4a1c5 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpHandlerTests.java @@ -0,0 +1,58 @@ +/* + * 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.test.web.reactive.server.samples.bind; + +import java.nio.charset.StandardCharsets; +import java.util.Collections; + +import org.junit.Test; +import reactor.core.publisher.Mono; + +import org.springframework.core.io.buffer.DataBuffer; +import org.springframework.core.io.buffer.DefaultDataBufferFactory; +import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + +/** + * Bind to an {@link HttpHandler}. + * @author Rossen Stoyanchev + */ +public class HttpHandlerTests { + + + @Test + public void testWebFilter() throws Exception { + + WebFilter myFilter = (exchange, chain) -> { + DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer(); + buffer.write("It works!".getBytes(StandardCharsets.UTF_8)); + return exchange.getResponse().writeWith(Mono.just(buffer)); + }; + + HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(exchange -> Mono.empty()) + .filters(Collections.singletonList(myFilter)).build(); + + WebTestClient.bindToHttpHandler(httpHandler).build() + .get().uri("/") + .exchange() + .expectStatus().isOk() + .expectBody(String.class).isEqualTo("It works!"); + } + +}