    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.intercept.web;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.acegisecurity.ConfigAttributeDefinition;
    21  import org.acegisecurity.MockFilterChain;
    22
    23
    24  import org.acegisecurity.SecurityConfig;
    25
    26  import org.springframework.mock.web.MockHttpServletRequest;
    27  import org.springframework.mock.web.MockHttpServletResponse;
    28
    29
    30  /***
    31   * Tests parts of {@link RegExpBasedFilterInvocationDefinitionMap} not tested
    32   * by {@link FilterInvocationDefinitionSourceEditorTests}.
    33   *
    34   * @author Ben Alex
    35   * @version $Id: RegExpBasedFilterDefinitionMapTests.java,v 1.5 2005/11/17 00:55:50 benalex Exp $
    36   */
    37  public class RegExpBasedFilterDefinitionMapTests extends TestCase {
    38      //~ Constructors ===========================================================
    39
    40      public RegExpBasedFilterDefinitionMapTests() {
    41          super();
    42      }
    43
    44      public RegExpBasedFilterDefinitionMapTests(String arg0) {
    45          super(arg0);
    46      }
    47
    48      //~ Methods ================================================================
    49
    50      public final void setUp() throws Exception {
    51          super.setUp();
    52      }
    53
    54      public static void main(String[] args) {
    55          junit.textui.TestRunner.run(RegExpBasedFilterDefinitionMapTests.class);
    56      }
    57
    58      public void testConvertUrlToLowercaseIsFalseByDefault() {
    59          RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
    60          assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
    61      }
    62
    63      public void testConvertUrlToLowercaseSetterRespected() {
    64          RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
    65          map.setConvertUrlToLowercaseBeforeComparison(true);
    66          assertTrue(map.isConvertUrlToLowercaseBeforeComparison());
    67      }
    68
    69      public void testLookupNotRequiringExactMatchSuccessIfNotMatching() {
    70          RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
    71          map.setConvertUrlToLowercaseBeforeComparison(true);
    72          assertTrue(map.isConvertUrlToLowercaseBeforeComparison());
    73
    74          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
    75          def.addConfigAttribute(new SecurityConfig("ROLE_ONE"));
    76          map.addSecureUrl("//A/secure/super.*//Z", def);
    77
    78          // Build a HTTP request
    79          MockHttpServletRequest request = new MockHttpServletRequest();
    80          request.setRequestURI(null);
    81          MockHttpServletRequest req = request;
    82          req.setServletPath("/SeCuRE/super/somefile.html");
    83
    84          FilterInvocation fi = new FilterInvocation(req,
    85                  new MockHttpServletResponse(), new MockFilterChain());
    86
    87          ConfigAttributeDefinition response = map.lookupAttributes(fi
    88                  .getRequestUrl());
    89          assertEquals(def, response);
    90      }
    91
    92      public void testLookupRequiringExactMatchFailsIfNotMatching() {
    93          RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
    94          assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
    95
    96          ConfigAttributeDefinition def = new ConfigAttributeDefinition();
    97          def.addConfigAttribute(new SecurityConfig("ROLE_ONE"));
    98          map.addSecureUrl("//A/secure/super.*//Z", def);
    99
    100         // Build a HTTP request
    101         MockHttpServletRequest request = new MockHttpServletRequest();
    102         request.setRequestURI(null);
    103         MockHttpServletRequest req = request;
    104         req.setServletPath("/SeCuRE/super/somefile.html");
    105
    106         FilterInvocation fi = new FilterInvocation(req,
    107                 new MockHttpServletResponse(), new MockFilterChain());
    108
    109         ConfigAttributeDefinition response = map.lookupAttributes(fi
    110                 .getRequestUrl());
    111         assertEquals(null, response);
    112     }
    113
    114     public void testLookupRequiringExactMatchIsSuccessful() {
    115         RegExpBasedFilterInvocationDefinitionMap map = new RegExpBasedFilterInvocationDefinitionMap();
    116         assertFalse(map.isConvertUrlToLowercaseBeforeComparison());
    117
    118         ConfigAttributeDefinition def = new ConfigAttributeDefinition();
    119         def.addConfigAttribute(new SecurityConfig("ROLE_ONE"));
    120         map.addSecureUrl("//A/secure/super.*//Z", def);
    121
    122         // Build a HTTP request
    123         MockHttpServletRequest request = new MockHttpServletRequest();
    124         request.setRequestURI(null);
    125         MockHttpServletRequest req = request;
    126         req.setServletPath("/secure/super/somefile.html");
    127
    128         FilterInvocation fi = new FilterInvocation(req,
    129                 new MockHttpServletResponse(), new MockFilterChain());
    130
    131         ConfigAttributeDefinition response = map.lookupAttributes(fi
    132                 .getRequestUrl());
    133         assertEquals(def, response);
    134     }
    135 }