Bug 1265708 - Pre: Implement URLUtils.stripRefFromURL r?sebastian
MozReview-Commit-ID: AO25iiUMXLE
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/util/URLUtils.java
@@ -0,0 +1,33 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.util;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class URLUtils {
+
+ /**
+ * Strip the ref from a URL, if present
+ *
+ * @return The base URL, without the ref. The original String is returned if it has no ref,
+ * of if the input is malformed.
+ */
+ public static String stripRefFromURL(final String inputURL) {
+ try {
+ final URL url = new URL(inputURL);
+
+ final String ref = url.getRef();
+ if (ref != null) {
+ // Remove the ref (-ref.length()), and the # separator (-1).
+ return inputURL.substring(0, inputURL.length() - ref.length() - 1);
+ }
+ } catch (MalformedURLException e) {
+ // Ignore - we can't do much with a malformed URL
+ }
+ return inputURL;
+ }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -126,16 +126,17 @@ gujar.sources += ['java/org/mozilla/geck
'util/NetworkUtils.java',
'util/NonEvictingLruCache.java',
'util/PrefUtils.java',
'util/ProxySelector.java',
'util/RawResource.java',
'util/StringUtils.java',
'util/ThreadUtils.java',
'util/UIAsyncTask.java',
+ 'util/URLUtils.java',
'util/UUIDUtil.java',
'util/WeakReferenceHandler.java',
'util/WebActivityMapper.java',
'util/WindowUtils.java',
]]
gujar.extra_jars = [
CONFIG['ANDROID_SUPPORT_ANNOTATIONS_JAR_LIB'],
CONFIG['ANDROID_SUPPORT_V4_AAR_LIB'],
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestURLUtils.java
@@ -0,0 +1,44 @@
+package org.mozilla.gecko.util;
+
+import android.support.v4.util.Pair;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Tests for uuid utils.
+ */
+@RunWith(TestRunner.class)
+public class TestURLUtils {
+ private static final List<Pair<String, String>> URLS;
+
+ static {
+ URLS = new ArrayList<>();
+
+ URLS.add(new Pair<String, String>(null, null));
+ URLS.add(new Pair<>("", ""));
+ URLS.add(new Pair<>("??AAABBBCCC", "??AAABBBCCC"));
+
+ URLS.add(new Pair<>("https://mozilla.org", "https://mozilla.org"));
+ URLS.add(new Pair<>("https://mozilla.org#BBBB", "https://mozilla.org"));
+ URLS.add(new Pair<>("https://mozilla.org/#BBBB", "https://mozilla.org/"));
+ }
+
+ @Test
+ public void testURLS() {
+ for (Pair<String, String> urlPair : URLS) {
+ final String in = urlPair.first;
+ final String expected = urlPair.second;
+
+ final String out = URLUtils.stripRefFromURL(in);
+
+ assertEquals(expected, out);
+ }
+ }
+}