1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
64
65
66
67
68
69 return false;
70 }
71 };
72 filter.setName(NAME);
73
74 return filter;
75 }
76
77
78
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
101
102 @SuppressWarnings({"JavaDoc"})
103 @Test
104 public void testEnabled() throws Exception {
105
106
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 }