commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1128396 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/UrlValidator.java test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
Date Fri, 27 May 2011 17:04:57 GMT
Author: nick
Date: Fri May 27 17:04:57 2011
New Revision: 1128396

URL: http://svn.apache.org/viewvc?rev=1128396&view=rev
Log:
VALIDATOR-288 - Add a new option ALLOW_LOCAL_URLS to UrlValidator, which if enabled requests
the domain validator accept local names. When set, allows http://localhost/ and http://machinename/
to validate (not allowed if the option is unset)

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java?rev=1128396&r1=1128395&r2=1128396&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java
Fri May 27 17:04:57 2011
@@ -87,6 +87,11 @@ public class UrlValidator implements Ser
      * Enabling this options disallows any URL fragments.
      */
     public static final long NO_FRAGMENTS = 1 << 2;
+    
+    /**
+     * Allow local URLs, such as http://localhost/ or http://machine/ 
+     */
+    public static final long ALLOW_LOCAL_URLS = 1 << 3;
 
     // Drop numeric, and  "+-." for now
     private static final String AUTHORITY_CHARS_REGEX = "\\p{Alnum}\\-\\.";
@@ -357,7 +362,7 @@ public class UrlValidator implements Ser
         String hostLocation = authorityMatcher.group(PARSE_AUTHORITY_HOST_IP);
         // check if authority is hostname or IP address:
         // try a hostname first since that's much more likely
-        DomainValidator domainValidator = DomainValidator.getInstance();
+        DomainValidator domainValidator = DomainValidator.getInstance(isOn(ALLOW_LOCAL_URLS));
         if (!domainValidator.isValid(hostLocation)) {
             // try an IP address
             InetAddressValidator inetAddressValidator =

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java?rev=1128396&r1=1128395&r2=1128396&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java
Fri May 27 17:04:57 2011
@@ -160,8 +160,54 @@ public class UrlValidatorTest extends Te
         assertTrue("www.apache.org should still validate",
                 validator.isValid("http://www.apache.org/test/index.html"));
 
+        // Now check using options
+        validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
+        
+        assertTrue("localhost URL should validate",
+              validator.isValid("http://localhost/test/index.html"));
+        
+        assertTrue("machinename URL should validate",
+              validator.isValid("http://machinename/test/index.html"));
+        
+        assertTrue("www.apache.org should still validate",
+              validator.isValid("http://www.apache.org/test/index.html"));
+    }
+
+    public void testValidator288() {
+        UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
+
+        assertTrue("hostname should validate",
+                validator.isValid("http://hostname"));
+
+        assertTrue("hostname with path should validate",
+                validator.isValid("http://hostname/test/index.html"));
+        
+        assertTrue("localhost URL should validate",
+                validator.isValid("http://localhost/test/index.html"));
+        
+        assertFalse("first.my-testing should not validate",
+                validator.isValid("http://first.my-testing/test/index.html"));
+
+        assertFalse("broke.hostname should not validate",
+                validator.isValid("http://broke.hostname/test/index.html"));
+
+        assertTrue("www.apache.org should still validate",
+                validator.isValid("http://www.apache.org/test/index.html"));
+
+        // Turn it off, and check
+        validator = new UrlValidator(0);
+
+        assertFalse("hostname should no longer validate",
+                validator.isValid("http://hostname"));
+
+        assertFalse("localhost URL should no longer validate",
+                validator.isValid("http://localhost/test/index.html"));
+        
+        assertTrue("www.apache.org should still validate",
+                validator.isValid("http://www.apache.org/test/index.html"));
     }
 
+    
    static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
       boolean carry = true;  //add 1 to lowest order part.
       boolean maxIndex = true;



Mime
View raw message