View Javadoc
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.servlet;
20  
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  import javax.servlet.FilterChain;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import java.io.IOException;
29  
30  import static org.easymock.EasyMock.*;
31  import static org.junit.Assert.assertFalse;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Unit tests for the {@link OncePerRequestFilter} implementation.
36   *
37   * @since 1.2
38   */
39  public class OncePerRequestFilterTest {
40  
41      private static final boolean[] FILTERED = new boolean[1];
42      private static final String NAME = "oncePerRequestFilter";
43      private static final String ATTR_NAME = NAME + OncePerRequestFilter.ALREADY_FILTERED_SUFFIX;
44  
45      private OncePerRequestFilter filter;
46      private FilterChain chain;
47      private ServletRequest request;
48      private ServletResponse response;
49  
50      @Before
51      public void setUp() {
52          FILTERED[0] = false;
53          filter = createTestInstance();
54          chain = createNiceMock(FilterChain.class);
55          request = createNiceMock(ServletRequest.class);
56          response = createNiceMock(ServletResponse.class);
57      }
58  
59      private OncePerRequestFilter createTestInstance() {
60          OncePerRequestFilter filter = new OncePerRequestFilter() {
61              @Override
62              protected void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
63                      throws ServletException, IOException {
64                  FILTERED[0] = true;
65              }
66          };
67          filter.setName(NAME);
68  
69          return filter;
70      }
71  
72      /**
73       * Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-221">SHIRO-221<a/>.
74       */
75      @SuppressWarnings({"JavaDoc"})
76      @Test
77      public void testEnabled() throws IOException, ServletException {
78          expect(request.getAttribute(ATTR_NAME)).andReturn(null).anyTimes();
79          replay(request);
80  
81          filter.doFilter(request, response, chain);
82  
83          verify(request);
84          assertTrue("Filter should have executed", FILTERED[0]);
85      }
86  
87      /**
88       * Test asserting <a href="https://issues.apache.org/jira/browse/SHIRO-221">SHIRO-221<a/>.
89       */
90      @SuppressWarnings({"JavaDoc"})
91      @Test
92      public void testDisabled() throws IOException, ServletException {
93          filter.setEnabled(false); //test disabled
94  
95          expect(request.getAttribute(ATTR_NAME)).andReturn(null).anyTimes();
96          replay(request);
97  
98          filter.doFilter(request, response, chain);
99  
100         verify(request);
101         assertFalse("Filter should NOT have executed", FILTERED[0]);
102     }
103 
104 }