    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.securechannel;
    17
    18  import junit.framework.TestCase;
    19
    20
    21
    22  import org.acegisecurity.MockPortResolver;
    23
    24  import org.acegisecurity.util.PortMapperImpl;
    25
    26  import java.util.HashMap;
    27  import java.util.Map;
    28
    29  import org.springframework.mock.web.MockHttpServletRequest;
    30  import org.springframework.mock.web.MockHttpServletResponse;
    31
    32
    33  /***
    34   * Tests {@link RetryWithHttpEntryPoint}.
    35   *
    36   * @author Ben Alex
    37   * @version $Id: RetryWithHttpEntryPointTests.java,v 1.5 2005/11/17 00:55:48 benalex Exp $
    38   */
    39  public class RetryWithHttpEntryPointTests extends TestCase {
    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(RetryWithHttpEntryPointTests.class);
    48      }
    49
    50      public void testDetectsMissingPortMapper() throws Exception {
    51          RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    52          ep.setPortMapper(null);
    53
    54          try {
    55              ep.afterPropertiesSet();
    56              fail("Should have thrown IllegalArgumentException");
    57          } catch (IllegalArgumentException expected) {
    58              assertEquals("portMapper is required", expected.getMessage());
    59          }
    60      }
    61
    62      public void testDetectsMissingPortResolver() throws Exception {
    63          RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    64          ep.setPortResolver(null);
    65
    66          try {
    67              ep.afterPropertiesSet();
    68              fail("Should have thrown IllegalArgumentException");
    69          } catch (IllegalArgumentException expected) {
    70              assertEquals("portResolver is required", expected.getMessage());
    71          }
    72      }
    73
    74      public void testGettersSetters() {
    75          RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    76          ep.setPortMapper(new PortMapperImpl());
    77          ep.setPortResolver(new MockPortResolver(8080, 8443));
    78          assertTrue(ep.getPortMapper() != null);
    79          assertTrue(ep.getPortResolver() != null);
    80      }
    81
    82      public void testNormalOperation() throws Exception {
    83          MockHttpServletRequest request = new MockHttpServletRequest();
    84          request.setQueryString("open=true");
    85          request.setScheme("https");
    86          request.setServerName("www.example.com");
    87          request.setContextPath("/bigWebApp");
    88          request.setServletPath("/hello");
    89          request.setPathInfo("/pathInfo.html");
    90          request.setServerPort(443);
    91
    92          MockHttpServletResponse response = new MockHttpServletResponse();
    93
    94          RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    95          ep.setPortMapper(new PortMapperImpl());
    96          ep.setPortResolver(new MockPortResolver(80, 443));
    97          ep.afterPropertiesSet();
    98
    99          ep.commence(request, response);
    100         assertEquals("http://www.example.com/bigWebApp/hello/pathInfo.html?open=true",
    101             response.getRedirectedUrl());
    102     }
    103
    104     public void testNormalOperationWithNullPathInfoAndNullQueryString()
    105         throws Exception {
    106         MockHttpServletRequest request = new MockHttpServletRequest();
    107         request.setScheme("https");
    108         request.setServerName("www.example.com");
    109         request.setContextPath("/bigWebApp");
    110         request.setServletPath("/hello");
    111         request.setPathInfo(null);
    112         request.setServerPort(443);
    113
    114         MockHttpServletResponse response = new MockHttpServletResponse();
    115
    116         RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    117         ep.setPortMapper(new PortMapperImpl());
    118         ep.setPortResolver(new MockPortResolver(80, 443));
    119         ep.afterPropertiesSet();
    120
    121         ep.commence(request, response);
    122         assertEquals("http://www.example.com/bigWebApp/hello",
    123             response.getRedirectedUrl());
    124     }
    125
    126     public void testOperationWhenTargetPortIsUnknown()
    127         throws Exception {
    128         MockHttpServletRequest request = new MockHttpServletRequest();
    129         request.setQueryString("open=true");
    130         request.setScheme("https");
    131         request.setServerName("www.example.com");
    132         request.setContextPath("/bigWebApp");
    133         request.setServletPath("/hello");
    134         request.setPathInfo("/pathInfo.html");
    135         request.setServerPort(8768);
    136
    137         MockHttpServletResponse response = new MockHttpServletResponse();
    138
    139         RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    140         ep.setPortMapper(new PortMapperImpl());
    141         ep.setPortResolver(new MockPortResolver(8768, 1234));
    142         ep.afterPropertiesSet();
    143
    144         ep.commence(request, response);
    145         assertEquals("/bigWebApp", response.getRedirectedUrl());
    146     }
    147
    148     public void testOperationWithNonStandardPort() throws Exception {
    149         MockHttpServletRequest request = new MockHttpServletRequest();
    150         request.setQueryString("open=true");
    151         request.setScheme("https");
    152         request.setServerName("www.example.com");
    153         request.setContextPath("/bigWebApp");
    154         request.setServletPath("/hello");
    155         request.setPathInfo("/pathInfo.html");
    156         request.setServerPort(9999);
    157
    158         MockHttpServletResponse response = new MockHttpServletResponse();
    159
    160         PortMapperImpl portMapper = new PortMapperImpl();
    161         Map map = new HashMap();
    162         map.put("8888", "9999");
    163         portMapper.setPortMappings(map);
    164
    165         RetryWithHttpEntryPoint ep = new RetryWithHttpEntryPoint();
    166         ep.setPortResolver(new MockPortResolver(8888, 9999));
    167         ep.setPortMapper(portMapper);
    168         ep.afterPropertiesSet();
    169
    170         ep.commence(request, response);
    171         assertEquals("http://www.example.com:8888/bigWebApp/hello/pathInfo.html?open=true",
    172             response.getRedirectedUrl());
    173     }
    174 }