diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index 7b0bb6beeca7..8174d7004c61 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -2,6 +2,9 @@ import android.test.InstrumentationTestCase; +import java.util.HashMap; +import java.util.Map; + public class UrlUtilsTest extends InstrumentationTestCase { public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() { assertNotNull(UrlUtils.getDomainFromUrl("")); @@ -17,4 +20,55 @@ public void testGetDomainFromUrlWithHostReturnsHost() { assertTrue(host.equals("www.wordpress.com")); } + + public void testAppendUrlParameter1() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test", "preview", "true"); + assertEquals("http://wp.com/test?preview=true", url); + } + + public void testAppendUrlParameter2() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony", "preview", "true"); + assertEquals("http://wp.com/test?q=pony&preview=true", url); + } + + public void testAppendUrlParameter3() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony#unicorn", "preview", "true"); + assertEquals("http://wp.com/test?q=pony&preview=true#unicorn", url); + } + + public void testAppendUrlParameter4() { + String url = UrlUtils.appendUrlParameter("/relative/test", "preview", "true"); + assertEquals("/relative/test?preview=true", url); + } + + public void testAppendUrlParameter5() { + String url = UrlUtils.appendUrlParameter("/relative/", "preview", "true"); + assertEquals("/relative/?preview=true", url); + } + + public void testAppendUrlParameter6() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test/", "preview", "true"); + assertEquals("http://wp.com/test/?preview=true", url); + } + + public void testAppendUrlParameter7() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test/?q=pony", "preview", "true"); + assertEquals("http://wp.com/test/?q=pony&preview=true", url); + } + + public void testAppendUrlParameters1() { + Map params = new HashMap<>(); + params.put("w", "200"); + params.put("h", "300"); + String url = UrlUtils.appendUrlParameters("http://wp.com/test", params); + assertEquals("http://wp.com/test?h=300&w=200", url); + } + + public void testAppendUrlParameters2() { + Map params = new HashMap<>(); + params.put("h", "300"); + params.put("w", "200"); + String url = UrlUtils.appendUrlParameters("/relative/test", params); + assertEquals("/relative/test?h=300&w=200", url); + } } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index c2529526a898..834fd8b48c6a 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -13,6 +13,8 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; public class UrlUtils { public static String urlEncode(final String text) { @@ -143,11 +145,7 @@ public static String removeQuery(final String urlString) { if (urlString == null) { return null; } - int pos = urlString.indexOf("?"); - if (pos == -1) { - return urlString; - } - return urlString.substring(0, pos); + return Uri.parse(urlString).buildUpon().clearQuery().toString(); } /** @@ -214,7 +212,17 @@ public static boolean isImageUrl(String url) { cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png"); } - public static String appendPreviewURLParameter(String url) { - return Uri.parse(url).buildUpon().appendQueryParameter("preview", "true").build().toString(); + public static String appendUrlParameter(String url, String paramName, String paramValue) { + Map parameters = new HashMap<>(); + parameters.put(paramName, paramValue); + return appendUrlParameters(url, parameters); + } + + public static String appendUrlParameters(String url, Map parameters) { + Uri.Builder uriBuilder = Uri.parse(url).buildUpon(); + for (Map.Entry parameter : parameters.entrySet()) { + uriBuilder.appendQueryParameter(parameter.getKey(), parameter.getValue()); + } + return uriBuilder.build().toString(); } }