mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Support custom WebTestClient timeouts
Update @AutoConfigureWebTestClient to support a custom `timeout` option. See gh-10555
This commit is contained in:
parent
ad92af1c99
commit
a9263998a1
@ -22,8 +22,10 @@ import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
|
||||
/**
|
||||
@ -38,6 +40,14 @@ import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
@Documented
|
||||
@Inherited
|
||||
@ImportAutoConfiguration
|
||||
@PropertyMapping("spring.test.webtestclient")
|
||||
public @interface AutoConfigureWebTestClient {
|
||||
|
||||
/**
|
||||
* The timeout duration for the client (in any format handled by
|
||||
* {@link Duration#parse(CharSequence)}).
|
||||
* @return the web client timeout
|
||||
*/
|
||||
String timeout() default "";
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.web.reactive;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@ -23,12 +24,14 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.web.codec.CodecCustomizer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.codec.ClientCodecConfigurer;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.Builder;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.reactive.function.client.ExchangeStrategies;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
@ -49,10 +52,18 @@ public class WebTestClientAutoConfiguration {
|
||||
public WebTestClient webTestClient(ApplicationContext applicationContext) {
|
||||
WebTestClient.Builder builder = WebTestClient
|
||||
.bindToApplicationContext(applicationContext).configureClient();
|
||||
customizeWebTestClient(builder, applicationContext);
|
||||
customizeWebTestClientCodecs(builder, applicationContext);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private void customizeWebTestClient(Builder builder,
|
||||
ApplicationContext applicationContext) {
|
||||
Binder.get(applicationContext.getEnvironment())
|
||||
.bind("spring.test.webtestclient.timeout", Duration.class)
|
||||
.ifBound(builder::responseTimeout);
|
||||
}
|
||||
|
||||
private void customizeWebTestClientCodecs(WebTestClient.Builder builder,
|
||||
ApplicationContext applicationContext) {
|
||||
Collection<CodecCustomizer> customizers = applicationContext
|
||||
|
@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.boot.test.autoconfigure.web.reactive;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -24,7 +28,10 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.http.codec.CodecConfigurer;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
|
||||
@ -58,12 +65,29 @@ public class WebTestClientAutoConfigurationTests {
|
||||
verify(codecCustomizer).customize(any(CodecConfigurer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCustomizeTimeout() throws Exception {
|
||||
PropertySource<?> propertySource = new MapPropertySource("test", Collections
|
||||
.singletonMap("spring.test.webtestclient.timeout", (Object) "PT15M"));
|
||||
load(propertySource, BaseConfiguration.class);
|
||||
WebTestClient webTestClient = this.context.getBean(WebTestClient.class);
|
||||
Object duration = ReflectionTestUtils.getField(webTestClient, "timeout");
|
||||
assertThat(duration).isEqualTo(Duration.of(15, ChronoUnit.MINUTES));
|
||||
}
|
||||
|
||||
private void load(Class<?>... config) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(config);
|
||||
ctx.register(WebTestClientAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
load(null, config);
|
||||
}
|
||||
|
||||
private void load(PropertySource<?> propertySource, Class<?>... config) {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
if (propertySource != null) {
|
||||
context.getEnvironment().getPropertySources().addFirst(propertySource);
|
||||
}
|
||||
context.register(config);
|
||||
context.register(WebTestClientAutoConfiguration.class);
|
||||
context.refresh();
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ -73,6 +97,7 @@ public class WebTestClientAutoConfigurationTests {
|
||||
public WebHandler webHandler() {
|
||||
return mock(WebHandler.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
Loading…
Reference in New Issue
Block a user