Search in sources :

Example 1 with MockFilterChain

use of cn.taketoday.mock.web.MockFilterChain in project today-infrastructure by TAKETODAY.

the class DelegatingFilterProxyRegistrationBeanTests method initShouldNotCauseEarlyInitialization.

@Test
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()).isTrue();
}
Also used : Filter(jakarta.servlet.Filter) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 2 with MockFilterChain

use of cn.taketoday.mock.web.MockFilterChain in project today-infrastructure by TAKETODAY.

the class MockFilterChainTests method doFilterEmptyChain.

@Test
void doFilterEmptyChain() throws Exception {
    MockFilterChain chain = new MockFilterChain();
    chain.doFilter(this.request, this.response);
    assertThat(chain.getRequest()).isEqualTo(request);
    assertThat(chain.getResponse()).isEqualTo(response);
    assertThatIllegalStateException().isThrownBy(() -> chain.doFilter(this.request, this.response)).withMessage("This FilterChain has already been called!");
}
Also used : MockFilterChain(cn.taketoday.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 3 with MockFilterChain

use of cn.taketoday.mock.web.MockFilterChain in project today-infrastructure by TAKETODAY.

the class MockFilterChainTests method doFilterWithServlet.

@Test
void doFilterWithServlet() throws Exception {
    Servlet servlet = mock(Servlet.class);
    MockFilterChain chain = new MockFilterChain(servlet);
    chain.doFilter(this.request, this.response);
    verify(servlet).service(this.request, this.response);
    assertThatIllegalStateException().isThrownBy(() -> chain.doFilter(this.request, this.response)).withMessage("This FilterChain has already been called!");
}
Also used : Servlet(jakarta.servlet.Servlet) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 4 with MockFilterChain

use of cn.taketoday.mock.web.MockFilterChain in project today-framework by TAKETODAY.

the class MockFilterChainTests method doFilterWithServletAndFilters.

@Test
void doFilterWithServletAndFilters() throws Exception {
    Servlet servlet = mock(Servlet.class);
    MockFilter filter2 = new MockFilter(servlet);
    MockFilter filter1 = new MockFilter(null);
    MockFilterChain chain = new MockFilterChain(servlet, filter1, filter2);
    chain.doFilter(this.request, this.response);
    assertThat(filter1.invoked).isTrue();
    assertThat(filter2.invoked).isTrue();
    verify(servlet).service(this.request, this.response);
    assertThatIllegalStateException().isThrownBy(() -> chain.doFilter(this.request, this.response)).withMessage("This FilterChain has already been called!");
}
Also used : Servlet(jakarta.servlet.Servlet) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) Test(org.junit.jupiter.api.Test)

Example 5 with MockFilterChain

use of cn.taketoday.mock.web.MockFilterChain in project today-framework by TAKETODAY.

the class DelegatingFilterProxyRegistrationBeanTests method initShouldNotCauseEarlyInitialization.

@Test
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()).isTrue();
}
Also used : Filter(jakarta.servlet.Filter) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

MockFilterChain (cn.taketoday.mock.web.MockFilterChain)14 Test (org.junit.jupiter.api.Test)14 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 MockFilterConfig (cn.taketoday.mock.web.MockFilterConfig)4 MockHttpServletRequest (cn.taketoday.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (cn.taketoday.mock.web.MockHttpServletResponse)4 Servlet (jakarta.servlet.Servlet)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)2 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)2 DelegatingFilterProxyRegistrationBean (cn.taketoday.framework.web.servlet.DelegatingFilterProxyRegistrationBean)2 Filter (jakarta.servlet.Filter)2 ServletContext (jakarta.servlet.ServletContext)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2