Search in sources :

Example 81 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class DelegatingFilterProxyRegistrationBeanTests method initShouldNotCauseEarlyInitialization.

@Test
public void initShouldNotCauseEarlyInitialization() throws Exception {
    this.applicationContext.registerBeanDefinition("mockFilter", new RootBeanDefinition(MockFilter.class));
    DelegatingFilterProxyRegistrationBean registrationBean = createFilterRegistrationBean();
    Filter filter = registrationBean.getFilter();
    filter.init(new MockFilterConfig());
    assertThat(mockFilterInitialized.get()).isNull();
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertThat(mockFilterInitialized.get()).isEqualTo(true);
}
Also used : Filter(javax.servlet.Filter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 82 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterAddsTimeTaken.

@Test
@SuppressWarnings("unchecked")
public void filterAddsTimeTaken() throws Exception {
    MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    this.filter.doFilter(request, response, chain);
    String timeTaken = (String) this.repository.findAll().iterator().next().getInfo().get("timeTaken");
    assertThat(timeTaken).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 83 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class HelloWebSecurityApplicationTests method setup.

@Before
public void setup() {
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.chain = new MockFilterChain();
}
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 84 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class RemoteDevToolsAutoConfigurationTests method setup.

@Before
public void setup() {
    this.request = new MockHttpServletRequest();
    this.response = new MockHttpServletResponse();
    this.chain = new MockFilterChain();
}
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 85 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-boot by spring-projects.

the class ErrorPageFilterTests method statusErrorWithCommittedResponse.

@Test
public void statusErrorWithCommittedResponse() throws Exception {
    this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
    this.chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            ((HttpServletResponse) response).sendError(400, "BAD");
            response.flushBuffer();
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(this.response.getForwardedUrl()).isNull();
}
Also used : ServletException(javax.servlet.ServletException) NestedServletException(org.springframework.web.util.NestedServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ErrorPage(org.springframework.boot.web.server.ErrorPage) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) IOException(java.io.IOException) MockFilterChain(org.springframework.mock.web.MockFilterChain) 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