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.web.filter;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import javax.servlet.ServletRequest;
25  import javax.servlet.ServletResponse;
26  import javax.servlet.http.HttpServletRequest;
27  
28  import static org.easymock.EasyMock.*;
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * Unit tests for the {@link PathMatchingFilter} implementation.
34   */
35  public class PathMatchingFilterTest {
36  
37      private static final String CONTEXT_PATH = "/";
38      private static final String ENABLED_PATH = CONTEXT_PATH + "enabled";
39      private static final String DISABLED_PATH = CONTEXT_PATH + "disabled";
40  
41      HttpServletRequest request;
42      ServletResponse response;
43      PathMatchingFilter filter;
44  
45      @Before
46      public void setUp() {
47          request = createNiceMock(HttpServletRequest.class);
48          response = createNiceMock(ServletResponse.class);
49          filter = createTestInstance();
50      }
51  
52      private PathMatchingFilter createTestInstance() {
53          final String NAME = "pathMatchingFilter";
54  
55          PathMatchingFilter filter = new PathMatchingFilter() {
56              @Override
57              protected boolean isEnabled(ServletRequest request, ServletResponse response, String path, Object mappedValue) throws Exception {
58                  return !path.equals(DISABLED_PATH);
59              }
60  
61              @Override
62              protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
63                  //simulate a subclass that handles the response itself (A 'false' return value indicates that the
64                  //FilterChain should not continue to be executed)
65                  //
66                  //This method should only be called if the filter is enabled, so we know if the return value is
67                  //false, then the filter was enabled.  A true return value from 'onPreHandle' indicates this test
68                  //filter was disabled or a path wasn't matched.
69                  return false;
70              }
71          };
72          filter.setName(NAME);
73  
74          return filter;
75      }
76  
77      /**
78       * Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-221">SHIRO-221<a/>.
79       */
80      @SuppressWarnings({"JavaDoc"})
81      @Test
82      public void testDisabledBasedOnPath() throws Exception {
83          filter.processPathConfig(DISABLED_PATH, null);
84  
85          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
86          ServletResponse response = createNiceMock(ServletResponse.class);
87  
88          expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
89          expect(request.getRequestURI()).andReturn(DISABLED_PATH).anyTimes();
90          replay(request);
91  
92          boolean continueFilterChain = filter.preHandle(request, response);
93  
94          assertTrue("FilterChain should continue.", continueFilterChain);
95  
96          verify(request);
97      }
98  
99      /**
100      * Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-221">SHIRO-221<a/>.
101      */
102     @SuppressWarnings({"JavaDoc"})
103     @Test
104     public void testEnabled() throws Exception {
105         //Configure the filter to reflect 2 configured paths.  This test will simulate a request to the
106         //enabled path
107         filter.processPathConfig(DISABLED_PATH, null);
108         filter.processPathConfig(ENABLED_PATH, null);
109 
110         HttpServletRequest request = createNiceMock(HttpServletRequest.class);
111         ServletResponse response = createNiceMock(ServletResponse.class);
112 
113         expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
114         expect(request.getRequestURI()).andReturn(ENABLED_PATH).anyTimes();
115         replay(request);
116 
117         boolean continueFilterChain = filter.preHandle(request, response);
118 
119         assertFalse("FilterChain should NOT continue.", continueFilterChain);
120 
121         verify(request);
122     }
123 
124 
125 }