Update WebTestClient builder

This commit is contained in:
Rossen Stoyanchev
2017-02-15 20:10:47 -05:00
parent f98ea11e5e
commit 285ba7d391
5 changed files with 65 additions and 158 deletions

View File

@@ -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();
}

View File

@@ -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<String, ?> 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();
}
}

View File

@@ -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;

View File

@@ -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<ViewResolverRegistry> 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<String, ?> defaultUriVariables);
/**
* Provide a pre-configured {@link UriBuilderFactory} instance. This is
* an alternative to and effectively overrides the following:
* <ul>
* <li>{@link #baseUrl(String)}
* <li>{@link #defaultUriVariables(Map)}.
* </ul>
* @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.
* <p>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.