diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultMockServerSpec.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultMockServerSpec.java new file mode 100644 index 0000000000..83a6a813a5 --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultMockServerSpec.java @@ -0,0 +1,43 @@ +/* + * 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; + +import org.springframework.util.Assert; +import org.springframework.web.server.WebHandler; +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; + +/** + * + * @author Rossen Stoyanchev + * @since 5.0 + */ +class DefaultMockServerSpec extends AbstractMockServerSpec { + + private final WebHandler webHandler; + + + DefaultMockServerSpec(WebHandler webHandler) { + Assert.notNull(webHandler, "'WebHandler' is required"); + this.webHandler = webHandler; + } + + + @Override + protected WebHttpHandlerBuilder initHttpHandlerBuilder() { + return WebHttpHandlerBuilder.webHandler(this.webHandler); + } + +} diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/MockServerExchangeMutator.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/MockServerExchangeMutator.java index 77e708ee84..ce6b64e2c3 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/MockServerExchangeMutator.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/MockServerExchangeMutator.java @@ -33,9 +33,10 @@ import org.springframework.web.server.WebFilterChain; * transformations during requests from the {@code WebTestClient} to a mock * server -- i.e. when one of the following is in use: * * *

Example of registering a "global" transformation: 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 1483ab2bfd..aa3df2dfb3 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 @@ -34,7 +34,6 @@ 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; @@ -51,6 +50,7 @@ import org.springframework.web.reactive.function.server.HandlerStrategies; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer; import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebHandler; import org.springframework.web.util.UriBuilder; import org.springframework.web.util.UriBuilderFactory; @@ -130,7 +130,7 @@ public interface WebTestClient { // Static, factory methods /** - * Integration testing without a server targeting specific annotated, + * Integration testing with a "mock" 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. @@ -142,9 +142,9 @@ public interface WebTestClient { } /** - * Integration testing without a server with WebFlux infrastructure detected - * from an {@link ApplicationContext} such as {@code @EnableWebFlux} - * Java config and annotated controller Spring beans. + * Integration testing with a "mock" server with WebFlux infrastructure + * detected from an {@link ApplicationContext} such as + * {@code @EnableWebFlux} Java config and annotated controller Spring beans. * @param applicationContext the context * @return the {@link WebTestClient} builder * @see org.springframework.web.reactive.config.EnableWebFlux @@ -163,12 +163,12 @@ public interface WebTestClient { } /** - * Integration testing without a server targeting the given HttpHandler. - * @param httpHandler the handler to test + * Integration testing with a "mock" server targeting the given WebHandler. + * @param webHandler the handler to test * @return the {@link WebTestClient} builder */ - static Builder bindToHttpHandler(HttpHandler httpHandler) { - return new DefaultWebTestClientBuilder(httpHandler); + static MockServerSpec bindToWebHandler(WebHandler webHandler) { + return new DefaultMockServerSpec(webHandler); } /** 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/WebFilterTests.java similarity index 74% rename from spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/HttpHandlerTests.java rename to spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/WebFilterTests.java index 36e0d4a1c5..674ab295e5 100644 --- 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/WebFilterTests.java @@ -17,39 +17,35 @@ 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}. + * Tests for a {@link WebFilter}. * @author Rossen Stoyanchev */ -public class HttpHandlerTests { - +public class WebFilterTests { @Test public void testWebFilter() throws Exception { - WebFilter myFilter = (exchange, chain) -> { + WebFilter filter = (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 client = WebTestClient.bindToWebHandler(exchange -> Mono.empty()) + .webFilter(filter) + .build(); - WebTestClient.bindToHttpHandler(httpHandler).build() - .get().uri("/") + client.get().uri("/") .exchange() .expectStatus().isOk() .expectBody(String.class).isEqualTo("It works!");