001/*
002 * Copyright 2012-2017 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.servlet;
018
019import com.gargoylesoftware.htmlunit.WebClient;
020
021import org.springframework.boot.autoconfigure.AutoConfigureAfter;
022import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
026import org.springframework.boot.test.web.htmlunit.LocalHostWebClient;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.core.env.Environment;
030import org.springframework.test.web.servlet.MockMvc;
031import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
032
033/**
034 * Auto-configuration for HtmlUnit {@link WebClient} MockMVC integration.
035 *
036 * @author Phillip Webb
037 * @since 1.4.0
038 */
039@Configuration
040@ConditionalOnClass(WebClient.class)
041@AutoConfigureAfter(MockMvcAutoConfiguration.class)
042@ConditionalOnProperty(prefix = "spring.test.mockmvc.webclient", name = "enabled", matchIfMissing = true)
043public class MockMvcWebClientAutoConfiguration {
044
045        private final Environment environment;
046
047        MockMvcWebClientAutoConfiguration(Environment environment) {
048                this.environment = environment;
049        }
050
051        @Bean
052        @ConditionalOnMissingBean({ WebClient.class, MockMvcWebClientBuilder.class })
053        @ConditionalOnBean(MockMvc.class)
054        public MockMvcWebClientBuilder mockMvcWebClientBuilder(MockMvc mockMvc) {
055                return MockMvcWebClientBuilder.mockMvcSetup(mockMvc)
056                                .withDelegate(new LocalHostWebClient(this.environment));
057        }
058
059        @Bean
060        @ConditionalOnMissingBean
061        @ConditionalOnBean(MockMvcWebClientBuilder.class)
062        public WebClient htmlUnitWebClient(MockMvcWebClientBuilder builder) {
063                return builder.build();
064        }
065
066}