Skip to content

Commit

Permalink
generic approach to add parameters to a URL
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Sep 9, 2015
1 parent 79b9497 commit 2c0b2d5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
Expand All @@ -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<String, String> 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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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<String, String> parameters = new HashMap<>();
parameters.put(paramName, paramValue);
return appendUrlParameters(url, parameters);
}

public static String appendUrlParameters(String url, Map<String, String> parameters) {
Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
uriBuilder.appendQueryParameter(parameter.getKey(), parameter.getValue());
}
return uriBuilder.build().toString();
}
}

0 comments on commit 2c0b2d5

Please sign in to comment.