1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.util;
17  
18  import junit.framework.TestCase;
19  import org.springframework.mock.web.MockHttpServletRequest;
20  
21  
22  
23  /***
24   * Tests {@link PortResolverImpl}.
25   *
26   * @author Ben Alex
27   * @version $Id: PortResolverImplTests.java,v 1.5 2005/11/17 00:56:08 benalex Exp $
28   */
29  public class PortResolverImplTests extends TestCase {
30      //~ Constructors ===========================================================
31  
32      public PortResolverImplTests() {
33          super();
34      }
35  
36      public PortResolverImplTests(String arg0) {
37          super(arg0);
38      }
39  
40      //~ Methods ================================================================
41  
42      public final void setUp() throws Exception {
43          super.setUp();
44      }
45  
46      public static void main(String[] args) {
47          junit.textui.TestRunner.run(PortResolverImplTests.class);
48      }
49  
50      public void testDetectsBuggyIeHttpRequest() throws Exception {
51          PortResolverImpl pr = new PortResolverImpl();
52          pr.afterPropertiesSet();
53  
54          MockHttpServletRequest request = new MockHttpServletRequest();
55          request.setServerPort(8443);
56          request.setScheme("HTtP"); // proves case insensitive handling
57          assertEquals(8080, pr.getServerPort(request));
58      }
59  
60      public void testDetectsBuggyIeHttpsRequest() throws Exception {
61          PortResolverImpl pr = new PortResolverImpl();
62          pr.afterPropertiesSet();
63  
64          MockHttpServletRequest request = new MockHttpServletRequest();
65          request.setServerPort(8080);
66          request.setScheme("HTtPs"); // proves case insensitive handling
67          assertEquals(8443, pr.getServerPort(request));
68      }
69  
70      public void testDetectsEmptyPortMapper() throws Exception {
71          PortResolverImpl pr = new PortResolverImpl();
72          pr.setPortMapper(null);
73  
74          try {
75              pr.afterPropertiesSet();
76              fail("Should have thrown IllegalArgumentException");
77          } catch (IllegalArgumentException expected) {
78              assertTrue(true);
79          }
80      }
81  
82      public void testGettersSetters() throws Exception {
83          PortResolverImpl pr = new PortResolverImpl();
84          assertTrue(pr.getPortMapper() != null);
85          pr.setPortMapper(new PortMapperImpl());
86          assertTrue(pr.getPortMapper() != null);
87      }
88  
89      public void testNormalOperation() throws Exception {
90          PortResolverImpl pr = new PortResolverImpl();
91          pr.afterPropertiesSet();
92  
93          MockHttpServletRequest request = new MockHttpServletRequest();
94          request.setScheme("http");
95          request.setServerPort(1021);
96          assertEquals(1021, pr.getServerPort(request));
97      }
98  }