Skip to content

Commit

Permalink
Merge branch 'develop' into feature/site-settings-review
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyr59h committed Sep 10, 2015
2 parents c4e8007 + 62ce487 commit 8d781ba
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
4 changes: 2 additions & 2 deletions WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
exclude group: 'commons-logging'
}
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.android.support:support-v13:23.0.0'
compile 'com.android.support:support-v13:23.0.1'
}

android {
Expand All @@ -29,7 +29,7 @@ android {
publishNonDefault true

compileSdkVersion 23
buildToolsVersion '23.0.0'
buildToolsVersion '23.0.1'

defaultConfig {
versionName "1.5.0"
Expand Down
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 @@ -10,10 +10,11 @@
import java.io.UnsupportedEncodingException;
import java.net.IDN;
import java.net.URI;
import java.net.URL;
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 @@ -144,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,4 +211,18 @@ public static boolean isImageUrl(String url) {
return cleanedUrl.endsWith("jpg") || cleanedUrl.endsWith("jpeg") ||
cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png");
}

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 8d781ba

Please sign in to comment.