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.time.Duration;
020import java.util.Collection;
021import java.util.function.Consumer;
022
023import org.springframework.boot.web.codec.CodecCustomizer;
024import org.springframework.http.codec.ClientCodecConfigurer;
025import org.springframework.test.web.reactive.server.WebTestClient;
026import org.springframework.test.web.reactive.server.WebTestClient.Builder;
027import org.springframework.util.CollectionUtils;
028import org.springframework.web.reactive.function.client.ExchangeStrategies;
029
030/**
031 * {@link WebTestClientBuilderCustomizer} for a typical Spring Boot application. Usually
032 * applied automatically via
033 * {@link AutoConfigureWebTestClient @AutoConfigureWebTestClient}, but may also be used
034 * directly.
035 *
036 * @author Andy Wilkinson
037 * @since 2.0.0
038 */
039public class SpringBootWebTestClientBuilderCustomizer
040                implements WebTestClientBuilderCustomizer {
041
042        private final Collection<CodecCustomizer> codecCustomizers;
043
044        private Duration timeout;
045
046        /**
047         * Create a new {@code SpringBootWebTestClientBuilderCustomizer} that will configure
048         * the builder's codecs using the given {@code codecCustomizers}.
049         * @param codecCustomizers the codec customizers
050         */
051        public SpringBootWebTestClientBuilderCustomizer(
052                        Collection<CodecCustomizer> codecCustomizers) {
053                this.codecCustomizers = codecCustomizers;
054        }
055
056        public void setTimeout(Duration timeout) {
057                this.timeout = timeout;
058        }
059
060        @Override
061        public void customize(Builder builder) {
062                if (this.timeout != null) {
063                        builder.responseTimeout(this.timeout);
064                }
065                customizeWebTestClientCodecs(builder);
066        }
067
068        private void customizeWebTestClientCodecs(WebTestClient.Builder builder) {
069                if (!CollectionUtils.isEmpty(this.codecCustomizers)) {
070                        builder.exchangeStrategies(ExchangeStrategies.builder()
071                                        .codecs(applyCustomizers(this.codecCustomizers)).build());
072                }
073        }
074
075        private Consumer<ClientCodecConfigurer> applyCustomizers(
076                        Collection<CodecCustomizer> customizers) {
077                return (codecs) -> customizers
078                                .forEach((customizer) -> customizer.customize(codecs));
079        }
080
081}