Search in sources :

Example 1 with FilterRegistrationBean

use of cn.taketoday.framework.web.servlet.FilterRegistrationBean in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method orderedBeanInsertedCorrectly.

@Test
void orderedBeanInsertedCorrectly() {
    addWebServerFactoryBean();
    OrderedFilter filter = new OrderedFilter();
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
    registration.setFilter(mock(Filter.class));
    registration.setOrder(100);
    this.context.registerBeanDefinition("filterRegistrationBean", beanDefinition(registration));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    then(factory.getServletContext()).should().addFilter("filterBean", filter);
    then(factory.getServletContext()).should().addFilter("object", registration.getFilter());
    assertThat(factory.getRegisteredFilter(0).getFilter()).isEqualTo(filter);
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) Filter(jakarta.servlet.Filter) FilterRegistrationBean(cn.taketoday.framework.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Example 2 with FilterRegistrationBean

use of cn.taketoday.framework.web.servlet.FilterRegistrationBean in project today-infrastructure by TAKETODAY.

the class ServletWebServerApplicationContextTests method filterRegistrationBeansSkipsRegisteredFilters.

@Test
void filterRegistrationBeansSkipsRegisteredFilters() {
    addWebServerFactoryBean();
    Filter filter = mock(Filter.class);
    FilterRegistrationBean<Filter> initializer = new FilterRegistrationBean<>(filter);
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    then(servletContext).should(atMost(1)).addFilter(anyString(), any(Filter.class));
}
Also used : Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) FilterRegistrationBean(cn.taketoday.framework.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Example 3 with FilterRegistrationBean

use of cn.taketoday.framework.web.servlet.FilterRegistrationBean in project today-framework by TAKETODAY.

the class ServletWebServerApplicationContextTests method filterRegistrationBeansSkipsRegisteredFilters.

@Test
void filterRegistrationBeansSkipsRegisteredFilters() {
    addWebServerFactoryBean();
    Filter filter = mock(Filter.class);
    FilterRegistrationBean<Filter> initializer = new FilterRegistrationBean<>(filter);
    this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    this.context.refresh();
    ServletContext servletContext = getWebServerFactory().getServletContext();
    then(servletContext).should(atMost(1)).addFilter(anyString(), any(Filter.class));
}
Also used : Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) FilterRegistrationBean(cn.taketoday.framework.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Example 4 with FilterRegistrationBean

use of cn.taketoday.framework.web.servlet.FilterRegistrationBean in project today-framework by TAKETODAY.

the class AbstractServletWebServerFactoryTests method startServletAndFilter.

@Test
void startServletAndFilter() throws Exception {
    AbstractServletWebServerFactory factory = getFactory();
    this.webServer = factory.getWebServer(exampleServletRegistration(), new FilterRegistrationBean<>(new ExampleFilter()));
    this.webServer.start();
    assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("[Hello World]");
}
Also used : ExampleFilter(cn.taketoday.test.web.servlet.ExampleFilter) FilterRegistrationBean(cn.taketoday.framework.web.servlet.FilterRegistrationBean) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 5 with FilterRegistrationBean

use of cn.taketoday.framework.web.servlet.FilterRegistrationBean in project today-framework by TAKETODAY.

the class ServletWebServerApplicationContextTests method orderedBeanInsertedCorrectly.

@Test
void orderedBeanInsertedCorrectly() {
    addWebServerFactoryBean();
    OrderedFilter filter = new OrderedFilter();
    this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
    FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
    registration.setFilter(mock(Filter.class));
    registration.setOrder(100);
    this.context.registerBeanDefinition("filterRegistrationBean", beanDefinition(registration));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    then(factory.getServletContext()).should().addFilter("filterBean", filter);
    then(factory.getServletContext()).should().addFilter("object", registration.getFilter());
    assertThat(factory.getRegisteredFilter(0).getFilter()).isEqualTo(filter);
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) Filter(jakarta.servlet.Filter) FilterRegistrationBean(cn.taketoday.framework.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Aggregations

FilterRegistrationBean (cn.taketoday.framework.web.servlet.FilterRegistrationBean)6 Test (org.junit.jupiter.api.Test)6 Filter (jakarta.servlet.Filter)4 MockServletWebServerFactory (cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory)2 ExampleFilter (cn.taketoday.test.web.servlet.ExampleFilter)2 ServletContext (jakarta.servlet.ServletContext)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2