Browse Source
This is a follow-up on the commit introducing MockMvcConfigurer:
c2b0fac852
This commit refines the MockMvcConfigurer contract to use (the new)
ConfigurableMockMvcBuilder hence not requiring downcasting to
AbstractMockMvcBuilder.
The same also no longer passes the "default" RequestBuilder which would
also require a downcast, but rather allows a RequestPostProcessor to be
returned so that a 3rd party framework or application can modify any
property of every performed MockHttpServletRequest.
To make this possible the new SmartRequestBuilder interface separates
request building from request post processing while the new
ConfigurableSmartRequestBuilder allows adding a RequestPostProcessor
to a MockMvcBuilder.
Issue: SPR-11497
pull/599/merge
10 changed files with 373 additions and 134 deletions
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.test.web.servlet; |
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor; |
||||
|
||||
/** |
||||
* Extended variant of a {@link RequestBuilder} that applies its |
||||
* {@link org.springframework.test.web.servlet.request.RequestPostProcessor}s |
||||
* as a separate step from the {@link #buildRequest} method. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public interface SmartRequestBuilder extends RequestBuilder { |
||||
|
||||
/** |
||||
* Apply request post processing. Typically that means invoking one or more |
||||
* {@link org.springframework.test.web.servlet.request.RequestPostProcessor}s. |
||||
* |
||||
* @param request the request to initialize |
||||
* @return the request to use, either the one passed in or a wrapped one |
||||
*/ |
||||
MockHttpServletRequest postProcessRequest(MockHttpServletRequest request); |
||||
|
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.test.web.servlet.request; |
||||
|
||||
import org.springframework.test.web.servlet.SmartRequestBuilder; |
||||
|
||||
|
||||
/** |
||||
* An extension of {@link org.springframework.test.web.servlet.SmartRequestBuilder |
||||
* SmartRequestBuilder} that can be configured with {@link RequestPostProcessor}s. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public interface ConfigurableSmartRequestBuilder<B extends ConfigurableSmartRequestBuilder<B>> |
||||
extends SmartRequestBuilder { |
||||
|
||||
/** |
||||
* Add the given {@code RequestPostProcessor}. |
||||
*/ |
||||
B with(RequestPostProcessor requestPostProcessor); |
||||
|
||||
} |
||||
@ -0,0 +1,127 @@
@@ -0,0 +1,127 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.servlet.setup; |
||||
|
||||
import org.springframework.test.web.servlet.MockMvcBuilder; |
||||
import org.springframework.test.web.servlet.RequestBuilder; |
||||
import org.springframework.test.web.servlet.ResultHandler; |
||||
import org.springframework.test.web.servlet.ResultMatcher; |
||||
|
||||
import javax.servlet.Filter; |
||||
|
||||
/** |
||||
* Defines common methods for building a {@code MockMvc}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public interface ConfigurableMockMvcBuilder<B extends ConfigurableMockMvcBuilder<B>> extends MockMvcBuilder { |
||||
|
||||
/** |
||||
* Add filters mapped to any request (i.e. "/*"). For example: |
||||
* |
||||
* <pre class="code"> |
||||
* mockMvcBuilder.addFilters(springSecurityFilterChain); |
||||
* </pre> |
||||
* |
||||
* <p>is the equivalent of the following web.xml configuration: |
||||
* |
||||
* <pre class="code"> |
||||
* <filter-mapping> |
||||
* <filter-name>springSecurityFilterChain</filter-name> |
||||
* <url-pattern>/*</url-pattern> |
||||
* </filter-mapping> |
||||
* </pre> |
||||
* |
||||
* <p>Filters will be invoked in the order in which they are provided. |
||||
* |
||||
* @param filters the filters to add |
||||
*/ |
||||
<T extends B> T addFilters(Filter... filters); |
||||
|
||||
/** |
||||
* Add a filter mapped to a specific set of patterns. For example: |
||||
* |
||||
* <pre class="code"> |
||||
* mockMvcBuilder.addFilters(myResourceFilter, "/resources/*"); |
||||
* </pre> |
||||
* |
||||
* <p>is the equivalent of: |
||||
* |
||||
* <pre class="code"> |
||||
* <filter-mapping> |
||||
* <filter-name>myResourceFilter</filter-name> |
||||
* <url-pattern>/resources/*</url-pattern> |
||||
* </filter-mapping> |
||||
* </pre> |
||||
* |
||||
* <p>Filters will be invoked in the order in which they are provided. |
||||
* |
||||
* @param filter the filter to add |
||||
* @param urlPatterns URL patterns to map to; if empty, "/*" is used by default |
||||
*/ |
||||
<T extends B> T addFilter(Filter filter, String... urlPatterns); |
||||
|
||||
/** |
||||
* Define default request properties that should be merged into all |
||||
* performed requests. In effect this provides a mechanism for defining |
||||
* common initialization for all requests such as the content type, request |
||||
* parameters, session attributes, and any other request property. |
||||
* |
||||
* <p>Properties specified at the time of performing a request override the |
||||
* default properties defined here. |
||||
* |
||||
* @param requestBuilder a RequestBuilder; see static factory methods in |
||||
* {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders} |
||||
* . |
||||
*/ |
||||
<T extends B> T defaultRequest(RequestBuilder requestBuilder); |
||||
|
||||
/** |
||||
* Define a global expectation that should <em>always</em> be applied to |
||||
* every response. For example, status code 200 (OK), content type |
||||
* {@code "application/json"}, etc. |
||||
* |
||||
* @param resultMatcher a ResultMatcher; see static factory methods in |
||||
* {@link org.springframework.test.web.servlet.result.MockMvcResultMatchers} |
||||
*/ |
||||
<T extends B> T alwaysExpect(ResultMatcher resultMatcher); |
||||
|
||||
/** |
||||
* Define a global action that should <em>always</em> be applied to every |
||||
* response. For example, writing detailed information about the performed |
||||
* request and resulting response to {@code System.out}. |
||||
* |
||||
* @param resultHandler a ResultHandler; see static factory methods in |
||||
* {@link org.springframework.test.web.servlet.result.MockMvcResultHandlers} |
||||
*/ |
||||
<T extends B> T alwaysDo(ResultHandler resultHandler); |
||||
|
||||
/** |
||||
* Whether to enable the DispatcherServlet property |
||||
* {@link org.springframework.web.servlet.DispatcherServlet#setDispatchOptionsRequest |
||||
* dispatchOptionsRequest} which allows processing of HTTP OPTIONS requests. |
||||
*/ |
||||
<T extends B> T dispatchOptions(boolean dispatchOptions); |
||||
|
||||
/** |
||||
* Add a {@code MockMvcConfigurer} that automates MockMvc setup and |
||||
* configures it for some specific purpose (e.g. security). |
||||
*/ |
||||
<T extends B> T apply(MockMvcConfigurer configurer); |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.servlet.setup; |
||||
|
||||
import org.springframework.test.web.servlet.RequestBuilder; |
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor; |
||||
import org.springframework.web.context.WebApplicationContext; |
||||
|
||||
|
||||
/** |
||||
* An empty method implementation of {@link MockMvcConfigurer}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public abstract class MockMvcConfigurerAdapter implements MockMvcConfigurer { |
||||
|
||||
@Override |
||||
public void afterConfigurerAdded(ConfigurableMockMvcBuilder<?> builder) { |
||||
} |
||||
|
||||
@Override |
||||
public RequestPostProcessor beforeMockMvcCreated(ConfigurableMockMvcBuilder<?> builder, WebApplicationContext cxt) { |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue