use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class AbstractServletWebServerFactoryTests method sessionConfiguration.
@Test
void sessionConfiguration() {
AbstractServletWebServerFactory factory = getFactory();
factory.getSession().setTimeout(Duration.ofSeconds(123));
factory.getSession().setTrackingModes(EnumSet.of(SessionTrackingMode.COOKIE, SessionTrackingMode.URL));
factory.getSession().getCookie().setName("testname");
factory.getSession().getCookie().setDomain("testdomain");
factory.getSession().getCookie().setPath("/testpath");
factory.getSession().getCookie().setComment("testcomment");
factory.getSession().getCookie().setHttpOnly(true);
factory.getSession().getCookie().setSecure(true);
factory.getSession().getCookie().setMaxAge(Duration.ofMinutes(1));
AtomicReference<ServletContext> contextReference = new AtomicReference<>();
factory.getWebServer(contextReference::set).start();
ServletContext servletContext = contextReference.get();
assertThat(servletContext.getEffectiveSessionTrackingModes()).isEqualTo(EnumSet.of(jakarta.servlet.SessionTrackingMode.COOKIE, jakarta.servlet.SessionTrackingMode.URL));
assertThat(servletContext.getSessionCookieConfig().getName()).isEqualTo("testname");
assertThat(servletContext.getSessionCookieConfig().getDomain()).isEqualTo("testdomain");
assertThat(servletContext.getSessionCookieConfig().getPath()).isEqualTo("/testpath");
assertThat(servletContext.getSessionCookieConfig().getComment()).isEqualTo("testcomment");
assertThat(servletContext.getSessionCookieConfig().isHttpOnly()).isTrue();
assertThat(servletContext.getSessionCookieConfig().isSecure()).isTrue();
assertThat(servletContext.getSessionCookieConfig().getMaxAge()).isEqualTo(60);
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class AbstractServletWebServerFactoryTests method servletContextListenerContextDestroyedIsCalledWhenContainerIsStopped.
@Test
void servletContextListenerContextDestroyedIsCalledWhenContainerIsStopped() throws Exception {
ServletContextListener listener = mock(ServletContextListener.class);
this.webServer = getFactory().getWebServer((servletContext) -> servletContext.addListener(listener));
this.webServer.start();
this.webServer.stop();
then(listener).should().contextDestroyed(any(ServletContextEvent.class));
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
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/");
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method servletContextInitializerBeansSkipsRegisteredServletsAndFilters.
@Test
void servletContextInitializerBeansSkipsRegisteredServletsAndFilters() {
addWebServerFactoryBean();
Servlet servlet = mock(Servlet.class);
Filter filter = mock(Filter.class);
ServletRegistrationBean<Servlet> initializer = new ServletRegistrationBean<>(servlet, "/foo");
this.context.registerBeanDefinition("initializerBean", beanDefinition(initializer));
this.context.registerBeanDefinition("servletBean", beanDefinition(servlet));
this.context.registerBeanDefinition("filterBean", beanDefinition(filter));
this.context.refresh();
ServletContext servletContext = getWebServerFactory().getServletContext();
then(servletContext).should(atMost(1)).addServlet(anyString(), any(Servlet.class));
then(servletContext).should(atMost(1)).addFilter(anyString(), any(Filter.class));
}
use of jakarta.servlet.ServletContext in project spring-boot by spring-projects.
the class ServletWebServerApplicationContextTests method multipleServletBeans.
@Test
void multipleServletBeans() {
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("servletBean1", beanDefinition(servlet1));
this.context.refresh();
MockServletWebServerFactory factory = getWebServerFactory();
ServletContext servletContext = factory.getServletContext();
InOrder ordered = inOrder(servletContext);
then(servletContext).should(ordered).addServlet("servletBean1", servlet1);
then(servletContext).should(ordered).addServlet("servletBean2", servlet2);
then(factory.getRegisteredServlet(0).getRegistration()).should().addMapping("/servletBean1/");
then(factory.getRegisteredServlet(1).getRegistration()).should().addMapping("/servletBean2/");
}
Aggregations