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.guice.web;
20  
21  import com.google.inject.Guice;
22  import com.google.inject.Injector;
23  import com.google.inject.Provides;
24  import org.apache.shiro.guice.ShiroModuleTest;
25  import org.apache.shiro.web.filter.mgt.FilterChainResolver;
26  import org.apache.shiro.web.util.WebUtils;
27  import org.junit.Test;
28  
29  import javax.servlet.FilterChain;
30  import javax.servlet.ServletContext;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  import static org.easymock.EasyMock.*;
35  import static org.junit.Assert.assertNotNull;
36  
37  public class FilterConfigTest {
38      private FilterChainResolver setupResolver() {
39          final ShiroModuleTest.MockRealm mockRealm = createMock(ShiroModuleTest.MockRealm.class);
40          ServletContext servletContext = createMock(ServletContext.class);
41  
42          Injector injector = Guice.createInjector(new ShiroWebModule(servletContext) {
43              @Override
44              protected void configureShiroWeb() {
45                  bindRealm().to(ShiroModuleTest.MockRealm.class);
46  
47                  addFilterChain("/index.html", AUTHC_BASIC);
48  //                addFilterChain("/index2.html", config(PERMS, "permission"));
49                  addFilterChain("/index2.html", filterConfig(PERMS, "permission"));
50              }
51  
52              @Provides
53              public ShiroModuleTest.MockRealm createRealm() {
54                  return mockRealm;
55              }
56          });
57          GuiceShiroFilter filter = injector.getInstance(GuiceShiroFilter.class);
58          return filter.getFilterChainResolver();
59      }
60  
61      @Test
62      public void testSimple() throws Exception {
63          FilterChainResolver resolver = setupResolver();
64          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
65          FilterChain chain = createNiceMock(FilterChain.class);
66          HttpServletRequest request = createMockRequest("/index.html");
67  
68          FilterChain resolved = resolver.getChain(request, response, chain);
69          assertNotNull(resolved);
70          verify(request);
71      }
72  
73      @Test
74      public void testWithConfig() throws Exception {
75          FilterChainResolver resolver = setupResolver();
76          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
77          FilterChain chain = createNiceMock(FilterChain.class);
78          HttpServletRequest request = createMockRequest("/index2.html");
79  
80          FilterChain resolved = resolver.getChain(request, response, chain);
81          assertNotNull(resolved);
82          verify(request);
83      }
84  
85      private HttpServletRequest createMockRequest(String path) {
86          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
87  
88          expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
89          expect(request.getContextPath()).andReturn("");
90          expect(request.getRequestURI()).andReturn(path);
91          replay(request);
92          return request;
93      }
94  
95  }