Make WebClientCustomizer optional for WebClient
This commit removes the required dependency on `WebClientCustomizer` instances when creating `WebClient` beans, making it optional. Closes gh-12458
This commit is contained in:
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,49 +48,59 @@ 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);
|
||||||
|
WebClientCodecCustomizer clientCustomizer = context
|
||||||
.getBean(WebClientCodecCustomizer.class);
|
.getBean(WebClientCodecCustomizer.class);
|
||||||
builder.build();
|
builder.build();
|
||||||
assertThat(clientCustomizer).isNotNull();
|
assertThat(clientCustomizer).isNotNull();
|
||||||
verify(codecCustomizer).customize(any(CodecConfigurer.class));
|
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);
|
||||||
|
WebClientCustomizer customizer = context.getBean(WebClientCustomizer.class);
|
||||||
builder.build();
|
builder.build();
|
||||||
verify(customizer).customize(any(WebClient.Builder.class));
|
verify(customizer).customize(any(WebClient.Builder.class));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldGetPrototypeScopedBean() {
|
public void shouldGetPrototypeScopedBean() {
|
||||||
load(WebClientCustomizerConfig.class);
|
this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class)
|
||||||
|
.run((context) -> {
|
||||||
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
||||||
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
|
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
|
||||||
given(firstConnector.connect(any(), any(), any()))
|
given(firstConnector.connect(any(), any(), any()))
|
||||||
.willReturn(Mono.just(response));
|
.willReturn(Mono.just(response));
|
||||||
WebClient.Builder firstBuilder = this.context.getBean(WebClient.Builder.class);
|
WebClient.Builder firstBuilder = context.getBean(WebClient.Builder.class);
|
||||||
firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org");
|
firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org");
|
||||||
ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
|
ClientHttpConnector secondConnector = mock(ClientHttpConnector.class);
|
||||||
given(secondConnector.connect(any(), any(), any()))
|
given(secondConnector.connect(any(), any(), any()))
|
||||||
.willReturn(Mono.just(response));
|
.willReturn(Mono.just(response));
|
||||||
WebClient.Builder secondBuilder = this.context.getBean(WebClient.Builder.class);
|
WebClient.Builder secondBuilder = context.getBean(WebClient.Builder.class);
|
||||||
secondBuilder.clientConnector(secondConnector)
|
secondBuilder.clientConnector(secondConnector)
|
||||||
.baseUrl("http://second.example.org");
|
.baseUrl("http://second.example.org");
|
||||||
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
|
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
|
||||||
|
@ -100,23 +110,19 @@ public class WebClientAutoConfigurationTests {
|
||||||
eq(URI.create("http://first.example.org/foo")), any());
|
eq(URI.create("http://first.example.org/foo")), any());
|
||||||
verify(secondConnector).connect(eq(HttpMethod.GET),
|
verify(secondConnector).connect(eq(HttpMethod.GET),
|
||||||
eq(URI.create("http://second.example.org/foo")), any());
|
eq(URI.create("http://second.example.org/foo")), any());
|
||||||
WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class);
|
WebClientCustomizer customizer = context.getBean(WebClientCustomizer.class);
|
||||||
verify(customizer, times(1)).customize(any(WebClient.Builder.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)
|
||||||
|
.run((context) -> {
|
||||||
|
WebClient.Builder builder = context.getBean(WebClient.Builder.class);
|
||||||
assertThat(builder).isInstanceOf(MyWebClientBuilder.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…
Reference in New Issue