From 285ba7d391a06ee058fafdc42746de2fc6990ef8 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 15 Feb 2017 20:10:47 -0500 Subject: [PATCH] Update WebTestClient builder --- .../server/DefaultControllerSpec.java | 4 +- .../reactive/server/DefaultWebClientSpec.java | 99 ------------------- .../server/DefaultWebTestClientBuilder.java | 47 ++++++++- .../web/reactive/server/WebTestClient.java | 71 ++++--------- .../reactive/server/samples/HeaderTests.java | 2 +- 5 files changed, 65 insertions(+), 158 deletions(-) delete mode 100644 spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebClientSpec.java diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java index 4156a791f2..fe46da2820 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java @@ -102,7 +102,7 @@ class DefaultControllerSpec implements WebTestClient.ControllerSpec { } @Override - public WebTestClient.WebClientSpec webClientSpec() { + public WebTestClient.Builder configureClient() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.controllers.forEach(controller -> registerBean(context, controller)); context.register(DelegatingWebFluxConfiguration.class); @@ -118,7 +118,7 @@ class DefaultControllerSpec implements WebTestClient.ControllerSpec { @Override public WebTestClient build() { - return webClientSpec().build(); + return configureClient().build(); } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebClientSpec.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebClientSpec.java deleted file mode 100644 index 835c3ac883..0000000000 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebClientSpec.java +++ /dev/null @@ -1,99 +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.test.web.reactive.server; - -import java.util.Map; - -import org.springframework.http.client.reactive.ClientHttpConnector; -import org.springframework.http.client.reactive.ReactorClientHttpConnector; -import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.web.reactive.function.client.ExchangeStrategies; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.util.UriBuilderFactory; - -/** - * Default implementation of {@link WebTestClient.WebClientSpec}. - * - * @author Rossen Stoyanchev - * @since 5.0 - */ -class DefaultWebClientSpec implements WebTestClient.WebClientSpec { - - private final WebClient.Builder builder = WebClient.builder(); - - private final ClientHttpConnector connector; - - - public DefaultWebClientSpec() { - this(new ReactorClientHttpConnector()); - } - - public DefaultWebClientSpec(HttpHandler httpHandler) { - this(new HttpHandlerConnector(httpHandler)); - } - - public DefaultWebClientSpec(ClientHttpConnector connector) { - this.connector = connector; - } - - - @Override - public WebTestClient.WebClientSpec baseUrl(String baseUrl) { - this.builder.baseUrl(baseUrl); - return this; - } - - @Override - public WebTestClient.WebClientSpec defaultUriVariables(Map defaultUriVariables) { - this.builder.defaultUriVariables(defaultUriVariables); - return this; - } - - @Override - public WebTestClient.WebClientSpec uriBuilderFactory(UriBuilderFactory uriBuilderFactory) { - this.builder.uriBuilderFactory(uriBuilderFactory); - return this; - } - - @Override - public WebTestClient.WebClientSpec defaultHeader(String headerName, String... headerValues) { - this.builder.defaultHeader(headerName, headerValues); - return this; - } - - @Override - public WebTestClient.WebClientSpec defaultCookie(String cookieName, String... cookieValues) { - this.builder.defaultCookie(cookieName, cookieValues); - return this; - } - - @Override - public WebTestClient.WebClientSpec exchangeStrategies(ExchangeStrategies strategies) { - this.builder.exchangeStrategies(strategies); - return this; - } - - @Override - public WebTestClient.Builder builder() { - return new DefaultWebTestClientBuilder(this.builder, this.connector); - } - - @Override - public WebTestClient build() { - return builder().build(); - } - -} diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java index 2c3389659b..dc2db6bc17 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java @@ -18,7 +18,11 @@ package org.springframework.test.web.reactive.server; import java.time.Duration; import org.springframework.http.client.reactive.ClientHttpConnector; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriBuilderFactory; /** * Default implementation of {@link WebTestClient.Builder}. @@ -28,19 +32,56 @@ import org.springframework.web.reactive.function.client.WebClient; */ class DefaultWebTestClientBuilder implements WebTestClient.Builder { - private final WebClient.Builder webClientBuilder; + private final WebClient.Builder webClientBuilder = WebClient.builder(); private final ClientHttpConnector connector; private Duration responseTimeout; - public DefaultWebTestClientBuilder(WebClient.Builder builder, ClientHttpConnector connector) { - this.webClientBuilder = builder; + public DefaultWebTestClientBuilder() { + this(new ReactorClientHttpConnector()); + } + + public DefaultWebTestClientBuilder(HttpHandler httpHandler) { + this(new HttpHandlerConnector(httpHandler)); + } + + public DefaultWebTestClientBuilder(ClientHttpConnector connector) { this.connector = connector; } + @Override + public WebTestClient.Builder baseUrl(String baseUrl) { + this.webClientBuilder.baseUrl(baseUrl); + return this; + } + + @Override + public WebTestClient.Builder uriBuilderFactory(UriBuilderFactory uriBuilderFactory) { + this.webClientBuilder.uriBuilderFactory(uriBuilderFactory); + return this; + } + + @Override + public WebTestClient.Builder defaultHeader(String headerName, String... headerValues) { + this.webClientBuilder.defaultHeader(headerName, headerValues); + return this; + } + + @Override + public WebTestClient.Builder defaultCookie(String cookieName, String... cookieValues) { + this.webClientBuilder.defaultCookie(cookieName, cookieValues); + return this; + } + + @Override + public WebTestClient.Builder exchangeStrategies(ExchangeStrategies strategies) { + this.webClientBuilder.exchangeStrategies(strategies); + return this; + } + @Override public WebTestClient.Builder responseTimeout(Duration timeout) { this.responseTimeout = timeout; 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 123e1e018d..5b0d283e7e 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 @@ -136,7 +136,7 @@ public interface WebTestClient { * {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux} * but can also be further customized through the returned spec. * @param controllers the controllers to test - * @return spec for controller configuration and test client builder + * @return spec for setting up controller configuration */ static ControllerSpec bindToController(Object... controllers) { return new DefaultControllerSpec(controllers); @@ -150,9 +150,9 @@ public interface WebTestClient { * @return the {@link WebTestClient} builder * @see org.springframework.web.reactive.config.EnableWebFlux */ - static WebClientSpec bindToApplicationContext(ApplicationContext applicationContext) { + static Builder bindToApplicationContext(ApplicationContext applicationContext) { HttpHandler httpHandler = WebHttpHandlerBuilder.applicationContext(applicationContext).build(); - return new DefaultWebClientSpec(httpHandler); + return new DefaultWebTestClientBuilder(httpHandler); } /** @@ -160,17 +160,17 @@ public interface WebTestClient { * @param routerFunction the RouterFunction to test * @return the {@link WebTestClient} builder */ - static WebClientSpec bindToRouterFunction(RouterFunction routerFunction) { + static Builder bindToRouterFunction(RouterFunction routerFunction) { HttpWebHandlerAdapter httpHandler = RouterFunctions.toHttpHandler(routerFunction); - return new DefaultWebClientSpec(httpHandler); + return new DefaultWebTestClientBuilder(httpHandler); } /** * Complete end-to-end integration tests with actual requests to a running server. * @return the {@link WebTestClient} builder */ - static WebClientSpec bindToServer() { - return new DefaultWebClientSpec(); + static Builder bindToServer() { + return new DefaultWebTestClientBuilder(); } @@ -229,12 +229,12 @@ public interface WebTestClient { ControllerSpec viewResolvers(Consumer consumer); /** - * Proceed to configure the {@link WebClient} to test with. + * Proceed to configure and build the test client. */ - WebClientSpec webClientSpec(); + Builder configureClient(); /** - * Shortcut to build the {@link WebTestClient}. + * Shortcut to build the test client. */ WebTestClient build(); @@ -244,76 +244,41 @@ public interface WebTestClient { * Steps for customizing the {@link WebClient} used to test with * internally delegating to a {@link WebClient.Builder}. */ - interface WebClientSpec { + interface Builder { /** * Configure a base URI as described in * {@link org.springframework.web.reactive.function.client.WebClient#create(String) * WebClient.create(String)}. - * @see #defaultUriVariables(Map) - * @see #uriBuilderFactory(UriBuilderFactory) */ - WebClientSpec baseUrl(String baseUrl); + Builder baseUrl(String baseUrl); /** - * Configure default URI variable values that will be used when expanding - * URI templates using a {@link Map}. - * @param defaultUriVariables the default values to use - * @see #baseUrl(String) - * @see #uriBuilderFactory(UriBuilderFactory) + * Provide a pre-configured {@link UriBuilderFactory} instance as an + * alternative to and effectively overriding {@link #baseUrl(String)}. */ - WebClientSpec defaultUriVariables(Map defaultUriVariables); - - /** - * Provide a pre-configured {@link UriBuilderFactory} instance. This is - * an alternative to and effectively overrides the following: - *
    - *
  • {@link #baseUrl(String)} - *
  • {@link #defaultUriVariables(Map)}. - *
- * @param uriBuilderFactory the URI builder factory to use - * @see #baseUrl(String) - * @see #defaultUriVariables(Map) - */ - WebClientSpec uriBuilderFactory(UriBuilderFactory uriBuilderFactory); + Builder uriBuilderFactory(UriBuilderFactory uriBuilderFactory); /** * Add the given header to all requests that haven't added it. * @param headerName the header name * @param headerValues the header values */ - WebClientSpec defaultHeader(String headerName, String... headerValues); + Builder defaultHeader(String headerName, String... headerValues); /** * Add the given header to all requests that haven't added it. * @param cookieName the cookie name * @param cookieValues the cookie values */ - WebClientSpec defaultCookie(String cookieName, String... cookieValues); + Builder defaultCookie(String cookieName, String... cookieValues); /** * Configure the {@link ExchangeStrategies} to use. *

By default {@link ExchangeStrategies#withDefaults()} is used. * @param strategies the strategies to use */ - WebClientSpec exchangeStrategies(ExchangeStrategies strategies); - - /** - * Proceed to building the {@link WebTestClient}. - */ - Builder builder(); - - /** - * Shortcut to build the {@link WebTestClient}. - */ - WebTestClient build(); - - } - - /** - * Build steps to create a {@link WebTestClient}. - */ - interface Builder { + Builder exchangeStrategies(ExchangeStrategies strategies); /** * Max amount of time to wait for responses. diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java index 8ed6824b4a..efe554925e 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/HeaderTests.java @@ -40,7 +40,7 @@ public class HeaderTests { public void setUp() throws Exception { this.client = WebTestClient .bindToController(new TestController()) - .webClientSpec().baseUrl("/header") + .configureClient().baseUrl("/header") .build(); }