1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Unit tests for the {@link ShiroFilterFactoryBean} implementation.
39   *
40   * @since 1.0
41   */
42  //@RunWith(SpringJUnit4ClassRunner.class)
43  //@ContextConfiguration(locations = {"/org/apache/shiro/spring/web/ShiroFilterFactoryBeanTest.xml"})
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       * Verifies fix for <a href="https://issues.apache.org/jira/browse/SHIRO-167">SHIRO-167</a>
67       *
68       * @throws Exception if there is any unexpected error
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                  //this line asserts the fix for the user-reported issue:
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 }