Bug 1390372: Add URIUtils.isPathEmpty. r=liuche

MozReview-Commit-ID: G8rq6NL54mZ

--HG--
extra : rebase_source : 0a3aa337545539437ff2a046216d8178bdabccca
This commit is contained in:
Michael Comella 2017-08-14 18:07:03 -07:00
Родитель c7e59bd618
Коммит bdfa67ee26
2 изменённых файлов: 45 добавлений и 1 удалений

Просмотреть файл

@ -11,18 +11,20 @@ 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. */
@ -35,6 +37,18 @@ public class URIUtils {
}
}
/**
* 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.
*

Просмотреть файл

@ -18,6 +18,36 @@ 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() {