Bug 1390372: Add URIUtils.isPathEmpty. r=liuche draft
authorMichael Comella <michael.l.comella@gmail.com>
Mon, 14 Aug 2017 18:07:03 -0700
changeset 646253 0a3aa337545539437ff2a046216d8178bdabccca
parent 646234 1f6579c74d1e8a9dede0ce07d739d4bbe2ddac83
child 646254 c090095bf3aa692c60afcb1341d4e151c0b77736
push id74041
push usermichael.l.comella@gmail.com
push dateTue, 15 Aug 2017 01:58:29 +0000
reviewersliuche
bugs1390372
milestone57.0a1
Bug 1390372: Add URIUtils.isPathEmpty. r=liuche MozReview-Commit-ID: G8rq6NL54mZ
mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestURIUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/URIUtils.java
@@ -6,41 +6,55 @@ package org.mozilla.gecko.util;
 
 import android.content.Context;
 import android.os.AsyncTask;
 import android.support.annotation.IntRange;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.annotation.WorkerThread;
 import android.text.TextUtils;
-import android.util.Log;
 import ch.boye.httpclientandroidlib.conn.util.InetAddressUtils;
 import org.mozilla.gecko.util.publicsuffix.PublicSuffix;
 
 import java.lang.ref.WeakReference;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.regex.Pattern;
 
 /** Utilities for operating on URLs. */
 public class URIUtils {
     private static final String LOGTAG = "GeckoURIUtils";
 
+    private static final Pattern EMPTY_PATH = Pattern.compile("/*");
+
     private URIUtils() {}
 
     /** @return a {@link URI} if possible, else null. */
     @Nullable
     public static URI uriOrNull(final String uriString) {
         try {
             return new URI(uriString);
         } catch (final URISyntaxException e) {
             return null;
         }
     }
 
     /**
+     * Returns true if {@link URI#getPath()} is not empty, false otherwise where empty means the given path contains
+     * characters other than "/".
+     *
+     * This is necessary because the URI method will return "/" for "http://google.com/".
+     */
+    public static boolean isPathEmpty(@NonNull final URI uri) {
+        final String path = uri.getPath();
+        return TextUtils.isEmpty(path) || EMPTY_PATH.matcher(path).matches();
+
+    }
+
+    /**
      * Returns the domain for the given URI, formatted by the other available parameters.
      *
      * A public suffix is a top-level domain. For the input, "https://github.com", you can specify
      * {@code shouldIncludePublicSuffix}:
      * - true: "github.com"
      * - false: "github"
      *
      * The subdomain count is the number of subdomains you want to include; the domain will always be included. For
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestURIUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestURIUtils.java
@@ -13,16 +13,46 @@ import org.robolectric.RuntimeEnvironmen
 import java.net.URI;
 import java.net.URISyntaxException;
 
 @RunWith(TestRunner.class)
 public class TestURIUtils {
 
     private final String BUGZILLA_URL = "https://bugzilla.mozilla.org/enter_bug.cgi?format=guided#h=dupes%7CData%20%26%20BI%20Services%20Team%7C";
 
+    @Test
+    public void testIsPathEmptyWithURINoPath() throws Exception {
+        final URI uri = new URI("https://google.com");
+        Assert.assertTrue(URIUtils.isPathEmpty(uri));
+    }
+
+    @Test
+    public void testIsPathEmptyWithURISlashPath() throws Exception {
+        final URI uri = new URI("http://google.com/");
+        Assert.assertTrue(URIUtils.isPathEmpty(uri));
+    }
+
+    @Test
+    public void testIsPathEmptyWithURIDoubleSlashPath() throws Exception {
+        final URI uri = new URI("http://google.com//");
+        Assert.assertTrue(URIUtils.isPathEmpty(uri));
+    }
+
+    @Test
+    public void testIsPathEmptyWithURIEncodedSpaceSlashPath() throws Exception {
+        final URI uri = new URI("http://google.com/%20/");
+        Assert.assertFalse(URIUtils.isPathEmpty(uri));
+    }
+
+    @Test
+    public void testIsPathEmptyWithURIPath() throws Exception {
+        final URI uri = new URI("http://google.com/search/whatever/");
+        Assert.assertFalse(URIUtils.isPathEmpty(uri));
+    }
+
     // --- getFormattedDomain, include PublicSuffix --- //
     @Test
     public void testGetFormattedDomainWithSuffix0Parts() {
         final boolean includePublicSuffix = true;
         final int subdomainCount = 0;
         assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google.com");
         assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "example.com");
         assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "foo.com");