Revise use of FilterChain in MockMvc
MockFilterChain should not be re-used across requests. This is not an issue unless tests are executed concurrently. This change ensures the MockFilterChain is re-created for each request in MockMvc. Issue: SPR-10838
This commit is contained in:
parent
a17912d19e
commit
072e5e8471
|
|
@ -19,6 +19,7 @@ package org.springframework.test.web.servlet;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.beans.Mergeable;
|
import org.springframework.beans.Mergeable;
|
||||||
|
|
@ -54,7 +55,9 @@ public final class MockMvc {
|
||||||
|
|
||||||
static String MVC_RESULT_ATTRIBUTE = MockMvc.class.getName().concat(".MVC_RESULT_ATTRIBUTE");
|
static String MVC_RESULT_ATTRIBUTE = MockMvc.class.getName().concat(".MVC_RESULT_ATTRIBUTE");
|
||||||
|
|
||||||
private final MockFilterChain filterChain;
|
private final TestDispatcherServlet servlet;
|
||||||
|
|
||||||
|
private final Filter[] filters;
|
||||||
|
|
||||||
private final ServletContext servletContext;
|
private final ServletContext servletContext;
|
||||||
|
|
||||||
|
|
@ -69,11 +72,15 @@ public final class MockMvc {
|
||||||
* Private constructor, not for direct instantiation.
|
* Private constructor, not for direct instantiation.
|
||||||
* @see org.springframework.test.web.servlet.setup.MockMvcBuilders
|
* @see org.springframework.test.web.servlet.setup.MockMvcBuilders
|
||||||
*/
|
*/
|
||||||
MockMvc(MockFilterChain filterChain, ServletContext servletContext) {
|
MockMvc(TestDispatcherServlet servlet, Filter[] filters, ServletContext servletContext) {
|
||||||
Assert.notNull(servletContext, "A ServletContext is required");
|
|
||||||
Assert.notNull(filterChain, "A MockFilterChain is required");
|
|
||||||
|
|
||||||
this.filterChain = filterChain;
|
Assert.notNull(servlet, "DispatcherServlet is required");
|
||||||
|
Assert.notNull(filters, "filters cannot be null");
|
||||||
|
Assert.noNullElements(filters, "filters cannot contain null values");
|
||||||
|
Assert.notNull(servletContext, "A ServletContext is required");
|
||||||
|
|
||||||
|
this.servlet = servlet;
|
||||||
|
this.filters = filters;
|
||||||
this.servletContext = servletContext;
|
this.servletContext = servletContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -130,8 +137,8 @@ public final class MockMvc {
|
||||||
final MvcResult mvcResult = new DefaultMvcResult(request, response);
|
final MvcResult mvcResult = new DefaultMvcResult(request, response);
|
||||||
request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);
|
request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);
|
||||||
|
|
||||||
this.filterChain.reset();
|
MockFilterChain filterChain = new MockFilterChain(this.servlet, this.filters);
|
||||||
this.filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
|
|
||||||
applyDefaultResultActions(mvcResult);
|
applyDefaultResultActions(mvcResult);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
import org.springframework.core.NestedRuntimeException;
|
import org.springframework.core.NestedRuntimeException;
|
||||||
import org.springframework.mock.web.MockFilterChain;
|
|
||||||
import org.springframework.mock.web.MockServletConfig;
|
import org.springframework.mock.web.MockServletConfig;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
|
@ -57,9 +56,7 @@ public abstract class MockMvcBuilderSupport {
|
||||||
throw new MockMvcBuildException("Failed to initialize TestDispatcherServlet", ex);
|
throw new MockMvcBuildException("Failed to initialize TestDispatcherServlet", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
MockFilterChain filterChain = new MockFilterChain(dispatcherServlet, filters);
|
MockMvc mockMvc = new MockMvc(dispatcherServlet, filters, servletContext);
|
||||||
|
|
||||||
MockMvc mockMvc = new MockMvc(filterChain, servletContext);
|
|
||||||
mockMvc.setDefaultRequest(defaultRequestBuilder);
|
mockMvc.setDefaultRequest(defaultRequestBuilder);
|
||||||
mockMvc.setGlobalResultMatchers(globalResultMatchers);
|
mockMvc.setGlobalResultMatchers(globalResultMatchers);
|
||||||
mockMvc.setGlobalResultHandlers(globalResultHandlers);
|
mockMvc.setGlobalResultHandlers(globalResultHandlers);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue