Search in sources :

Example 71 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class Spr8510Tests method annotationConfigWAC.

/**
 * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely.
 */
@Test
public void annotationConfigWAC() {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.scan("does.not.matter");
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    cll.contextInitialized(new ServletContextEvent(new MockServletContext()));
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(jakarta.servlet.ServletContextEvent) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 72 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class Spr8510Tests method abstractRefreshableWAC_fallsBackToInitParam.

/**
 * If setConfigLocation has not been called explicitly against the application context,
 * then fall back to the ContextLoaderListener init-param if present.
 */
@Test
public void abstractRefreshableWAC_fallsBackToInitParam() {
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    // ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically
    ContextLoaderListener cll = new ContextLoaderListener(ctx);
    MockServletContext sc = new MockServletContext();
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml");
    assertThatExceptionOfType(Throwable.class).isThrownBy(() -> cll.contextInitialized(new ServletContextEvent(sc))).withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]");
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 73 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class CharacterEncodingFilterTests method withBeanInitialization.

@Test
public void withBeanInitialization() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    given(request.getCharacterEncoding()).willReturn(null);
    given(request.getAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE)).willReturn(null);
    given(request.getAttribute(filteredName(FILTER_NAME))).willReturn(null);
    given(request.getDispatcherType()).willReturn(DispatcherType.REQUEST);
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding(ENCODING);
    filter.setBeanName(FILTER_NAME);
    filter.setServletContext(new MockServletContext());
    filter.doFilter(request, response, filterChain);
    verify(request).setCharacterEncoding(ENCODING);
    verify(request).setAttribute(filteredName(FILTER_NAME), Boolean.TRUE);
    verify(request).removeAttribute(filteredName(FILTER_NAME));
    verify(filterChain).doFilter(request, response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 74 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxy.

@Test
public void testDelegatingFilterProxy() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    MockFilterConfig proxyConfig = new MockFilterConfig(sc);
    proxyConfig.addInitParameter("targetBeanName", "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertThat(targetFilter.filterConfig).isNull();
    assertThat(request.getAttribute("called")).isEqualTo(Boolean.TRUE);
    filterProxy.destroy();
    assertThat(targetFilter.filterConfig).isNull();
}
Also used : 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) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Example 75 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanName.

@Test
public void testDelegatingFilterProxyWithTargetBeanName() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter");
    filterProxy.init(new MockFilterConfig(sc));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertThat(targetFilter.filterConfig).isNull();
    assertThat(request.getAttribute("called")).isEqualTo(Boolean.TRUE);
    filterProxy.destroy();
    assertThat(targetFilter.filterConfig).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7