Add apply method to WebClient.Builder

This commit introduces an apply method to `WebClient.Builder`, allowing
users to make multiple changes to the builder in one consumer.

Issue: SPR-15743
This commit is contained in:
Arjen Poutsma 2017-07-12 11:05:03 +02:00
parent 9ac71afbda
commit d6c102d1b8
3 changed files with 32 additions and 5 deletions

View File

@ -256,4 +256,12 @@ class DefaultWebClientBuilder implements WebClient.Builder {
return new DefaultWebClientBuilder(this);
}
@Override
public WebClient.Builder apply(Consumer<WebClient.Builder> builderConsumer) {
Assert.notNull(builderConsumer, "'builderConsumer' must not be null");
builderConsumer.accept(this);
return this;
}
}

View File

@ -301,16 +301,22 @@ public interface WebClient {
*/
Builder exchangeFunction(ExchangeFunction exchangeFunction);
/**
* Builder the {@link WebClient} instance.
*/
WebClient build();
/**
* Clone this {@code WebClient.Builder}
*/
Builder clone();
/**
* Shortcut for pre-packaged customizations to WebTest builder.
* @param builderConsumer the consumer to apply
*/
Builder apply(Consumer<Builder> builderConsumer);
/**
* Builder the {@link WebClient} instance.
*/
WebClient build();
}

View File

@ -157,6 +157,19 @@ public class DefaultWebClientTests {
client.get().uri("/path").attribute("foo", "bar").exchange();
}
@Test
public void apply() {
WebClient client = builder()
.apply(builder -> builder.defaultHeader("Accept", "application/json").defaultCookie("id", "123"))
.build();
client.get().uri("/path").exchange();
ClientRequest request = verifyExchange();
assertEquals("application/json", request.headers().getFirst("Accept"));
assertEquals("123", request.cookies().getFirst("id"));
verifyNoMoreInteractions(this.exchangeFunction);
}
private WebClient.Builder builder() {