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.servlet;
018
019import java.util.List;
020
021import org.springframework.boot.autoconfigure.AutoConfigureAfter;
022import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
025import org.springframework.boot.autoconfigure.web.ServerProperties;
026import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
027import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
028import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
029import org.springframework.boot.context.properties.ConfigurationProperties;
030import org.springframework.boot.context.properties.EnableConfigurationProperties;
031import org.springframework.context.annotation.Bean;
032import org.springframework.context.annotation.Configuration;
033import org.springframework.test.web.servlet.DispatcherServletCustomizer;
034import org.springframework.test.web.servlet.MockMvc;
035import org.springframework.test.web.servlet.MockMvcBuilder;
036import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
037import org.springframework.test.web.servlet.setup.MockMvcBuilders;
038import org.springframework.web.context.WebApplicationContext;
039import org.springframework.web.servlet.DispatcherServlet;
040
041/**
042 * Auto-configuration for {@link MockMvc}.
043 *
044 * @author Phillip Webb
045 * @author Andy Wilkinson
046 * @author Stephane Nicoll
047 * @see AutoConfigureWebMvc
048 * @since 1.4.0
049 */
050@Configuration
051@ConditionalOnWebApplication(type = Type.SERVLET)
052@AutoConfigureAfter(WebMvcAutoConfiguration.class)
053@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })
054public class MockMvcAutoConfiguration {
055
056        private final WebApplicationContext context;
057
058        private final WebMvcProperties webMvcProperties;
059
060        MockMvcAutoConfiguration(WebApplicationContext context,
061                        WebMvcProperties webMvcProperties) {
062                this.context = context;
063                this.webMvcProperties = webMvcProperties;
064        }
065
066        @Bean
067        @ConditionalOnMissingBean
068        public DispatcherServletPath dispatcherServletPath() {
069                return () -> this.webMvcProperties.getServlet().getPath();
070        }
071
072        @Bean
073        @ConditionalOnMissingBean(MockMvcBuilder.class)
074        public DefaultMockMvcBuilder mockMvcBuilder(
075                        List<MockMvcBuilderCustomizer> customizers) {
076                DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.context);
077                builder.addDispatcherServletCustomizer(
078                                new MockMvcDispatcherServletCustomizer(this.webMvcProperties));
079                for (MockMvcBuilderCustomizer customizer : customizers) {
080                        customizer.customize(builder);
081                }
082                return builder;
083        }
084
085        @Bean
086        @ConfigurationProperties(prefix = "spring.test.mockmvc")
087        public SpringBootMockMvcBuilderCustomizer springBootMockMvcBuilderCustomizer() {
088                return new SpringBootMockMvcBuilderCustomizer(this.context);
089        }
090
091        @Bean
092        @ConditionalOnMissingBean
093        public MockMvc mockMvc(MockMvcBuilder builder) {
094                return builder.build();
095        }
096
097        @Bean
098        @ConditionalOnMissingBean
099        public DispatcherServlet dispatcherServlet(MockMvc mockMvc) {
100                return mockMvc.getDispatcherServlet();
101        }
102
103        private static class MockMvcDispatcherServletCustomizer
104                        implements DispatcherServletCustomizer {
105
106                private final WebMvcProperties webMvcProperties;
107
108                MockMvcDispatcherServletCustomizer(WebMvcProperties webMvcProperties) {
109                        this.webMvcProperties = webMvcProperties;
110                }
111
112                @Override
113                public void customize(DispatcherServlet dispatcherServlet) {
114                        dispatcherServlet.setDispatchOptionsRequest(
115                                        this.webMvcProperties.isDispatchOptionsRequest());
116                        dispatcherServlet.setDispatchTraceRequest(
117                                        this.webMvcProperties.isDispatchTraceRequest());
118                        dispatcherServlet.setThrowExceptionIfNoHandlerFound(
119                                        this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
120                }
121
122        }
123
124}