Search in sources :

Example 91 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenDefaultsThenSessionTrackingModes.

@Test
public void onStartupWhenDefaultsThenSessionTrackingModes() {
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    ArgumentCaptor<Set<SessionTrackingMode>> modesCaptor = ArgumentCaptor.forClass(new HashSet<SessionTrackingMode>() {
    }.getClass());
    willDoNothing().given(context).setSessionTrackingModes(modesCaptor.capture());
    new AbstractSecurityWebApplicationInitializer() {
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    Set<SessionTrackingMode> modes = modesCaptor.getValue();
    assertThat(modes).hasSize(1);
    assertThat(modes).containsExactly(SessionTrackingMode.COOKIE);
}
Also used : HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) SessionTrackingMode(jakarta.servlet.SessionTrackingMode) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 92 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenCustomSecurityDispatcherTypesThenUses.

@Test
public void onStartupWhenCustomSecurityDispatcherTypesThenUses() {
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected EnumSet<DispatcherType> getSecurityDispatcherTypes() {
            return EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.FORWARD);
        }
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.FORWARD), false, "/*");
    verify(registration).setAsyncSupported(true);
    verifyNoAddListener(context);
}
Also used : EnumSet(java.util.EnumSet) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 93 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenDuplicateFilterInsertedThenException.

@Test
public void onStartupWhenDuplicateFilterInsertedThenException() {
    Filter filter1 = mock(Filter.class);
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    assertThatIllegalStateException().isThrownBy(() -> new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            insertFilters(context, filter1);
        }
    }.onStartup(context)).withMessage("Duplicate Filter registration for 'object'. Check to ensure the Filter is only configured once.");
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(context).addFilter(anyString(), eq(filter1));
}
Also used : Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 94 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenDuplicateFilterAppendedThenException.

@Test
public void onStartupWhenDuplicateFilterAppendedThenException() {
    Filter filter1 = mock(Filter.class);
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    assertThatIllegalStateException().isThrownBy(() -> new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            appendFilters(context, filter1);
        }
    }.onStartup(context)).withMessage("Duplicate Filter registration for 'object'. " + "Check to ensure the Filter is only configured once.");
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(context).addFilter(anyString(), eq(filter1));
}
Also used : Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 95 with ServletContext

use of jakarta.servlet.ServletContext in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenNullFilterAppendedThenException.

@Test
public void onStartupWhenNullFilterAppendedThenException() {
    Filter filter = mock(Filter.class);
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    given(context.addFilter(anyString(), eq(filter))).willReturn(registration);
    assertThatIllegalArgumentException().isThrownBy(() -> new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            appendFilters(context, filter, null);
        }
    }.onStartup(context)).withMessageContaining("filters cannot contain null values");
    verify(context, times(2)).addFilter(anyString(), any(Filter.class));
}
Also used : Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContext (jakarta.servlet.ServletContext)116 Test (org.junit.jupiter.api.Test)45 ServletConfig (jakarta.servlet.ServletConfig)34 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)17 FilterRegistration (jakarta.servlet.FilterRegistration)15 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 ServletException (jakarta.servlet.ServletException)12 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 Filter (jakarta.servlet.Filter)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 Context (org.apache.catalina.Context)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 Test (org.junit.Test)6