|
1 | 1 | /* |
2 | | - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.test.autoconfigure.web.reactive; |
18 | 18 |
|
19 | | -import java.time.Duration; |
20 | 19 | import java.util.Collection; |
21 | | -import java.util.function.Consumer; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
22 | 22 |
|
| 23 | +import org.springframework.beans.factory.ObjectProvider; |
23 | 24 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
24 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
25 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
26 | 27 | import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration; |
27 | | -import org.springframework.boot.context.properties.bind.Binder; |
| 28 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 29 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
28 | 30 | import org.springframework.boot.web.codec.CodecCustomizer; |
29 | 31 | import org.springframework.context.ApplicationContext; |
30 | 32 | import org.springframework.context.annotation.Bean; |
31 | 33 | import org.springframework.context.annotation.Configuration; |
32 | | -import org.springframework.http.codec.ClientCodecConfigurer; |
33 | 34 | import org.springframework.test.web.reactive.server.WebTestClient; |
34 | | -import org.springframework.test.web.reactive.server.WebTestClient.Builder; |
35 | | -import org.springframework.util.CollectionUtils; |
36 | | -import org.springframework.web.reactive.function.client.ExchangeStrategies; |
37 | 35 | import org.springframework.web.reactive.function.client.WebClient; |
38 | 36 |
|
39 | 37 | /** |
40 | 38 | * Auto-configuration for {@link WebTestClient}. |
41 | 39 | * |
42 | 40 | * @author Stephane Nicoll |
| 41 | + * @author Andy Wilkinson |
43 | 42 | * @since 2.0.0 |
44 | 43 | */ |
45 | 44 | @Configuration |
46 | 45 | @ConditionalOnClass({ WebClient.class, WebTestClient.class }) |
47 | 46 | @AutoConfigureAfter(CodecsAutoConfiguration.class) |
| 47 | +@EnableConfigurationProperties |
48 | 48 | public class WebTestClientAutoConfiguration { |
49 | 49 |
|
50 | 50 | @Bean |
51 | 51 | @ConditionalOnMissingBean |
52 | | - public WebTestClient webTestClient(ApplicationContext applicationContext) { |
| 52 | + public WebTestClient webTestClient(ApplicationContext applicationContext, |
| 53 | + List<WebTestClientBuilderCustomizer> customizers) { |
53 | 54 | WebTestClient.Builder builder = WebTestClient |
54 | 55 | .bindToApplicationContext(applicationContext).configureClient(); |
55 | | - customizeWebTestClient(builder, applicationContext); |
56 | | - customizeWebTestClientCodecs(builder, applicationContext); |
57 | | - return builder.build(); |
58 | | - } |
59 | | - |
60 | | - private void customizeWebTestClient(Builder builder, |
61 | | - ApplicationContext applicationContext) { |
62 | | - Binder.get(applicationContext.getEnvironment()) |
63 | | - .bind("spring.test.webtestclient.timeout", Duration.class) |
64 | | - .ifBound(builder::responseTimeout); |
65 | | - } |
66 | | - |
67 | | - private void customizeWebTestClientCodecs(WebTestClient.Builder builder, |
68 | | - ApplicationContext applicationContext) { |
69 | | - Collection<CodecCustomizer> customizers = applicationContext |
70 | | - .getBeansOfType(CodecCustomizer.class).values(); |
71 | | - if (!CollectionUtils.isEmpty(customizers)) { |
72 | | - builder.exchangeStrategies(ExchangeStrategies.builder() |
73 | | - .codecs(applyCustomizers(customizers)).build()); |
| 56 | + for (WebTestClientBuilderCustomizer customizer : customizers) { |
| 57 | + customizer.customize(builder); |
74 | 58 | } |
| 59 | + return builder.build(); |
75 | 60 | } |
76 | 61 |
|
77 | | - private Consumer<ClientCodecConfigurer> applyCustomizers( |
78 | | - Collection<CodecCustomizer> customizers) { |
79 | | - return (codecs) -> customizers |
80 | | - .forEach((customizer) -> customizer.customize(codecs)); |
| 62 | + @Bean |
| 63 | + @ConfigurationProperties(prefix = "spring.test.webtestclient") |
| 64 | + public SpringBootWebTestClientBuilderCustomizer springBootWebTestClientBuilderCustomizer( |
| 65 | + ObjectProvider<Collection<CodecCustomizer>> codecCustomizers) { |
| 66 | + return new SpringBootWebTestClientBuilderCustomizer( |
| 67 | + codecCustomizers.getIfAvailable(Collections::emptyList)); |
81 | 68 | } |
82 | 69 |
|
83 | 70 | } |
0 commit comments