001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.test.autoconfigure.web.reactive; 018 019import java.util.List; 020import java.util.stream.Collectors; 021 022import org.springframework.beans.factory.ObjectProvider; 023import org.springframework.boot.autoconfigure.AutoConfigureAfter; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 027import org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration; 028import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; 029import org.springframework.boot.context.properties.ConfigurationProperties; 030import org.springframework.boot.context.properties.EnableConfigurationProperties; 031import org.springframework.boot.web.codec.CodecCustomizer; 032import org.springframework.context.ApplicationContext; 033import org.springframework.context.annotation.Bean; 034import org.springframework.context.annotation.Configuration; 035import org.springframework.context.annotation.Import; 036import org.springframework.test.web.reactive.server.MockServerConfigurer; 037import org.springframework.test.web.reactive.server.WebTestClient; 038import org.springframework.web.reactive.function.client.WebClient; 039import org.springframework.web.server.WebHandler; 040 041/** 042 * Auto-configuration for {@link WebTestClient}. 043 * 044 * @author Stephane Nicoll 045 * @author Andy Wilkinson 046 * @since 2.0.0 047 */ 048@Configuration 049@ConditionalOnClass({ WebClient.class, WebTestClient.class }) 050@AutoConfigureAfter({ CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class }) 051@Import(WebTestClientSecurityConfiguration.class) 052@EnableConfigurationProperties 053public class WebTestClientAutoConfiguration { 054 055 @Bean 056 @ConditionalOnMissingBean 057 @ConditionalOnBean(WebHandler.class) 058 public WebTestClient webTestClient(ApplicationContext applicationContext, 059 List<WebTestClientBuilderCustomizer> customizers, 060 List<MockServerConfigurer> configurers) { 061 WebTestClient.MockServerSpec<?> mockServerSpec = WebTestClient 062 .bindToApplicationContext(applicationContext); 063 for (MockServerConfigurer configurer : configurers) { 064 mockServerSpec.apply(configurer); 065 } 066 WebTestClient.Builder builder = mockServerSpec.configureClient(); 067 for (WebTestClientBuilderCustomizer customizer : customizers) { 068 customizer.customize(builder); 069 } 070 return builder.build(); 071 } 072 073 @Bean 074 @ConfigurationProperties(prefix = "spring.test.webtestclient") 075 public SpringBootWebTestClientBuilderCustomizer springBootWebTestClientBuilderCustomizer( 076 ObjectProvider<CodecCustomizer> codecCustomizers) { 077 return new SpringBootWebTestClientBuilderCustomizer( 078 codecCustomizers.orderedStream().collect(Collectors.toList())); 079 } 080 081}