Search in sources :

Example 86 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenAppendFiltersEmptyThenException.

@Test
public void onStartupWhenAppendFiltersEmptyThenException() {
    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);
    assertThatIllegalArgumentException().isThrownBy(() -> new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            appendFilters(context);
        }
    }.onStartup(context)).withMessage("filters cannot be null or empty");
    assertProxyDefaults(proxyCaptor.getValue());
}
Also used : ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 87 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenInsertFiltersThenInserted.

@Test
public void onStartupWhenInsertFiltersThenInserted() {
    Filter filter1 = mock(Filter.class);
    Filter filter2 = 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(filter1))).willReturn(registration);
    given(context.addFilter(anyString(), eq(filter2))).willReturn(registration);
    new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            insertFilters(context, filter1, filter2);
        }
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration, times(3)).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration, times(3)).setAsyncSupported(true);
    verifyNoAddListener(context);
    verify(context).addFilter(anyString(), eq(filter1));
    verify(context).addFilter(anyString(), eq(filter2));
}
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 88 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenAppendFiltersThenAppended.

@Test
public void onStartupWhenAppendFiltersThenAppended() {
    Filter filter1 = mock(Filter.class);
    Filter filter2 = 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(filter1))).willReturn(registration);
    given(context.addFilter(anyString(), eq(filter2))).willReturn(registration);
    new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
            appendFilters(context, filter1, filter2);
        }
    }.onStartup(context);
    verify(registration, times(1)).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration, times(2)).addMappingForUrlPatterns(DEFAULT_DISPATCH, true, "/*");
    verify(registration, times(3)).setAsyncSupported(true);
    verifyNoAddListener(context);
    verify(context, times(3)).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)

Example 89 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenSpringSecurityFilterChainAlreadyRegisteredThenException.

@Test
public void onStartupWhenSpringSecurityFilterChainAlreadyRegisteredThenException() {
    ServletContext context = mock(ServletContext.class);
    assertThatIllegalStateException().isThrownBy(() -> new AbstractSecurityWebApplicationInitializer() {
    }.onStartup(context)).withMessage("Duplicate Filter registration for 'springSecurityFilterChain'. " + "Check to ensure the Filter is only configured once.");
}
Also used : ServletContext(jakarta.servlet.ServletContext) Test(org.junit.jupiter.api.Test)

Example 90 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenConfigurationClassThenAddsContextLoaderListener.

@Test
public void onStartupWhenConfigurationClassThenAddsContextLoaderListener() {
    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(MyRootConfiguration.class) {
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration).setAsyncSupported(true);
    verify(context).addListener(any(ContextLoaderListener.class));
}
Also used : ServletContext(jakarta.servlet.ServletContext) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) 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