Search in sources :

Example 61 with MockFilterChain

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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 62 with MockFilterChain

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;
        }
    };
}
Also used : Mergeable(org.springframework.beans.Mergeable) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 63 with MockFilterChain

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();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) List(java.util.List) MockFilterChain(org.springframework.mock.web.MockFilterChain) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext) WebStatFilter(com.alibaba.druid.support.http.WebStatFilter) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig)

Example 64 with MockFilterChain

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();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterInvocation(org.springframework.security.web.FilterInvocation) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 65 with MockFilterChain

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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)108 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)106 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)106 Test (org.junit.Test)77 ServletRequest (javax.servlet.ServletRequest)28 ServletResponse (javax.servlet.ServletResponse)28 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 NestedServletException (org.springframework.web.util.NestedServletException)19 Before (org.junit.Before)17 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)14 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)11 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)9 MockServletContext (org.springframework.mock.web.MockServletContext)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8