Make WebClientCustomizer optional for WebClient

This commit removes the required dependency on `WebClientCustomizer`
instances when creating `WebClient` beans, making it optional.

Closes gh-12458
pull/12492/merge
Brian Clozel 7 years ago
parent d1e51d7f98
commit 57147241cb

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -69,7 +69,7 @@ public class WebClientAutoConfiguration {
@Bean @Bean
@Scope("prototype") @Scope("prototype")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WebClient.Builder webClientBuilder(List<WebClientCustomizer> customizers) { public WebClient.Builder webClientBuilder() {
return this.webClientBuilder.clone(); return this.webClientBuilder.clone();
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.web.reactive.function.client;
import java.net.URI; import java.net.URI;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
@ -48,75 +48,81 @@ import static org.mockito.Mockito.verify;
*/ */
public class WebClientAutoConfigurationTests { public class WebClientAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class));
@Test
public void shouldCreateBuilder() {
this.contextRunner
.run((context) -> {
WebClient.Builder builder = context.getBean(WebClient.Builder.class);
WebClient webClient = builder.build();
assertThat(webClient).isNotNull();
});
@After
public void close() {
if (this.context != null) {
this.context.close();
}
} }
@Test @Test
public void shouldCustomizeClientCodecs() { public void shouldCustomizeClientCodecs() {
load(CodecConfiguration.class); this.contextRunner.withUserConfiguration(CodecConfiguration.class)
WebClient.Builder builder = this.context.getBean(WebClient.Builder.class); .run((context) -> {
CodecCustomizer codecCustomizer = this.context.getBean(CodecCustomizer.class); WebClient.Builder builder = context.getBean(WebClient.Builder.class);
WebClientCodecCustomizer clientCustomizer = this.context CodecCustomizer codecCustomizer = context.getBean(CodecCustomizer.class);
.getBean(WebClientCodecCustomizer.class); WebClientCodecCustomizer clientCustomizer = context
builder.build(); .getBean(WebClientCodecCustomizer.class);
assertThat(clientCustomizer).isNotNull(); builder.build();
verify(codecCustomizer).customize(any(CodecConfigurer.class)); assertThat(clientCustomizer).isNotNull();
verify(codecCustomizer).customize(any(CodecConfigurer.class));
});
} }
@Test @Test
public void webClientShouldApplyCustomizers() { public void webClientShouldApplyCustomizers() {
load(WebClientCustomizerConfig.class); this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class)
WebClient.Builder builder = this.context.getBean(WebClient.Builder.class); .run((context) -> {
WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class); WebClient.Builder builder = context.getBean(WebClient.Builder.class);
builder.build(); WebClientCustomizer customizer = context.getBean(WebClientCustomizer.class);
verify(customizer).customize(any(WebClient.Builder.class)); builder.build();
verify(customizer).customize(any(WebClient.Builder.class));
});
} }
@Test @Test
public void shouldGetPrototypeScopedBean() { public void shouldGetPrototypeScopedBean() {
load(WebClientCustomizerConfig.class); this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class)
ClientHttpResponse response = mock(ClientHttpResponse.class); .run((context) -> {
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class); ClientHttpResponse response = mock(ClientHttpResponse.class);
given(firstConnector.connect(any(), any(), any())) ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
.willReturn(Mono.just(response)); given(firstConnector.connect(any(), any(), any()))
WebClient.Builder firstBuilder = this.context.getBean(WebClient.Builder.class); .willReturn(Mono.just(response));
firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org"); WebClient.Builder firstBuilder = context.getBean(WebClient.Builder.class);
ClientHttpConnector secondConnector = mock(ClientHttpConnector.class); firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org");
given(secondConnector.connect(any(), any(), any())) ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
.willReturn(Mono.just(response)); given(secondConnector.connect(any(), any(), any()))
WebClient.Builder secondBuilder = this.context.getBean(WebClient.Builder.class); .willReturn(Mono.just(response));
secondBuilder.clientConnector(secondConnector) WebClient.Builder secondBuilder = context.getBean(WebClient.Builder.class);
.baseUrl("http://second.example.org"); secondBuilder.clientConnector(secondConnector)
assertThat(firstBuilder).isNotEqualTo(secondBuilder); .baseUrl("http://second.example.org");
firstBuilder.build().get().uri("/foo").exchange().block(); assertThat(firstBuilder).isNotEqualTo(secondBuilder);
secondBuilder.build().get().uri("/foo").exchange().block(); firstBuilder.build().get().uri("/foo").exchange().block();
verify(firstConnector).connect(eq(HttpMethod.GET), secondBuilder.build().get().uri("/foo").exchange().block();
eq(URI.create("http://first.example.org/foo")), any()); verify(firstConnector).connect(eq(HttpMethod.GET),
verify(secondConnector).connect(eq(HttpMethod.GET), eq(URI.create("http://first.example.org/foo")), any());
eq(URI.create("http://second.example.org/foo")), any()); verify(secondConnector).connect(eq(HttpMethod.GET),
WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class); eq(URI.create("http://second.example.org/foo")), any());
verify(customizer, times(1)).customize(any(WebClient.Builder.class)); WebClientCustomizer customizer = context.getBean(WebClientCustomizer.class);
verify(customizer, times(1)).customize(any(WebClient.Builder.class));
});
} }
@Test @Test
public void shouldNotCreateClientBuilderIfAlreadyPresent() { public void shouldNotCreateClientBuilderIfAlreadyPresent() {
load(WebClientCustomizerConfig.class, CustomWebClientBuilderConfig.class); this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class,
WebClient.Builder builder = this.context.getBean(WebClient.Builder.class); CustomWebClientBuilderConfig.class)
assertThat(builder).isInstanceOf(MyWebClientBuilder.class); .run((context) -> {
} WebClient.Builder builder = context.getBean(WebClient.Builder.class);
assertThat(builder).isInstanceOf(MyWebClientBuilder.class);
private void load(Class<?>... config) { });
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(config);
ctx.register(WebClientAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
} }
@Configuration @Configuration

Loading…
Cancel
Save