17
17
package org .springframework .test .web .servlet .setup ;
18
18
19
19
import org .springframework .mock .web .MockServletConfig ;
20
- import org .springframework .test .web .servlet .*;
20
+ import org .springframework .test .web .servlet .MockMvc ;
21
+ import org .springframework .test .web .servlet .MockMvcBuilderSupport ;
22
+ import org .springframework .test .web .servlet .RequestBuilder ;
23
+ import org .springframework .test .web .servlet .ResultHandler ;
24
+ import org .springframework .test .web .servlet .ResultMatcher ;
25
+ import org .springframework .test .web .servlet .request .ConfigurableSmartRequestBuilder ;
26
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
27
+ import org .springframework .test .web .servlet .request .RequestPostProcessor ;
21
28
import org .springframework .util .Assert ;
22
29
import org .springframework .web .context .WebApplicationContext ;
23
30
38
45
* @since 4.0
39
46
*/
40
47
public abstract class AbstractMockMvcBuilder <B extends AbstractMockMvcBuilder <B >>
41
- extends MockMvcBuilderSupport implements MockMvcBuilder {
48
+ extends MockMvcBuilderSupport implements ConfigurableMockMvcBuilder < B > {
42
49
43
50
private List <Filter > filters = new ArrayList <Filter >();
44
51
@@ -53,27 +60,6 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
53
60
private final List <MockMvcConfigurer > configurers = new ArrayList <MockMvcConfigurer >(4 );
54
61
55
62
56
-
57
- /**
58
- * Add filters mapped to any request (i.e. "/*"). For example:
59
- *
60
- * <pre class="code">
61
- * mockMvcBuilder.addFilters(springSecurityFilterChain);
62
- * </pre>
63
- *
64
- * <p>is the equivalent of the following web.xml configuration:
65
- *
66
- * <pre class="code">
67
- * <filter-mapping>
68
- * <filter-name>springSecurityFilterChain</filter-name>
69
- * <url-pattern>/*</url-pattern>
70
- * </filter-mapping>
71
- * </pre>
72
- *
73
- * <p>Filters will be invoked in the order in which they are provided.
74
- *
75
- * @param filters the filters to add
76
- */
77
63
@ SuppressWarnings ("unchecked" )
78
64
public final <T extends B > T addFilters (Filter ... filters ) {
79
65
Assert .notNull (filters , "filters cannot be null" );
@@ -85,27 +71,6 @@ public final <T extends B> T addFilters(Filter... filters) {
85
71
return (T ) this ;
86
72
}
87
73
88
- /**
89
- * Add a filter mapped to a specific set of patterns. For example:
90
- *
91
- * <pre class="code">
92
- * mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");
93
- * </pre>
94
- *
95
- * <p>is the equivalent of:
96
- *
97
- * <pre class="code">
98
- * <filter-mapping>
99
- * <filter-name>myResourceFilter</filter-name>
100
- * <url-pattern>/resources/*</url-pattern>
101
- * </filter-mapping>
102
- * </pre>
103
- *
104
- * <p>Filters will be invoked in the order in which they are provided.
105
- *
106
- * @param filter the filter to add
107
- * @param urlPatterns URL patterns to map to; if empty, "/*" is used by default
108
- */
109
74
@ SuppressWarnings ("unchecked" )
110
75
public final <T extends B > T addFilter (Filter filter , String ... urlPatterns ) {
111
76
@@ -120,70 +85,32 @@ public final <T extends B> T addFilter(Filter filter, String... urlPatterns) {
120
85
return (T ) this ;
121
86
}
122
87
123
- /**
124
- * Define default request properties that should be merged into all
125
- * performed requests. In effect this provides a mechanism for defining
126
- * common initialization for all requests such as the content type, request
127
- * parameters, session attributes, and any other request property.
128
- *
129
- * <p>Properties specified at the time of performing a request override the
130
- * default properties defined here.
131
- *
132
- * @param requestBuilder a RequestBuilder; see static factory methods in
133
- * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
134
- * .
135
- */
136
88
@ SuppressWarnings ("unchecked" )
137
89
public final <T extends B > T defaultRequest (RequestBuilder requestBuilder ) {
138
90
this .defaultRequestBuilder = requestBuilder ;
139
91
return (T ) this ;
140
92
}
141
93
142
- /**
143
- * Define a global expectation that should <em>always</em> be applied to
144
- * every response. For example, status code 200 (OK), content type
145
- * {@code "application/json"}, etc.
146
- *
147
- * @param resultMatcher a ResultMatcher; see static factory methods in
148
- * {@link org.springframework.test.web.servlet.result.MockMvcResultMatchers}
149
- */
150
94
@ SuppressWarnings ("unchecked" )
151
95
public final <T extends B > T alwaysExpect (ResultMatcher resultMatcher ) {
152
96
this .globalResultMatchers .add (resultMatcher );
153
97
return (T ) this ;
154
98
}
155
99
156
- /**
157
- * Define a global action that should <em>always</em> be applied to every
158
- * response. For example, writing detailed information about the performed
159
- * request and resulting response to {@code System.out}.
160
- *
161
- * @param resultHandler a ResultHandler; see static factory methods in
162
- * {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers}
163
- */
164
100
@ SuppressWarnings ("unchecked" )
165
101
public final <T extends B > T alwaysDo (ResultHandler resultHandler ) {
166
102
this .globalResultHandlers .add (resultHandler );
167
103
return (T ) this ;
168
104
}
169
105
170
- /**
171
- * Whether to enable the DispatcherServlet property
172
- * {@link org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest
173
- * dispatchOptionsRequest} which allows processing of HTTP OPTIONS requests.
174
- */
175
106
@ SuppressWarnings ("unchecked" )
176
107
public final <T extends B > T dispatchOptions (boolean dispatchOptions ) {
177
108
this .dispatchOptions = dispatchOptions ;
178
109
return (T ) this ;
179
110
}
180
111
181
- /**
182
- * Add a {@code MockMvcConfigurer} which encapsulates ways to further configure
183
- * this MockMvcBuilder with some specific purpose in mind.
184
- */
185
112
@ SuppressWarnings ("unchecked" )
186
- public final <T extends B > T add (MockMvcConfigurer configurer ) {
113
+ public final <T extends B > T apply (MockMvcConfigurer configurer ) {
187
114
configurer .afterConfigurerAdded (this );
188
115
this .configurers .add (configurer );
189
116
return (T ) this ;
@@ -202,7 +129,15 @@ public final MockMvc build() {
202
129
MockServletConfig mockServletConfig = new MockServletConfig (servletContext );
203
130
204
131
for (MockMvcConfigurer configurer : this .configurers ) {
205
- configurer .beforeMockMvcCreated (this , this .defaultRequestBuilder , wac );
132
+ RequestPostProcessor processor = configurer .beforeMockMvcCreated (this , wac );
133
+ if (processor != null ) {
134
+ if (this .defaultRequestBuilder == null ) {
135
+ this .defaultRequestBuilder = MockMvcRequestBuilders .get ("/" );
136
+ }
137
+ if (this .defaultRequestBuilder instanceof ConfigurableSmartRequestBuilder ) {
138
+ ((ConfigurableSmartRequestBuilder ) this .defaultRequestBuilder ).with (processor );
139
+ }
140
+ }
206
141
}
207
142
208
143
Filter [] filterArray = this .filters .toArray (new Filter [this .filters .size ()]);
@@ -218,4 +153,4 @@ public final MockMvc build() {
218
153
*/
219
154
protected abstract WebApplicationContext initWebAppContext ();
220
155
221
- }
156
+ }
0 commit comments