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              }
50  
51              @Provides
52              public ShiroModuleTest.MockRealm createRealm() {
53                  return mockRealm;
54              }
55          });
56          GuiceShiroFilter filter = injector.getInstance(GuiceShiroFilter.class);
57          return filter.getFilterChainResolver();
58      }
59  
60      @Test
61      public void testSimple() throws Exception {
62          FilterChainResolver resolver = setupResolver();
63          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
64          FilterChain chain = createNiceMock(FilterChain.class);
65          HttpServletRequest request = createMockRequest("/index.html");
66  
67          FilterChain resolved = resolver.getChain(request, response, chain);
68          assertNotNull(resolved);
69          verify(request);
70      }
71  
72      @Test
73      public void testWithConfig() throws Exception {
74          FilterChainResolver resolver = setupResolver();
75          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
76          FilterChain chain = createNiceMock(FilterChain.class);
77          HttpServletRequest request = createMockRequest("/index2.html");
78  
79          FilterChain resolved = resolver.getChain(request, response, chain);
80          assertNotNull(resolved);
81          verify(request);
82      }
83  
84      private HttpServletRequest createMockRequest(String path) {
85          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
86  
87          expect(request.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE)).andReturn(null).anyTimes();
88          expect(request.getContextPath()).andReturn("");
89          expect(request.getRequestURI()).andReturn(path);
90          replay(request);
91          return request;
92      }
93  
94  }