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.filter.authz;
20  
21  import org.junit.Test;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import static org.easymock.EasyMock.*;
27  import static org.junit.Assert.*;
28  
29  /**
30   * Tests for the {@link PortFilter} class.
31   *
32   * @since 1.1
33   */
34  public class PortFilterTest {
35  
36      protected HttpServletRequest createBaseMockRequest() {
37          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
38          expect(request.getScheme()).andReturn("http");
39          expect(request.getServerName()).andReturn("localhost");
40          expect(request.getRequestURI()).andReturn("/");
41          return request;
42      }
43  
44      @Test
45      public void testDefault() throws Exception {
46          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
47          HttpServletRequest request = createBaseMockRequest();
48  
49          expect(response.encodeRedirectURL(eq("http://localhost/"))).andReturn("http://localhost/");
50          replay(request);
51          replay(response);
52  
53          PortFilter filter = new PortFilter();
54          boolean result = filter.onAccessDenied(request, response, null);
55  
56          verify(request);
57          verify(response);
58          assertFalse(result);
59      }
60  
61      /**
62       * This tests the case where the client (e.g. browser) specifies a simple request to http://localhost/
63       * (i.e. http scheme with the implied port of 80). The redirectURL should reflect the configured port (8080) instead
64       * of the implied port 80.
65       *
66       * @throws Exception if there is a test failure
67       */
68      @Test
69      public void testConfiguredPort() throws Exception {
70          int port = 8080;
71          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
72          HttpServletRequest request = createBaseMockRequest();
73  
74          String expected = "http://localhost:" + port + "/";
75          expect(response.encodeRedirectURL(eq(expected))).andReturn(expected);
76          replay(request);
77          replay(response);
78  
79          PortFilter filter = new PortFilter();
80          filter.setPort(port);
81          boolean result = filter.onAccessDenied(request, response, null);
82  
83          verify(request);
84          verify(response);
85          assertFalse(result);
86      }
87  
88  }