1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.spring.web;
20
21 import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
22 import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
23 import org.apache.shiro.web.filter.mgt.NamedFilterList;
24 import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
25 import org.apache.shiro.web.servlet.AbstractShiroFilter;
26 import org.junit.Test;
27 import org.springframework.context.support.ClassPathXmlApplicationContext;
28
29 import javax.servlet.*;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import java.io.IOException;
33
34 import static org.easymock.EasyMock.*;
35 import static org.junit.Assert.*;
36
37
38
39
40
41
42
43
44 public class ShiroFilterFactoryBeanTest {
45
46 @Test
47 public void testFilterDefinition() {
48
49 ClassPathXmlApplicationContext context =
50 new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
51
52 AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
53
54 PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver();
55 DefaultFilterChainManager fcManager = (DefaultFilterChainManager) resolver.getFilterChainManager();
56 NamedFilterList chain = fcManager.getChain("/test");
57 assertNotNull(chain);
58 assertEquals(chain.size(), 2);
59 Filter[] filters = new Filter[chain.size()];
60 filters = chain.toArray(filters);
61 assertTrue(filters[0] instanceof DummyFilter);
62 assertTrue(filters[1] instanceof FormAuthenticationFilter);
63 }
64
65
66
67
68
69
70 @Test
71 public void testFilterDefinitionWithInit() throws Exception {
72
73 ClassPathXmlApplicationContext context =
74 new ClassPathXmlApplicationContext("org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml");
75
76 AbstractShiroFilter shiroFilter = (AbstractShiroFilter) context.getBean("shiroFilter");
77
78 FilterConfig mockFilterConfig = createNiceMock(FilterConfig.class);
79 ServletContext mockServletContext = createNiceMock(ServletContext.class);
80 expect(mockFilterConfig.getServletContext()).andReturn(mockServletContext).anyTimes();
81 HttpServletRequest mockRequest = createNiceMock(HttpServletRequest.class);
82 expect(mockRequest.getContextPath()).andReturn("/").anyTimes();
83 expect(mockRequest.getRequestURI()).andReturn("/").anyTimes();
84 HttpServletResponse mockResponse = createNiceMock(HttpServletResponse.class);
85
86 replay(mockFilterConfig);
87 replay(mockServletContext);
88 shiroFilter.init(mockFilterConfig);
89 verify(mockServletContext);
90 verify(mockFilterConfig);
91
92 FilterChain filterChain = new FilterChain() {
93 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
94 HttpServletRequest request = (HttpServletRequest) servletRequest;
95 assertNotNull(request.getSession());
96
97 assertNotNull(request.getSession().getServletContext());
98 }
99 };
100
101 replay(mockRequest);
102 replay(mockResponse);
103
104 shiroFilter.doFilter(mockRequest, mockResponse, filterChain);
105
106 verify(mockResponse);
107 verify(mockRequest);
108 }
109 }