use of org.springframework.mock.web.MockFilterChain in project spring-framework by spring-projects.
the class ConditionalDelegatingFilterProxyTests method setup.
@Before
public void setup() {
request = new MockHttpServletRequest();
request.setContextPath("/context");
response = new MockHttpServletResponse();
filterChain = new MockFilterChain();
delegate = new MockFilter();
}
use of org.springframework.mock.web.MockFilterChain in project spring-framework by spring-projects.
the class MockMvc method perform.
/**
* Perform a request and return a type that allows chaining further
* actions, such as asserting expectations, on the result.
*
* @param requestBuilder used to prepare the request to execute;
* see static factory methods in
* {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
*
* @return an instance of {@link ResultActions}; never {@code null}
*
* @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
*/
public ResultActions perform(RequestBuilder requestBuilder) throws Exception {
if (this.defaultRequestBuilder != null) {
if (requestBuilder instanceof Mergeable) {
requestBuilder = (RequestBuilder) ((Mergeable) requestBuilder).merge(this.defaultRequestBuilder);
}
}
MockHttpServletRequest request = requestBuilder.buildRequest(this.servletContext);
MockHttpServletResponse response = new MockHttpServletResponse();
if (requestBuilder instanceof SmartRequestBuilder) {
request = ((SmartRequestBuilder) requestBuilder).postProcessRequest(request);
}
final MvcResult mvcResult = new DefaultMvcResult(request, response);
request.setAttribute(MVC_RESULT_ATTRIBUTE, mvcResult);
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request, response));
MockFilterChain filterChain = new MockFilterChain(this.servlet, this.filters);
filterChain.doFilter(request, response);
if (DispatcherType.ASYNC.equals(request.getDispatcherType()) && request.getAsyncContext() != null & !request.isAsyncStarted()) {
request.getAsyncContext().complete();
}
applyDefaultResultActions(mvcResult);
RequestContextHolder.setRequestAttributes(previousAttributes);
return new ResultActions() {
@Override
public ResultActions andExpect(ResultMatcher matcher) throws Exception {
matcher.match(mvcResult);
return this;
}
@Override
public ResultActions andDo(ResultHandler handler) throws Exception {
handler.handle(mvcResult);
return this;
}
@Override
public MvcResult andReturn() {
return mvcResult;
}
};
}
use of org.springframework.mock.web.MockFilterChain in project druid by alibaba.
the class DruidStatServiceTest method test_statService_getWebURIList.
public void test_statService_getWebURIList() throws Exception {
String uri = "/";
MockServletContext servletContext = new MockServletContext();
MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
WebStatFilter filter = new WebStatFilter();
filter.init(filterConfig);
// first request test
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
filter.doFilter(request, response, chain);
String result = DruidStatService.getInstance().service("/weburi.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> webURIList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(webURIList.size(), equalTo(1));
Map<String, Object> webURI = webURIList.get(0);
assertThat((String) webURI.get("URI"), equalTo(uri));
assertThat((Integer) webURI.get("RequestCount"), equalTo(1));
// second request test
MockHttpServletRequest request2 = new MockHttpServletRequest("GET", uri);
MockHttpServletResponse response2 = new MockHttpServletResponse();
MockFilterChain chain2 = new MockFilterChain();
filter.doFilter(request2, response2, chain2);
result = DruidStatService.getInstance().service("/weburi.json");
resultMap = (Map<String, Object>) JSONUtils.parse(result);
webURIList = (List<Map<String, Object>>) resultMap.get("Content");
assertThat(webURIList.size(), equalTo(1));
webURI = webURIList.get(0);
assertThat((String) webURI.get("URI"), equalTo(uri));
assertThat((Integer) webURI.get("RequestCount"), equalTo(2));
filter.destroy();
}
use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.
the class AbstractVariableEvaluationContextPostProcessorTests method setup.
@Before
public void setup() {
this.processor = new VariableEvaluationContextPostProcessor();
this.request = new MockHttpServletRequest();
this.request.setServletPath("/");
this.response = new MockHttpServletResponse();
this.invocation = new FilterInvocation(this.request, this.response, new MockFilterChain());
this.context = new StandardEvaluationContext();
}
use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.
the class AbstractPreAuthenticatedProcessingFilterTests method requiresAuthenticationOverridePrincipalChangedTrue.
@Test
public void requiresAuthenticationOverridePrincipalChangedTrue() throws Exception {
Object principal = new Object();
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(principal, "something", "ROLE_USER"));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
ConcretePreAuthenticatedProcessingFilter filter = new ConcretePreAuthenticatedProcessingFilter() {
@Override
protected boolean principalChanged(HttpServletRequest request, Authentication currentAuthentication) {
return true;
}
};
filter.setCheckForPrincipalChanges(true);
filter.principal = principal;
AuthenticationManager am = mock(AuthenticationManager.class);
filter.setAuthenticationManager(am);
filter.afterPropertiesSet();
filter.doFilter(request, response, chain);
verify(am).authenticate(any(PreAuthenticatedAuthenticationToken.class));
}
Aggregations