Search in sources :

Example 16 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenInsertFiltersEmptyThenException.

@Test
public void onStartupWhenInsertFiltersEmptyThenException() {
    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) {
            insertFilters(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 17 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenEnableHttpSessionEventPublisherIsTrueThenAddsHttpSessionEventPublisher.

@Test
public void onStartupWhenEnableHttpSessionEventPublisherIsTrueThenAddsHttpSessionEventPublisher() {
    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 boolean enableHttpSessionEventPublisher() {
            return true;
        }
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration).setAsyncSupported(true);
    verify(context).addListener(HttpSessionEventPublisher.class.getName());
}
Also used : HttpSessionEventPublisher(org.springframework.security.web.session.HttpSessionEventPublisher) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 18 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenNullFilterInsertedThenException.

@Test
public void onStartupWhenNullFilterInsertedThenException() {
    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) {
            insertFilters(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)

Example 19 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenDefaultContextThenRegistersSpringSecurityFilterChain.

@Test
public void onStartupWhenDefaultContextThenRegistersSpringSecurityFilterChain() {
    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() {
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration).setAsyncSupported(true);
    verifyNoAddListener(context);
}
Also used : ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 20 with ServletContext

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

the class FreeMarkerMacroTests method setup.

@BeforeEach
public void setup() throws Exception {
    ServletContext sc = new MockServletContext();
    wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    // final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
    fc.afterPropertiesSet();
    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();
    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}
Also used : FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ServletContext (jakarta.servlet.ServletContext)148 Test (org.junit.jupiter.api.Test)63 ServletConfig (jakarta.servlet.ServletConfig)40 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)20 ServletException (jakarta.servlet.ServletException)17 FilterRegistration (jakarta.servlet.FilterRegistration)16 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 Filter (jakarta.servlet.Filter)13 Test (org.junit.Test)13 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 WebApplicationContext (org.springframework.web.context.WebApplicationContext)9 File (java.io.File)8 Context (org.apache.catalina.Context)7 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)7