Browse Source

Add WebTestClient.mutate()

This commit introduces a WebTestClient.mutate() method,
returning a WebTestClient.Builder.

Issue: SPR-15657
pull/1448/merge
Arjen Poutsma 8 years ago
parent
commit
a95cf07317
  1. 9
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java
  2. 18
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java
  3. 5
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
  4. 3
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java

9
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

@ -74,7 +74,8 @@ class DefaultWebTestClient implements WebTestClient { @@ -74,7 +74,8 @@ class DefaultWebTestClient implements WebTestClient {
private final AtomicLong requestIndex = new AtomicLong();
DefaultWebTestClient(WebClient.Builder clientBuilder, ClientHttpConnector connector, @Nullable Duration timeout) {
DefaultWebTestClient(WebClient.Builder clientBuilder, ClientHttpConnector connector,
@Nullable Duration timeout) {
Assert.notNull(clientBuilder, "WebClient.Builder is required");
this.wiretapConnector = new WiretapConnector(connector);
this.webClient = clientBuilder.clientConnector(this.wiretapConnector).build();
@ -122,6 +123,12 @@ class DefaultWebTestClient implements WebTestClient { @@ -122,6 +123,12 @@ class DefaultWebTestClient implements WebTestClient {
return toUriSpec(wc -> wc.method(HttpMethod.OPTIONS));
}
@Override
public Builder mutate() {
return new DefaultWebTestClientBuilder(this.wiretapConnector.getDelegate(),
this.webClient.mutate(), this.timeout);
}
private <S extends RequestHeadersSpec<?>> UriSpec<S> toUriSpec(
Function<WebClient, WebClient.UriSpec<WebClient.RequestBodySpec>> function) {

18
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClientBuilder.java

@ -24,6 +24,7 @@ import org.springframework.http.HttpHeaders; @@ -24,6 +24,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
@ -38,7 +39,7 @@ import org.springframework.web.util.UriBuilderFactory; @@ -38,7 +39,7 @@ import org.springframework.web.util.UriBuilderFactory;
*/
class DefaultWebTestClientBuilder implements WebTestClient.Builder {
private final WebClient.Builder webClientBuilder = WebClient.builder();
private final WebClient.Builder webClientBuilder;
private final ClientHttpConnector connector;
@ -49,12 +50,21 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder { @@ -49,12 +50,21 @@ class DefaultWebTestClientBuilder implements WebTestClient.Builder {
this(new ReactorClientHttpConnector());
}
DefaultWebTestClientBuilder(HttpHandler httpHandler) {
this(new HttpHandlerConnector(httpHandler));
}
DefaultWebTestClientBuilder(ClientHttpConnector connector) {
this.connector = connector;
this(connector, null, null);
}
DefaultWebTestClientBuilder(HttpHandler httpHandler) {
this.connector = new HttpHandlerConnector(httpHandler);
DefaultWebTestClientBuilder(ClientHttpConnector connector,
@Nullable WebClient.Builder webClientBuilder,
@Nullable Duration responseTimeout) {
this.connector = connector;
this.webClientBuilder = (webClientBuilder != null ? webClientBuilder : WebClient.builder());
this.responseTimeout = responseTimeout;
}

5
spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

@ -127,6 +127,11 @@ public interface WebTestClient { @@ -127,6 +127,11 @@ public interface WebTestClient {
UriSpec<RequestHeadersSpec<?>> options();
/**
* Return a builder to mutate properties of this web test client.
*/
Builder mutate();
// Static, factory methods
/**

3
spring-test/src/main/java/org/springframework/test/web/reactive/server/WiretapConnector.java

@ -50,6 +50,9 @@ class WiretapConnector implements ClientHttpConnector { @@ -50,6 +50,9 @@ class WiretapConnector implements ClientHttpConnector {
this.delegate = delegate;
}
public ClientHttpConnector getDelegate() {
return this.delegate;
}
@Override
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,

Loading…
Cancel
Save