001/* 002 * Copyright 2002-2014 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 * https://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.test.web.servlet.setup; 018 019import javax.servlet.Filter; 020 021import org.springframework.test.web.servlet.MockMvcBuilder; 022import org.springframework.test.web.servlet.RequestBuilder; 023import org.springframework.test.web.servlet.ResultHandler; 024import org.springframework.test.web.servlet.ResultMatcher; 025 026/** 027 * Defines common methods for building a {@code MockMvc}. 028 * 029 * @author Rossen Stoyanchev 030 * @since 4.1 031 */ 032public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder { 033 034 /** 035 * Add filters mapped to any request (i.e. "/*"). For example: 036 * 037 * <pre class="code"> 038 * mockMvcBuilder.addFilters(springSecurityFilterChain); 039 * </pre> 040 * 041 * <p>is the equivalent of the following web.xml configuration: 042 * 043 * <pre class="code"> 044 * <filter-mapping> 045 * <filter-name>springSecurityFilterChain</filter-name> 046 * <url-pattern>/*</url-pattern> 047 * </filter-mapping> 048 * </pre> 049 * 050 * <p>Filters will be invoked in the order in which they are provided. 051 * 052 * @param filters the filters to add 053 */ 054 <T extends B> T addFilters(Filter... filters); 055 056 /** 057 * Add a filter mapped to a specific set of patterns. For example: 058 * 059 * <pre class="code"> 060 * mockMvcBuilder.addFilters(myResourceFilter, "/resources/*"); 061 * </pre> 062 * 063 * <p>is the equivalent of: 064 * 065 * <pre class="code"> 066 * <filter-mapping> 067 * <filter-name>myResourceFilter</filter-name> 068 * <url-pattern>/resources/*</url-pattern> 069 * </filter-mapping> 070 * </pre> 071 * 072 * <p>Filters will be invoked in the order in which they are provided. 073 * 074 * @param filter the filter to add 075 * @param urlPatterns URL patterns to map to; if empty, "/*" is used by default 076 */ 077 <T extends B> T addFilter(Filter filter, String... urlPatterns); 078 079 /** 080 * Define default request properties that should be merged into all 081 * performed requests. In effect this provides a mechanism for defining 082 * common initialization for all requests such as the content type, request 083 * parameters, session attributes, and any other request property. 084 * 085 * <p>Properties specified at the time of performing a request override the 086 * default properties defined here. 087 * 088 * @param requestBuilder a RequestBuilder; see static factory methods in 089 * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders} 090 * . 091 */ 092 <T extends B> T defaultRequest(RequestBuilder requestBuilder); 093 094 /** 095 * Define a global expectation that should <em>always</em> be applied to 096 * every response. For example, status code 200 (OK), content type 097 * {@code "application/json"}, etc. 098 * 099 * @param resultMatcher a ResultMatcher; see static factory methods in 100 * {@link org.springframework.test.web.servlet.result.MockMvcResultMatchers} 101 */ 102 <T extends B> T alwaysExpect(ResultMatcher resultMatcher); 103 104 /** 105 * Define a global action that should <em>always</em> be applied to every 106 * response. For example, writing detailed information about the performed 107 * request and resulting response to {@code System.out}. 108 * 109 * @param resultHandler a ResultHandler; see static factory methods in 110 * {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers} 111 */ 112 <T extends B> T alwaysDo(ResultHandler resultHandler); 113 114 /** 115 * Whether to enable the DispatcherServlet property 116 * {@link org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest 117 * dispatchOptionsRequest} which allows processing of HTTP OPTIONS requests. 118 */ 119 <T extends B> T dispatchOptions(boolean dispatchOptions); 120 121 /** 122 * Add a {@code MockMvcConfigurer} that automates MockMvc setup and 123 * configures it for some specific purpose (e.g. security). 124 */ 125 <T extends B> T apply(MockMvcConfigurer configurer); 126 127}