Search in sources :

Example 1 with MockFilterConfig

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

the class ErrorPageFilterTests method oncePerRequest.

@Test
void oncePerRequest() throws Exception {
    this.chain = new TestFilterChain((request, response, chain) -> {
        response.sendError(400, "BAD");
        assertThat(request.getAttribute("FILTER.FILTERED")).isNotNull();
    });
    this.filter.init(new MockFilterConfig("FILTER"));
    this.filter.doFilter(this.request, this.response, this.chain);
}
Also used : MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MockRequestDispatcher(cn.taketoday.mock.web.MockRequestDispatcher) ClientAbortException(org.apache.catalina.connector.ClientAbortException) DeferredResult(cn.taketoday.web.context.async.DeferredResult) ServletException(jakarta.servlet.ServletException) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) OutputCaptureExtension(cn.taketoday.framework.test.system.OutputCaptureExtension) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ErrorPageFilter(cn.taketoday.framework.web.servlet.support.ErrorPageFilter) NestedServletException(cn.taketoday.web.servlet.NestedServletException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) Map(java.util.Map) WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) WebAsyncUtils(cn.taketoday.web.context.async.WebAsyncUtils) ServletRequest(jakarta.servlet.ServletRequest) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Mockito.never(org.mockito.Mockito.never) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletResponse(jakarta.servlet.ServletResponse) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) HttpStatus(cn.taketoday.http.HttpStatus) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) Mockito.mock(org.mockito.Mockito.mock) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 2 with MockFilterConfig

use of cn.taketoday.mock.web.MockFilterConfig 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 3 with MockFilterConfig

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

the class ErrorPageFilterTests method oncePerRequest.

@Test
void oncePerRequest() throws Exception {
    this.chain = new TestFilterChain((request, response, chain) -> {
        response.sendError(400, "BAD");
        assertThat(request.getAttribute("FILTER.FILTERED")).isNotNull();
    });
    this.filter.init(new MockFilterConfig("FILTER"));
    this.filter.doFilter(this.request, this.response, this.chain);
}
Also used : MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MockRequestDispatcher(cn.taketoday.mock.web.MockRequestDispatcher) ClientAbortException(org.apache.catalina.connector.ClientAbortException) DeferredResult(cn.taketoday.web.context.async.DeferredResult) ServletException(jakarta.servlet.ServletException) MockFilterChain(cn.taketoday.mock.web.MockFilterChain) OutputCaptureExtension(cn.taketoday.framework.test.system.OutputCaptureExtension) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ErrorPageFilter(cn.taketoday.framework.web.servlet.support.ErrorPageFilter) NestedServletException(cn.taketoday.web.servlet.NestedServletException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BDDMockito.given(org.mockito.BDDMockito.given) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) Map(java.util.Map) WebAsyncManager(cn.taketoday.web.context.async.WebAsyncManager) WebAsyncUtils(cn.taketoday.web.context.async.WebAsyncUtils) ServletRequest(jakarta.servlet.ServletRequest) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test) ErrorPage(cn.taketoday.framework.web.server.ErrorPage) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) Mockito.never(org.mockito.Mockito.never) StandardServletAsyncWebRequest(cn.taketoday.web.context.async.StandardServletAsyncWebRequest) ServletResponse(jakarta.servlet.ServletResponse) CapturedOutput(cn.taketoday.framework.test.system.CapturedOutput) HttpStatus(cn.taketoday.http.HttpStatus) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockHttpServletResponse(cn.taketoday.mock.web.MockHttpServletResponse) Mockito.mock(org.mockito.Mockito.mock) MockFilterConfig(cn.taketoday.mock.web.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 4 with MockFilterConfig

use of cn.taketoday.mock.web.MockFilterConfig 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)

Example 5 with MockFilterConfig

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

the class ServletWebServerApplicationContextTests method delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames.

@Test
void delegatingFilterProxyRegistrationBeansSkipsTargetBeanNames() {
    addWebServerFactoryBean();
    DelegatingFilterProxyRegistrationBean initializer = new DelegatingFilterProxyRegistrationBean("filterBean");
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    BeanDefinition filterBeanDefinition = beanDefinition(new IllegalStateException("Create FilterBean Failure"));
    filterBeanDefinition.setLazyInit(true);
    this.context.registerBeanDefinition("filterBean", filterBeanDefinition);
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    then(servletContext).should(atMost(1)).addFilter(anyString(), this.filterCaptor.capture());
    // Up to this point the filterBean should not have been created, calling
    // the delegate proxy will trigger creation and an exception
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
        this.filterCaptor.getValue().init(new MockFilterConfig());
        this.filterCaptor.getValue().doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    }).havingRootCause().isInstanceOf(IllegalStateException.class).withMessageContaining("Create FilterBean Failure");
}
Also used : DelegatingFilterProxyRegistrationBean(cn.taketoday.framework.web.servlet.DelegatingFilterProxyRegistrationBean) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(cn.taketoday.mock.web.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) AbstractBeanDefinition(cn.taketoday.beans.factory.support.AbstractBeanDefinition) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) 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)6 MockFilterConfig (cn.taketoday.mock.web.MockFilterConfig)6 MockHttpServletRequest (cn.taketoday.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (cn.taketoday.mock.web.MockHttpServletResponse)6 Test (org.junit.jupiter.api.Test)6 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)2 AbstractBeanDefinition (cn.taketoday.beans.factory.support.AbstractBeanDefinition)2 CapturedOutput (cn.taketoday.framework.test.system.CapturedOutput)2 OutputCaptureExtension (cn.taketoday.framework.test.system.OutputCaptureExtension)2 ErrorPage (cn.taketoday.framework.web.server.ErrorPage)2 DelegatingFilterProxyRegistrationBean (cn.taketoday.framework.web.servlet.DelegatingFilterProxyRegistrationBean)2 ErrorPageFilter (cn.taketoday.framework.web.servlet.support.ErrorPageFilter)2 HttpStatus (cn.taketoday.http.HttpStatus)2 MockRequestDispatcher (cn.taketoday.mock.web.MockRequestDispatcher)2 MissingRequestParameterException (cn.taketoday.web.bind.MissingRequestParameterException)2 DeferredResult (cn.taketoday.web.context.async.DeferredResult)2 StandardServletAsyncWebRequest (cn.taketoday.web.context.async.StandardServletAsyncWebRequest)2 WebAsyncManager (cn.taketoday.web.context.async.WebAsyncManager)2 WebAsyncUtils (cn.taketoday.web.context.async.WebAsyncUtils)2