Search in sources :

Example 1 with MockServletWebServerFactory

use of cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory 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 MockServletWebServerFactory

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

the class ServletWebServerApplicationContextTests method multipleServletBeansWithMainDispatcher.

@Test
void multipleServletBeansWithMainDispatcher() {
    addWebServerFactoryBean();
    Servlet servlet1 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet1).getOrder()).willReturn(1);
    Servlet servlet2 = mock(Servlet.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) servlet2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean2", beanDefinition(servlet2));
    this.context.registerBeanDefinition("dispatcherServlet", beanDefinition(servlet1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(servletContext).should(ordered).addServlet("dispatcherServlet", servlet1);
    then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletWebServerFactory

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

the class ServletWebServerApplicationContextTests method startRegistrations.

@Test
void startRegistrations() {
    addWebServerFactoryBean();
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    // Ensure that the context has been setup
    assertThat(this.context.getServletContext()).isEqualTo(factory.getServletContext());
    then(factory.getServletContext()).should().setAttribute(WebServletApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
    // Ensure WebApplicationContextUtils.registerWebApplicationScopes was called
    assertThat(this.context.getBeanFactory().getRegisteredScope(WebApplicationContext.SCOPE_SESSION)).isInstanceOf(SessionScope.class);
    // Ensure WebApplicationContextUtils.registerEnvironmentBeans was called
    assertThat(this.context.containsBean(WebServletApplicationContext.SERVLET_CONTEXT_BEAN_NAME)).isTrue();
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) Test(org.junit.jupiter.api.Test)

Example 4 with MockServletWebServerFactory

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

the class ServletWebServerApplicationContextTests method servletAndFilterBeans.

@Test
void servletAndFilterBeans() {
    addWebServerFactoryBean();
    Servlet servlet = mock(Servlet.class);
    Filter filter1 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter1).getOrder()).willReturn(1);
    Filter filter2 = mock(Filter.class, withSettings().extraInterfaces(Ordered.class));
    given(((Ordered) filter2).getOrder()).willReturn(2);
    this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
    this.context.registerBeanDefinition("filterBean2", beanDefinition(filter2));
    this.context.registerBeanDefinition("filterBean1", beanDefinition(filter1));
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    ServletContext servletContext = factory.getServletContext();
    InOrder ordered = inOrder(servletContext);
    then(factory.getServletContext()).should().addServlet("servletBean", servlet);
    then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/");
    then(factory.getServletContext()).should(ordered).addFilter("filterBean1", filter1);
    then(factory.getServletContext()).should(ordered).addFilter("filterBean2", filter2);
    then(factory.getRegisteredFilter(0).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
    then(factory.getRegisteredFilter(1).getRegistration()).should().addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*");
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) InOrder(org.mockito.InOrder) Filter(jakarta.servlet.Filter) Ordered(cn.taketoday.core.Ordered) Servlet(jakarta.servlet.Servlet) ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockServletWebServerFactory

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

the class ServletWebServerApplicationContextTests method stopOnClose.

@Test
void stopOnClose() {
    addWebServerFactoryBean();
    this.context.refresh();
    MockServletWebServerFactory factory = getWebServerFactory();
    this.context.close();
    then(factory.getWebServer()).should().stop();
}
Also used : MockServletWebServerFactory(cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletWebServerFactory (cn.taketoday.framework.web.servlet.server.MockServletWebServerFactory)18 Test (org.junit.jupiter.api.Test)14 Servlet (jakarta.servlet.Servlet)12 Ordered (cn.taketoday.core.Ordered)6 ServletContext (jakarta.servlet.ServletContext)6 InOrder (org.mockito.InOrder)6 Filter (jakarta.servlet.Filter)4 FilterRegistrationBean (cn.taketoday.framework.web.servlet.FilterRegistrationBean)2 MockServlet (cn.taketoday.framework.web.servlet.mock.MockServlet)2 GenericServlet (jakarta.servlet.GenericServlet)2