    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  package org.acegisecurity.ui.webapp;
    16
    17  import junit.framework.TestCase;
    18
    19  import org.acegisecurity.MockPortResolver;
    20  import org.acegisecurity.util.PortMapperImpl;
    21
    22  import org.springframework.mock.web.MockHttpServletRequest;
    23  import org.springframework.mock.web.MockHttpServletResponse;
    24
    25  import java.util.HashMap;
    26  import java.util.Map;
    27
    28
    29  /***
    30   * Tests {@link AuthenticationProcessingFilterEntryPoint}.
    31   *
    32   * @author Ben Alex
    33   * @author colin sampaleanu
    34   * @version $Id: AuthenticationProcessingFilterEntryPointTests.java,v 1.12 2005/11/17 00:56:10 benalex Exp $
    35   */
    36  public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
    37      public final void setUp() throws Exception {
    38          super.setUp();
    39      }
    40
    41      public static void main(String[] args) {
    42          junit.textui.TestRunner.run(AuthenticationProcessingFilterEntryPointTests.class);
    43      }
    44
    45      public void testDetectsMissingLoginFormUrl() throws Exception {
    46          AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    47          ep.setPortMapper(new PortMapperImpl());
    48          ep.setPortResolver(new MockPortResolver(80, 443));
    49
    50          try {
    51              ep.afterPropertiesSet();
    52              fail("Should have thrown IllegalArgumentException");
    53          } catch (IllegalArgumentException expected) {
    54              assertEquals("loginFormUrl must be specified", expected.getMessage());
    55          }
    56      }
    57
    58      public void testDetectsMissingPortMapper() throws Exception {
    59          AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    60          ep.setLoginFormUrl("xxx");
    61          ep.setPortMapper(null);
    62
    63          try {
    64              ep.afterPropertiesSet();
    65              fail("Should have thrown IllegalArgumentException");
    66          } catch (IllegalArgumentException expected) {
    67              assertEquals("portMapper must be specified", expected.getMessage());
    68          }
    69      }
    70
    71      public void testDetectsMissingPortResolver() throws Exception {
    72          AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    73          ep.setLoginFormUrl("xxx");
    74          ep.setPortResolver(null);
    75
    76          try {
    77              ep.afterPropertiesSet();
    78              fail("Should have thrown IllegalArgumentException");
    79          } catch (IllegalArgumentException expected) {
    80              assertEquals("portResolver must be specified", expected.getMessage());
    81          }
    82      }
    83
    84      public void testGettersSetters() {
    85          AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    86          ep.setLoginFormUrl("/hello");
    87          ep.setPortMapper(new PortMapperImpl());
    88          ep.setPortResolver(new MockPortResolver(8080, 8443));
    89          assertEquals("/hello", ep.getLoginFormUrl());
    90          assertTrue(ep.getPortMapper() != null);
    91          assertTrue(ep.getPortResolver() != null);
    92
    93          ep.setForceHttps(false);
    94          assertFalse(ep.getForceHttps());
    95          ep.setForceHttps(true);
    96          assertTrue(ep.getForceHttps());
    97      }
    98
    99      public void testHttpsOperationFromOriginalHttpUrl()
    100         throws Exception {
    101         MockHttpServletRequest request = new MockHttpServletRequest();
    102         request.setRequestURI("/some_path");
    103         request.setScheme("http");
    104         request.setServerName("www.example.com");
    105         request.setContextPath("/bigWebApp");
    106         request.setServerPort(80);
    107
    108         MockHttpServletResponse response = new MockHttpServletResponse();
    109
    110         AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    111         ep.setLoginFormUrl("/hello");
    112         ep.setPortMapper(new PortMapperImpl());
    113         ep.setForceHttps(true);
    114         ep.setPortMapper(new PortMapperImpl());
    115         ep.setPortResolver(new MockPortResolver(80, 443));
    116         ep.afterPropertiesSet();
    117
    118         ep.commence(request, response, null);
    119         assertEquals("https://www.example.com/bigWebApp/hello",
    120             response.getRedirectedUrl());
    121
    122         request.setServerPort(8080);
    123         response = new MockHttpServletResponse();
    124         ep.setPortResolver(new MockPortResolver(8080, 8443));
    125         ep.commence(request, response, null);
    126         assertEquals("https://www.example.com:8443/bigWebApp/hello",
    127             response.getRedirectedUrl());
    128
    129         // Now test an unusual custom HTTP:HTTPS is handled properly
    130         request.setServerPort(8888);
    131         response = new MockHttpServletResponse();
    132         ep.commence(request, response, null);
    133         assertEquals("https://www.example.com:8443/bigWebApp/hello",
    134             response.getRedirectedUrl());
    135
    136         PortMapperImpl portMapper = new PortMapperImpl();
    137         Map map = new HashMap();
    138         map.put("8888", "9999");
    139         portMapper.setPortMappings(map);
    140         response = new MockHttpServletResponse();
    141
    142         ep = new AuthenticationProcessingFilterEntryPoint();
    143         ep.setLoginFormUrl("/hello");
    144         ep.setPortMapper(new PortMapperImpl());
    145         ep.setForceHttps(true);
    146         ep.setPortMapper(portMapper);
    147         ep.setPortResolver(new MockPortResolver(8888, 9999));
    148         ep.afterPropertiesSet();
    149
    150         ep.commence(request, response, null);
    151         assertEquals("https://www.example.com:9999/bigWebApp/hello",
    152             response.getRedirectedUrl());
    153     }
    154
    155     public void testHttpsOperationFromOriginalHttpsUrl()
    156         throws Exception {
    157         MockHttpServletRequest request = new MockHttpServletRequest();
    158         request.setRequestURI("/some_path");
    159         request.setScheme("https");
    160         request.setServerName("www.example.com");
    161         request.setContextPath("/bigWebApp");
    162         request.setServerPort(443);
    163
    164         MockHttpServletResponse response = new MockHttpServletResponse();
    165
    166         AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    167         ep.setLoginFormUrl("/hello");
    168         ep.setPortMapper(new PortMapperImpl());
    169         ep.setForceHttps(true);
    170         ep.setPortMapper(new PortMapperImpl());
    171         ep.setPortResolver(new MockPortResolver(80, 443));
    172         ep.afterPropertiesSet();
    173
    174         ep.commence(request, response, null);
    175         assertEquals("/bigWebApp/hello", response.getRedirectedUrl());
    176
    177         request.setServerPort(8443);
    178         response = new MockHttpServletResponse();
    179         ep.setPortResolver(new MockPortResolver(8080, 8443));
    180         ep.commence(request, response, null);
    181         assertEquals("/bigWebApp/hello", response.getRedirectedUrl());
    182     }
    183
    184     public void testNormalOperation() throws Exception {
    185         AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    186         ep.setLoginFormUrl("/hello");
    187         ep.setPortMapper(new PortMapperImpl());
    188         ep.setPortResolver(new MockPortResolver(80, 443));
    189         ep.afterPropertiesSet();
    190
    191         MockHttpServletRequest request = new MockHttpServletRequest();
    192         request.setRequestURI("/some_path");
    193         request.setContextPath("/bigWebApp");
    194         request.setScheme("http");
    195         request.setServerName("www.example.com");
    196         request.setContextPath("/bigWebApp");
    197         request.setServerPort(80);
    198
    199         MockHttpServletResponse response = new MockHttpServletResponse();
    200
    201         ep.afterPropertiesSet();
    202         ep.commence(request, response, null);
    203         assertEquals("/bigWebApp/hello", response.getRedirectedUrl());
    204     }
    205
    206     public void testOperationWhenHttpsRequestsButHttpsPortUnknown()
    207         throws Exception {
    208         AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
    209         ep.setLoginFormUrl("/hello");
    210         ep.setPortMapper(new PortMapperImpl());
    211         ep.setPortResolver(new MockPortResolver(8888, 1234));
    212         ep.setForceHttps(true);
    213         ep.afterPropertiesSet();
    214
    215         MockHttpServletRequest request = new MockHttpServletRequest();
    216         request.setRequestURI("/some_path");
    217         request.setContextPath("/bigWebApp");
    218         request.setScheme("http");
    219         request.setServerName("www.example.com");
    220         request.setContextPath("/bigWebApp");
    221         request.setServerPort(8888); // NB: Port we can't resolve
    222
    223         MockHttpServletResponse response = new MockHttpServletResponse();
    224
    225         ep.afterPropertiesSet();
    226         ep.commence(request, response, null);
    227
    228         // Response doesn't switch to HTTPS, as we didn't know HTTP port 8888 to HTTP port mapping
    229         assertEquals("/bigWebApp/hello", response.getRedirectedUrl());
    230     }
    231 }