Search in sources :

Example 61 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithFilterDelegateInstance.

@Test
public void testDelegatingFilterProxyWithFilterDelegateInstance() throws ServletException, IOException {
    MockFilter targetFilter = new MockFilter();
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(targetFilter);
    filterProxy.init(new MockFilterConfig(new MockServletContext()));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Example 62 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyNotInjectedWacServletAttrPreferred.

@Test
public void testDelegatingFilterProxyNotInjectedWacServletAttrPreferred() throws ServletException, IOException {
    ServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    sc.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    StaticWebApplicationContext wacToUse = new StaticWebApplicationContext();
    wacToUse.setServletContext(sc);
    String beanName = "targetFilter";
    String attrName = "customAttrName";
    wacToUse.registerSingleton(beanName, MockFilter.class);
    wacToUse.refresh();
    sc.setAttribute(attrName, wacToUse);
    MockFilter targetFilter = (MockFilter) wacToUse.getBean(beanName);
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy(beanName);
    filterProxy.setContextAttribute(attrName);
    filterProxy.setServletContext(sc);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 63 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithFilterName.

@Test
public void testDelegatingFilterProxyWithFilterName() 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, "targetFilter");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Example 64 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetFilterLifecycle.

@Test
public void testDelegatingFilterProxyWithTargetFilterLifecycle() 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");
    proxyConfig.addInitParameter("targetFilterLifecycle", "true");
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy();
    filterProxy.init(proxyConfig);
    assertEquals(proxyConfig, targetFilter.filterConfig);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    assertEquals(proxyConfig, targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Example 65 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext.

@Test
public void testDelegatingFilterProxyWithTargetBeanNameAndNotYetRefreshedApplicationContext() throws ServletException, IOException {
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.registerSingleton("targetFilter", MockFilter.class);
    // wac.refresh();
    // note that the context is not set as the ROOT attribute in the ServletContext!
    DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", wac);
    filterProxy.init(new MockFilterConfig(sc));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    filterProxy.doFilter(request, response, null);
    MockFilter targetFilter = (MockFilter) wac.getBean("targetFilter");
    assertNull(targetFilter.filterConfig);
    assertEquals(Boolean.TRUE, request.getAttribute("called"));
    filterProxy.destroy();
    assertNull(targetFilter.filterConfig);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)152 Test (org.junit.Test)120 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)56 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)50 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 ServletContext (javax.servlet.ServletContext)22 ServletContextEvent (javax.servlet.ServletContextEvent)21 HashMap (java.util.HashMap)18 Before (org.junit.Before)18 HttpServletResponse (javax.servlet.http.HttpServletResponse)16 MockFilterConfig (org.springframework.mock.web.test.MockFilterConfig)16 TestBean (org.springframework.tests.sample.beans.TestBean)15 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)14 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)13 View (org.springframework.web.servlet.View)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebApplicationContext (org.springframework.web.context.WebApplicationContext)12 Resource (org.springframework.core.io.Resource)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9